import { getContext, setContext } from 'svelte'; import { FileManager } from '../project-system/file-manager'; import { FileDatabase } from '../project-system/db'; import { createCsoundStore } from '../csound/store'; import { ExecutionContext } from '../csound/execution-context'; import { createEditorSettingsStore } from '../stores/editorSettings'; import { EditorState } from '../stores/editorState.svelte'; import { UIState } from '../stores/uiState.svelte'; import { getCsoundVersion } from '../utils/preferences'; import type { CsoundStore } from '../csound/store'; import type { EditorSettingsStore } from '../stores/editorSettings'; export interface AppContext { fileManager: FileManager; csound: CsoundStore; executionContext: ExecutionContext; editorSettings: EditorSettingsStore; editorState: EditorState; uiState: UIState; } const APP_CONTEXT_KEY = Symbol('app-context'); export function createAppContext(): AppContext { const db = new FileDatabase(); const fileManager = new FileManager(db); const csound = createCsoundStore({ getProjectFiles: async () => { const result = await fileManager.getAllFiles(); return result.success ? result.data : []; }, useCsound7: getCsoundVersion() }); const executionContext = new ExecutionContext(csound); return { fileManager, csound, executionContext, editorSettings: createEditorSettingsStore(), editorState: new EditorState(fileManager), uiState: new UIState() }; } export function setAppContext(context: AppContext): void { setContext(APP_CONTEXT_KEY, context); } export function getAppContext(): AppContext { const context = getContext(APP_CONTEXT_KEY); if (!context) { throw new Error('AppContext not found. Did you forget to call setAppContext?'); } return context; }