feat: Implement Worker Service v2 with improved architecture

- 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.
This commit is contained in:
Alex Newman
2025-11-06 23:56:25 -05:00
parent 9eddc51979
commit 980151a50e
12 changed files with 3721 additions and 2 deletions
+68
View File
@@ -0,0 +1,68 @@
/**
* 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();
}
}