feat(stats): speed up session maintenance and improve stats UI (#111)

This commit is contained in:
2026-06-08 02:20:52 -07:00
committed by GitHub
parent e6a16a069b
commit 311f1e8ee5
108 changed files with 7441 additions and 729 deletions
+65 -6
View File
@@ -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);
}
};
}