Files
oldboy/src/lib/contexts/app-context.ts
2025-11-14 01:53:18 +01:00

59 lines
1.8 KiB
TypeScript

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<AppContext>(APP_CONTEXT_KEY, context);
}
export function getAppContext(): AppContext {
const context = getContext<AppContext>(APP_CONTEXT_KEY);
if (!context) {
throw new Error('AppContext not found. Did you forget to call setAppContext?');
}
return context;
}