export function recalculateConfidenceScores(
matches: MangaMatchResult[],
config: Partial<MatchEngineConfig>,
options: ConfidenceRecalculationOptions = {},
): ConfidenceRecalculationExecution {
const taskId = createTaskId();
const pool = getGenericWorkerPool();
const { useWorkers = true, onProgress, shouldContinue, yieldEvery } = options;
const effectiveYieldEvery =
yieldEvery ??
(matches.length > LARGE_MATCH_YIELD_THRESHOLD
? LARGE_MATCH_YIELD_COUNT
: undefined);
const promise = (async () => {
if (useWorkers) {
try {
const result = await executeViaWorker(
pool,
taskId,
matches,
config,
onProgress,
shouldContinue,
effectiveYieldEvery,
);
if (result) {
return result;
}
} catch (error) {
console.warn(
"[ConfidenceRecalc] Worker execution failed, falling back to main thread",
error,
);
}
}
return recalculateOnMainThread(matches, config, {
onProgress,
shouldContinue,
yieldEvery: effectiveYieldEvery,
});
})();
return {
taskId,
promise,
cancel: () => {
if (useWorkers) {
pool.cancelTask(taskId);
}
},
};
}
Executes confidence recalculation using a worker when possible. Falls back to main-thread processing if workers are not available.