This commit is contained in:
syuilo 2026-05-14 15:41:12 +09:00
commit fb130466ae
3 changed files with 26 additions and 3 deletions

View file

@ -84,6 +84,7 @@ import { store } from '@/store.js';
import MkFoldableSection from '@/components/MkFoldableSection.vue';
import { PreviewEngineController } from '@/world/room/previewEngineController.js';
import MkInput from '@/components/MkInput.vue';
import { withTimeout } from '@/utility/promise-timeout.js';
// TODO: instanceidid -> type
@ -168,8 +169,7 @@ watch(selectedId, (newId) => {
const closeWaiting = os.waiting();
nextTick(() => {
try {
// TODO: timeout
controller.loadObject(newId).then(res => {
withTimeout(controller.loadObject(newId), 10000).then(res => {
selectedInstanceId.value = res.id;
selectedObjectOptionsState.value = deepClone(res.options);
controller.resumeRender();

View file

@ -0,0 +1,23 @@
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
export function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {
let timeoutId: ReturnType<typeof setTimeout>;
const timeoutPromise = new Promise<never>((_, reject) => {
// workerで実行される可能性がある
// eslint-disable-next-line no-restricted-globals
timeoutId = setTimeout(() => {
reject(new Error('Operation timed out'));
}, ms);
});
return Promise.race([promise, timeoutPromise])
.finally(() => {
// workerで実行される可能性がある
// eslint-disable-next-line no-restricted-globals
clearTimeout(timeoutId);
});
}

View file

@ -292,7 +292,7 @@ export abstract class EngineControllerBase<T extends RoomEngineBase> {
}
}
protected callAndWaitReturn<FN extends keyof T>(fn: FN, args: Parameters<T[FN]> = [] as any): Promise<ReturnType<T[FN]>> {
protected callAndWaitReturn<FN extends keyof T>(fn: FN, args: Parameters<T[FN]> = [] as any): ReturnType<T[FN]> extends Promise<any> ? ReturnType<T[FN]> : Promise<ReturnType<T[FN]>> {
if (!this.isReady.value) {
throw new Error('Engine is not initialized');
}