980151a50e
- Complete rewrite of the Worker Service following object-oriented principles. - Introduced a single long-lived database connection to reduce overhead. - Implemented event-driven queues to eliminate polling. - Added DRY utilities for pagination and settings management. - Reduced code size significantly from 1173 lines to approximately 600-700 lines. - Created various composed services including DatabaseManager, SessionManager, and SDKAgent. - Enhanced SSE broadcasting capabilities for real-time client updates. - Established a structured approach for session lifecycle management and event handling. - Introduced type safety with shared TypeScript interfaces for better maintainability.
69 lines
1.8 KiB
TypeScript
69 lines
1.8 KiB
TypeScript
/**
|
|
* SettingsManager: DRY settings CRUD utility
|
|
*
|
|
* Responsibility:
|
|
* - DRY helper for viewer settings CRUD
|
|
* - Eliminates duplication in settings read/write logic
|
|
* - Type-safe settings management
|
|
*/
|
|
|
|
import { DatabaseManager } from './DatabaseManager.js';
|
|
import { logger } from '../../utils/logger.js';
|
|
import type { ViewerSettings } from '../worker-types.js';
|
|
|
|
export class SettingsManager {
|
|
private dbManager: DatabaseManager;
|
|
private readonly defaultSettings: ViewerSettings = {
|
|
sidebarOpen: true,
|
|
selectedProject: null,
|
|
theme: 'system'
|
|
};
|
|
|
|
constructor(dbManager: DatabaseManager) {
|
|
this.dbManager = dbManager;
|
|
}
|
|
|
|
/**
|
|
* Get current viewer settings (with defaults)
|
|
*/
|
|
getSettings(): ViewerSettings {
|
|
const db = this.dbManager.getSessionStore().db;
|
|
|
|
try {
|
|
const stmt = db.prepare('SELECT key, value FROM viewer_settings');
|
|
const rows = stmt.all() as Array<{ key: string; value: string }>;
|
|
|
|
const settings: ViewerSettings = { ...this.defaultSettings };
|
|
for (const row of rows) {
|
|
const key = row.key as keyof ViewerSettings;
|
|
if (key in settings) {
|
|
(settings as any)[key] = JSON.parse(row.value);
|
|
}
|
|
}
|
|
|
|
return settings;
|
|
} catch (error) {
|
|
logger.debug('WORKER', 'Failed to load settings, using defaults', {}, error as Error);
|
|
return { ...this.defaultSettings };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update viewer settings (partial update)
|
|
*/
|
|
updateSettings(updates: Partial<ViewerSettings>): ViewerSettings {
|
|
const db = this.dbManager.getSessionStore().db;
|
|
|
|
const stmt = db.prepare(`
|
|
INSERT OR REPLACE INTO viewer_settings (key, value)
|
|
VALUES (?, ?)
|
|
`);
|
|
|
|
for (const [key, value] of Object.entries(updates)) {
|
|
stmt.run(key, JSON.stringify(value));
|
|
}
|
|
|
|
return this.getSettings();
|
|
}
|
|
}
|