mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-06-12 15:13:32 -07:00
feat(stats): speed up session maintenance and improve stats UI (#111)
This commit is contained in:
@@ -1,18 +1,24 @@
|
||||
export type StatsWordHelperResponse = {
|
||||
ok: boolean;
|
||||
noteId?: number;
|
||||
deckName?: string;
|
||||
error?: string;
|
||||
};
|
||||
|
||||
type StatsWordHelperMode = 'add-word' | 'deck-name';
|
||||
|
||||
export type StatsWordHelperSpawnOptions = {
|
||||
scriptPath: string;
|
||||
responsePath: string;
|
||||
userDataPath: string;
|
||||
mode: StatsWordHelperMode;
|
||||
word?: string;
|
||||
};
|
||||
|
||||
export function createInvokeStatsWordHelperHandler(deps: {
|
||||
createTempDir: (prefix: string) => string;
|
||||
joinPath: (...parts: string[]) => string;
|
||||
spawnHelper: (options: {
|
||||
scriptPath: string;
|
||||
responsePath: string;
|
||||
userDataPath: string;
|
||||
word: string;
|
||||
}) => Promise<number>;
|
||||
spawnHelper: (options: StatsWordHelperSpawnOptions) => Promise<number>;
|
||||
waitForResponse: (responsePath: string) => Promise<StatsWordHelperResponse>;
|
||||
removeDir: (targetPath: string) => void;
|
||||
}) {
|
||||
@@ -29,6 +35,7 @@ export function createInvokeStatsWordHelperHandler(deps: {
|
||||
scriptPath: options.helperScriptPath,
|
||||
responsePath,
|
||||
userDataPath: options.userDataPath,
|
||||
mode: 'add-word',
|
||||
word: options.word,
|
||||
});
|
||||
|
||||
@@ -64,3 +71,55 @@ export function createInvokeStatsWordHelperHandler(deps: {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function createReadStatsYomitanDeckNameHandler(deps: {
|
||||
createTempDir: (prefix: string) => string;
|
||||
joinPath: (...parts: string[]) => string;
|
||||
spawnHelper: (options: StatsWordHelperSpawnOptions) => Promise<number>;
|
||||
waitForResponse: (responsePath: string) => Promise<StatsWordHelperResponse>;
|
||||
removeDir: (targetPath: string) => void;
|
||||
}) {
|
||||
return async (options: { helperScriptPath: string; userDataPath: string }): Promise<string> => {
|
||||
const tempDir = deps.createTempDir('subminer-stats-word-helper-');
|
||||
const responsePath = deps.joinPath(tempDir, 'response.json');
|
||||
|
||||
try {
|
||||
const helperExitPromise = deps.spawnHelper({
|
||||
scriptPath: options.helperScriptPath,
|
||||
responsePath,
|
||||
userDataPath: options.userDataPath,
|
||||
mode: 'deck-name',
|
||||
});
|
||||
|
||||
const startupResult = await Promise.race([
|
||||
deps
|
||||
.waitForResponse(responsePath)
|
||||
.then((response) => ({ kind: 'response' as const, response })),
|
||||
helperExitPromise.then((status) => ({ kind: 'exit' as const, status })),
|
||||
]);
|
||||
|
||||
let response: StatsWordHelperResponse;
|
||||
if (startupResult.kind === 'response') {
|
||||
response = startupResult.response;
|
||||
} else {
|
||||
if (startupResult.status !== 0) {
|
||||
throw new Error(
|
||||
`Stats word helper exited before response (status ${startupResult.status}).`,
|
||||
);
|
||||
}
|
||||
response = await deps.waitForResponse(responsePath);
|
||||
}
|
||||
|
||||
const exitStatus = await helperExitPromise;
|
||||
if (exitStatus !== 0) {
|
||||
throw new Error(`Stats word helper exited with status ${exitStatus}.`);
|
||||
}
|
||||
if (!response.ok || typeof response.deckName !== 'string') {
|
||||
throw new Error(response.error || 'Stats word helper failed.');
|
||||
}
|
||||
return response.deckName.trim();
|
||||
} finally {
|
||||
deps.removeDir(tempDir);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user