This commit is contained in:
syuilo 2026-05-14 14:38:00 +09:00
commit bfdda83a6d
2 changed files with 24 additions and 14 deletions

View file

@ -279,25 +279,35 @@ export abstract class EngineControllerBase<T extends RoomEngineBase> {
private callCounter = 0;
// TODO: いい感じに型付け
protected call(fn, args = [], needReturnValue = false) {
protected call<FN extends keyof T>(fn: FN, args: Parameters<T[FN]> = [] as any): void {
if (!this.isReady.value) {
throw new Error('Engine is not initialized');
}
if (this.worker != null) {
if (needReturnValue) {
return new Promise((resolve) => {
const id = this.callCounter++;
this.returnHooks.set(id, (value) => {
resolve(value);
});
this.worker!.postMessage({ type: 'call', fn, args, needReturnValue: true, id });
});
} else {
this.worker.postMessage({ type: 'call', fn, args });
}
this.worker.postMessage({ type: 'call', fn, args });
} else if (this.engine != null) {
return this.engine[fn](...args);
this.engine[fn](...args);
} else {
throw new Error('Engine is not initialized');
}
}
protected callAndWaitReturn<FN extends keyof T>(fn: FN, args: Parameters<T[FN]> = [] as any): Promise<ReturnType<T[FN]>> {
if (!this.isReady.value) {
throw new Error('Engine is not initialized');
}
if (this.worker != null) {
return new Promise((resolve) => {
const id = this.callCounter++;
this.returnHooks.set(id, (value) => {
resolve(value);
});
this.worker!.postMessage({ type: 'call', fn, args, needReturnValue: true, id });
});
} else if (this.engine != null) {
return new Promise((resolve) => {
resolve(this.engine![fn](...args));
});
} else {
throw new Error('Engine is not initialized');
}

View file

@ -48,7 +48,7 @@ export class PreviewEngineController extends EngineControllerBase<RoomObjectPrev
}
public loadObject(type: string) {
return this.call('loadObject', [type], true);
return this.callAndWaitReturn('loadObject', [type]);
}
public clearObject() {