UNPKG

@eka-care/patient-ts-sdk

Version:

TypeScript SDK for Trinity Patient Profile Management System

96 lines (95 loc) 2.8 kB
/** * Web Worker for background data synchronization */ import { DataLoaderService } from '../services/data-loader'; class SyncWorker { constructor() { this.dataLoader = null; this.isRunning = false; this.lastSyncTime = 0; self.addEventListener('message', this.handleMessage.bind(this)); } handleMessage(event) { const { type, payload } = event.data; switch (type) { case 'start': if (payload) { this.startSync(payload.config); } break; case 'stop': this.stopSync(); break; case 'status': this.sendStatus(); break; } } async startSync(config) { if (this.isRunning) { this.postMessage({ type: 'error', payload: { error: 'Sync already running' } }); return; } this.isRunning = true; try { // Initialize data loader this.dataLoader = new DataLoaderService(config); await this.dataLoader.init(); // Start loading data await this.dataLoader.loadAllData({ onProgress: (progress) => { this.postMessage({ type: 'progress', payload: { progress } }); }, onComplete: () => { this.lastSyncTime = Date.now(); this.postMessage({ type: 'complete', payload: { lastSync: this.lastSyncTime } }); }, onError: (error) => { this.postMessage({ type: 'error', payload: { error } }); } }); } catch (error) { this.postMessage({ type: 'error', payload: { error: error instanceof Error ? error.message : 'Unknown error' } }); } finally { this.isRunning = false; if (this.dataLoader) { this.dataLoader.close(); this.dataLoader = null; } } } stopSync() { if (this.dataLoader) { this.dataLoader.stopLoading(); } this.isRunning = false; } sendStatus() { this.postMessage({ type: 'status', payload: { isRunning: this.isRunning, lastSync: this.lastSyncTime } }); } postMessage(message) { self.postMessage(message); } } // Initialize worker new SyncWorker();