// External imports import { TLStore, TLEditorSnapshot, loadSnapshot, getSnapshot } from '@tldraw/tldraw'; // Local imports import { logger } from '../../debugConfig'; import { LoadingState } from './snapshotService'; import { storageService, StorageKeys } from '../auth/localStorageService'; interface AutoSaveConfig { checkInterval: number; // Changed to required saveInterval: number; // Changed to required } const DEFAULT_CONFIG: AutoSaveConfig = { checkInterval: 5000, saveInterval: 30000 }; export class SharedStoreService { private lastSaveTime: number = Date.now(); private autoSaveInterval: ReturnType | null = null; private config: AutoSaveConfig; private isDirty = false; private dirtyListener: (() => void) | null = null; constructor(private store: TLStore, config?: Partial) { this.config = { ...DEFAULT_CONFIG, ...config }; this.dirtyListener = store.listen(() => { this.isDirty = true; }); logger.debug('shared-store-service', '๐Ÿ—๏ธ Initializing SharedStoreService'); } public startAutoSave(setLoadingState: (state: LoadingState) => void): void { if (this.autoSaveInterval) { this.stopAutoSave(); } this.autoSaveInterval = setInterval(() => { this.checkAndSave(setLoadingState); }, this.config.checkInterval); logger.debug('shared-store-service', 'โฐ Auto-save started', { checkInterval: this.config.checkInterval, saveInterval: this.config.saveInterval }); } public stopAutoSave(): void { if (this.autoSaveInterval) { clearInterval(this.autoSaveInterval); this.autoSaveInterval = null; logger.debug('shared-store-service', 'โน๏ธ Auto-save stopped'); } } private async checkAndSave(setLoadingState: (state: LoadingState) => void): Promise { if (!this.isDirty) { return; } this.isDirty = false; const now = Date.now(); if (now - this.lastSaveTime >= this.config.saveInterval) { await this.saveSnapshot(getSnapshot(this.store), setLoadingState); this.lastSaveTime = now; } } public async saveSnapshot( snapshot: Partial, setLoadingState: (state: LoadingState) => void ): Promise { try { storageService.set(StorageKeys.LOCAL_SNAPSHOT, snapshot); setLoadingState({ status: 'ready', error: '' }); logger.debug('shared-store-service', 'โœ… Snapshot saved successfully'); } catch (error) { logger.error('shared-store-service', 'โŒ Failed to save snapshot:', error); setLoadingState({ status: 'error', error: error instanceof Error ? error.message : 'Failed to save snapshot' }); } } public async loadSnapshot( snapshot: Partial, setLoadingState: (state: LoadingState) => void ): Promise { try { setLoadingState({ status: 'loading', error: '' }); loadSnapshot(this.store, snapshot); setLoadingState({ status: 'ready', error: '' }); logger.debug('shared-store-service', 'โœ… Snapshot loaded successfully'); } catch (error) { logger.error('shared-store-service', 'โŒ Failed to load snapshot:', error); this.store.clear(); setLoadingState({ status: 'error', error: error instanceof Error ? error.message : 'Failed to load snapshot' }); } } public getStore(): TLStore { return this.store; } public clear(): void { this.stopAutoSave(); this.dirtyListener?.(); this.dirtyListener = null; this.store.clear(); this.isDirty = false; logger.debug('shared-store-service', '๐Ÿงน Store cleared'); } } export const createSharedStore = ( store: TLStore, config?: Partial ): SharedStoreService => { return new SharedStoreService(store, config); };