Skip to content

Commit

Permalink
chore: engineInfosをPromiseでやる
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenc-nanashi committed Feb 8, 2025
1 parent 83f3d85 commit ceb87ae
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 60 deletions.
63 changes: 26 additions & 37 deletions src/backend/vst/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
logInfo,
logWarn,
logError,
onReceivedIPCMessage,
} from "./ipc";
import {
EngineId,
Expand All @@ -31,6 +32,9 @@ import { UnreachableError } from "@/type/utility";
export const projectFilePath = "/dev/vst-project.vvproj";

let zoomValue = 1;

let engineInfoPromise: Promise<EngineInfo[]> | undefined;

/**
* VST版のSandBox実装
* ブラウザ版のSandBoxを継承している
Expand All @@ -45,45 +49,30 @@ export const api: Sandbox = {
return appInfo;
},
async engineInfos() {
const state = new URLSearchParams(window.location.search);
const status = state.get("engineStatus");
const baseEngineInfo = loadEnvEngineInfos()[0];
if (baseEngineInfo.type != "path") {
throw new Error("default engine type must be path");
if (!engineInfoPromise) {
const { promise, resolve } = Promise.withResolvers<EngineInfo[]>();
// エンジンが準備完了したときの処理
onReceivedIPCMessage("engineReady", ({ port }: { port: number }) => {
const baseEngineInfo = loadEnvEngineInfos()[0];
if (baseEngineInfo.type != "path") {
throw new Error("default engine type must be path");
}
resolve([
{
...baseEngineInfo,
protocol: "http://",
hostname: "localhost",
defaultPort: port.toString(),
pathname: "",
type: "path",
isDefault: true,
},
]);
});
engineInfoPromise = promise;
}
if (status === "ready") {
const port = state.get("port");
if (!port) {
throw new Error("port is not found");
}

const { protocol, hostname, pathname } = new URL(baseEngineInfo.host);
return [
{
...baseEngineInfo,
protocol,
hostname,
defaultPort: port,
pathname: pathname === "/" ? "" : pathname,
type: "path",
isDefault: true,
} satisfies EngineInfo,
];
} else {
// 「エンジン起動中...」を出すため、常に失敗するエンジン情報を返す。
// TODO: もっと良い方法を考える
return [
{
...baseEngineInfo,
type: "path",
protocol: "http://",
hostname: "voicevox-always-fail.internal",
defaultPort: "0",
pathname: "/",
isDefault: true,
},
];
}
return engineInfoPromise;
},
async restartEngine(engineId) {
const engineInfos = await this.engineInfos();
Expand Down
38 changes: 15 additions & 23 deletions src/backend/vst/vstPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { createLogger } from "@/helpers/log";
import { getOrThrow } from "@/helpers/mapHelper";
import { showQuestionDialog } from "@/components/Dialog/Dialog";
import { UnreachableError } from "@/type/utility";
import { loadEnvEngineInfos } from "@/domain/defaultEngine/envEngineInfo";

export type Message =
| {
Expand Down Expand Up @@ -67,29 +68,20 @@ export const vstPlugin: Plugin = {
}
});

// エンジンが準備完了したときの処理
onReceivedIPCMessage("engineReady", ({ port }: { port: number }) => {
location.search = `?engineStatus=ready&port=${port}`;
});

// エンジンが起動していない時はエンジンを起動するように頼む
const urlState = new URLSearchParams(window.location.search);
if (urlState.get("engineStatus") !== "ready") {
void (async () => {
const engineInfos = await window.backend.engineInfos();
const engineSettings =
await window.backend.getSetting("engineSettings");
const engineId = engineInfos[0].uuid;
const engineSetting = engineSettings[engineId];
if (!engineSetting) {
throw new UnreachableError(`unreachable: engineSetting is not found`);
}
await startEngine({
useGpu: engineSetting.useGpu,
forceRestart: false,
});
})();
}
// エンジンを起動するように指示
void (async () => {
const baseEngineInfo = loadEnvEngineInfos()[0];
const engineSettings = await window.backend.getSetting("engineSettings");
const engineId = baseEngineInfo.uuid;
const engineSetting = engineSettings[engineId];
if (!engineSetting) {
throw new UnreachableError(`unreachable: engineSetting is not found`);
}
await startEngine({
useGpu: engineSetting.useGpu,
forceRestart: false,
});
})();

// 再生位置の更新
const updatePlayheadPosition = async () => {
Expand Down

0 comments on commit ceb87ae

Please sign in to comment.