Repository navigation
Expand file tree
/
Copy pathentropy-worker.ts
More file actions
61 lines (51 loc) · 1.86 KB
/
Copy pathentropy-worker.ts
File metadata and controls
61 lines (51 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Web Worker for the Wordle solving pipeline
*
* Offloads filtering + Shannon-entropy ranking off the main thread so the UI
* stays responsive. All algorithmic logic lives in `lib/logic/solver.ts` and is
* imported here — there is no duplicated logic between threads.
*
* The dictionary is imported statically (bundled), so it never travels over
* postMessage: a SOLVE request only carries the guesses made so far.
*/
import { solve } from '../lib/logic/solver';
import { buildDictionary } from '../lib/logic/dictionary';
import type {
WorkerRequest,
WorkerResponse,
WorkerProgress,
} from '../lib/types';
// Built once when the worker module loads.
const dictionary = buildDictionary();
// Incremented on each CANCEL so an in-flight progress callback can bail out.
let generation = 0;
self.onmessage = (e: MessageEvent<WorkerRequest>) => {
const data = e.data;
if (data.type === 'CANCEL') {
generation++;
return;
}
if (data.type === 'SOLVE') {
const myGeneration = ++generation;
const startTime = performance.now();
const result = solve(data.guesses, dictionary, (processed, total) => {
// Cooperative cancellation: a newer request (or a CANCEL) bumped the
// generation, so stop reporting progress for this stale run.
if (myGeneration !== generation) return;
const progress: WorkerProgress = { type: 'PROGRESS', processed, total };
self.postMessage(progress);
});
// If a newer request superseded this one mid-computation, drop the result.
if (myGeneration !== generation) return;
const response: WorkerResponse = {
type: 'ENTROPY_RESULT',
suggestions: result.suggestions,
remainingCount: result.remainingCount,
solution: result.solution,
calculationTime: performance.now() - startTime,
};
self.postMessage(response);
}
};
// Make this file a module.
export {};