mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-03-02 06:22:42 -08:00
fix(osd): show subtitle-annotation loading status during tokenization
This commit is contained in:
@@ -66,8 +66,88 @@ export function createCreateMecabTokenizerAndCheckMainHandler<TMecab>(deps: {
|
||||
export function createPrewarmSubtitleDictionariesMainHandler(deps: {
|
||||
ensureJlptDictionaryLookup: () => Promise<void>;
|
||||
ensureFrequencyDictionaryLookup: () => Promise<void>;
|
||||
showMpvOsd?: (message: string) => void;
|
||||
shouldShowOsdNotification?: () => boolean;
|
||||
setInterval?: (callback: () => void, delayMs: number) => unknown;
|
||||
clearInterval?: (timer: unknown) => void;
|
||||
}) {
|
||||
return async (): Promise<void> => {
|
||||
await Promise.all([deps.ensureJlptDictionaryLookup(), deps.ensureFrequencyDictionaryLookup()]);
|
||||
let prewarmed = false;
|
||||
let prewarmPromise: Promise<void> | null = null;
|
||||
let loadingOsdDepth = 0;
|
||||
let loadingOsdFrame = 0;
|
||||
let loadingOsdTimer: unknown = null;
|
||||
const showMpvOsd = deps.showMpvOsd;
|
||||
const shouldShowOsdNotification = deps.shouldShowOsdNotification ?? (() => false);
|
||||
const setIntervalHandler =
|
||||
deps.setInterval ??
|
||||
((callback: () => void, delayMs: number): unknown => setInterval(callback, delayMs));
|
||||
const clearIntervalHandler =
|
||||
deps.clearInterval ??
|
||||
((timer: unknown): void => clearInterval(timer as ReturnType<typeof setInterval>));
|
||||
const spinnerFrames = ['|', '/', '-', '\\'];
|
||||
|
||||
const beginLoadingOsd = (): boolean => {
|
||||
if (!showMpvOsd || !shouldShowOsdNotification()) {
|
||||
return false;
|
||||
}
|
||||
loadingOsdDepth += 1;
|
||||
if (loadingOsdDepth > 1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
loadingOsdFrame = 0;
|
||||
showMpvOsd(`Loading subtitle annotations ${spinnerFrames[loadingOsdFrame]}`);
|
||||
loadingOsdFrame += 1;
|
||||
loadingOsdTimer = setIntervalHandler(() => {
|
||||
if (!showMpvOsd) {
|
||||
return;
|
||||
}
|
||||
showMpvOsd(`Loading subtitle annotations ${spinnerFrames[loadingOsdFrame % spinnerFrames.length]}`);
|
||||
loadingOsdFrame += 1;
|
||||
}, 180);
|
||||
return true;
|
||||
};
|
||||
|
||||
const endLoadingOsd = (): void => {
|
||||
if (!showMpvOsd || !shouldShowOsdNotification()) {
|
||||
return;
|
||||
}
|
||||
|
||||
loadingOsdDepth = Math.max(0, loadingOsdDepth - 1);
|
||||
if (loadingOsdDepth > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (loadingOsdTimer) {
|
||||
clearIntervalHandler(loadingOsdTimer);
|
||||
loadingOsdTimer = null;
|
||||
}
|
||||
showMpvOsd('Subtitle annotations loaded');
|
||||
};
|
||||
|
||||
return async (options?: { showLoadingOsd?: boolean }): Promise<void> => {
|
||||
if (prewarmed) {
|
||||
return;
|
||||
}
|
||||
const shouldTrackLoadingOsd = options?.showLoadingOsd === true;
|
||||
const loadingOsdStarted = shouldTrackLoadingOsd ? beginLoadingOsd() : false;
|
||||
|
||||
if (!prewarmPromise) {
|
||||
prewarmPromise = (async () => {
|
||||
try {
|
||||
await Promise.all([deps.ensureJlptDictionaryLookup(), deps.ensureFrequencyDictionaryLookup()]);
|
||||
prewarmed = true;
|
||||
} finally {
|
||||
prewarmPromise = null;
|
||||
}
|
||||
})();
|
||||
}
|
||||
try {
|
||||
await prewarmPromise;
|
||||
} finally {
|
||||
if (loadingOsdStarted) {
|
||||
endLoadingOsd();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user