Files
oldboy/src/lib/contexts/app-context.ts

50 lines
1.6 KiB
TypeScript

import { getContext, setContext } from 'svelte';
import { ProjectManager } from '../project-system/project-manager';
import { ProjectDatabase } from '../project-system/db';
import { createCsoundStore } from '../csound/store';
import { ExecutionContext } from '../csound/execution-context';
import { createEditorSettingsStore } from '../stores/editorSettings';
import { ProjectEditor } from '../stores/projectEditor.svelte';
import { UIState } from '../stores/uiState.svelte';
import type { CsoundStore } from '../csound/store';
import type { EditorSettingsStore } from '../stores/editorSettings';
export interface AppContext {
projectManager: ProjectManager;
csound: CsoundStore;
executionContext: ExecutionContext;
editorSettings: EditorSettingsStore;
projectEditor: ProjectEditor;
uiState: UIState;
}
const APP_CONTEXT_KEY = Symbol('app-context');
export function createAppContext(): AppContext {
const db = new ProjectDatabase();
const projectManager = new ProjectManager(db);
const csound = createCsoundStore();
const executionContext = new ExecutionContext(csound, 'composition');
return {
projectManager,
csound,
executionContext,
editorSettings: createEditorSettingsStore(),
projectEditor: new ProjectEditor(projectManager),
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;
}