Integrate CCN 2025-2026 files
This commit is contained in:
@ -1,102 +1,48 @@
|
||||
import type { CsoundStore } from './store';
|
||||
import type { ProjectMode } from '../project-system/types';
|
||||
import { CompositionStrategy, LiveCodingStrategy, type ExecutionStrategy } from './execution-strategies';
|
||||
|
||||
export type EvalSource = 'selection' | 'block' | 'document';
|
||||
|
||||
export interface ExecutionSession {
|
||||
mode: ProjectMode;
|
||||
projectId: string | null;
|
||||
isActive: boolean;
|
||||
startTime: Date;
|
||||
}
|
||||
export type EvalSource = 'selection' | 'block' | 'file';
|
||||
|
||||
export class ExecutionContext {
|
||||
private session: ExecutionSession | null = null;
|
||||
private strategy: ExecutionStrategy;
|
||||
private currentMode: ProjectMode;
|
||||
private contentProvider: (() => string) | null = null;
|
||||
|
||||
constructor(
|
||||
private csound: CsoundStore,
|
||||
mode: ProjectMode
|
||||
) {
|
||||
this.currentMode = mode;
|
||||
this.strategy = this.createStrategyForMode(mode);
|
||||
}
|
||||
|
||||
get mode(): ProjectMode {
|
||||
return this.currentMode;
|
||||
}
|
||||
|
||||
get activeSession(): ExecutionSession | null {
|
||||
return this.session;
|
||||
}
|
||||
constructor(private csound: CsoundStore) {}
|
||||
|
||||
setContentProvider(provider: () => string): void {
|
||||
this.contentProvider = provider;
|
||||
}
|
||||
|
||||
async startSession(projectId: string | null): Promise<void> {
|
||||
await this.endSession();
|
||||
|
||||
this.session = {
|
||||
mode: this.currentMode,
|
||||
projectId,
|
||||
isActive: true,
|
||||
startTime: new Date()
|
||||
};
|
||||
|
||||
if (this.currentMode === 'livecoding') {
|
||||
this.resetStrategy();
|
||||
}
|
||||
}
|
||||
|
||||
async endSession(): Promise<void> {
|
||||
if (!this.session?.isActive) return;
|
||||
|
||||
if (this.currentMode === 'livecoding') {
|
||||
await this.csound.stop();
|
||||
this.resetStrategy();
|
||||
/**
|
||||
* Execute entire file
|
||||
*/
|
||||
async executeFile(): Promise<void> {
|
||||
const content = this.contentProvider?.() ?? '';
|
||||
if (!content.trim()) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.session = { ...this.session, isActive: false };
|
||||
await this.csound.stop();
|
||||
await this.csound.evaluateCode(content);
|
||||
}
|
||||
|
||||
async execute(code: string, source: EvalSource): Promise<void> {
|
||||
if (this.currentMode === 'livecoding' && (!this.session || !this.session.isActive)) {
|
||||
await this.startSession(this.session?.projectId ?? null);
|
||||
/**
|
||||
* Execute code block (instrument, opcode, or paragraph)
|
||||
*/
|
||||
async executeBlock(code: string): Promise<void> {
|
||||
if (!code.trim()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const fullContent = this.contentProvider?.() ?? '';
|
||||
await this.strategy.execute(this.csound, code, fullContent, source);
|
||||
await this.csound.evaluateCode(code);
|
||||
}
|
||||
|
||||
async switchMode(newMode: ProjectMode): Promise<void> {
|
||||
if (newMode === this.currentMode) return;
|
||||
|
||||
await this.endSession();
|
||||
this.currentMode = newMode;
|
||||
this.strategy = this.createStrategyForMode(newMode);
|
||||
|
||||
if (newMode === 'composition') {
|
||||
await this.csound.stop();
|
||||
/**
|
||||
* Execute selected text
|
||||
*/
|
||||
async executeSelection(code: string): Promise<void> {
|
||||
if (!code.trim()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private createStrategyForMode(mode: ProjectMode): ExecutionStrategy {
|
||||
return mode === 'livecoding'
|
||||
? new LiveCodingStrategy()
|
||||
: new CompositionStrategy();
|
||||
}
|
||||
|
||||
private resetStrategy(): void {
|
||||
if (this.strategy instanceof LiveCodingStrategy) {
|
||||
this.strategy.reset();
|
||||
}
|
||||
}
|
||||
|
||||
async destroy(): Promise<void> {
|
||||
await this.endSession();
|
||||
await this.csound.evaluateCode(code);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user