-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
49 lines (44 loc) · 1.35 KB
/
Copy pathworker.js
File metadata and controls
49 lines (44 loc) · 1.35 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
importScripts('pips.js');
importScripts('sudsolve.js');
let PipsWasmModule = null;
let SudokuModuleRef = null;
let SolveSudokuStr = null;
PipsModule().then(m => {
PipsWasmModule = m;
postMessage({ type: 'ready' });
});
SudokuModule().then(function(mod) {
SudokuModuleRef = mod;
SolveSudokuStr = SudokuModuleRef.cwrap(
'SolveSudokuStr',
'string',
['string']
);
postMessage({ type: 'ready' });
});
self.onmessage = function(e) {
if (e.data.type === 'solve_pips') {
if (!PipsWasmModule) {
postMessage({ type: 'error', error: 'Pips module not loaded' });
return;
}
try {
const result = PipsWasmModule.ccall('SolvePuzzleStr', 'string', ['string'], [e.data.str]);
postMessage({ type: 'result', result: result });
} catch (err) {
postMessage({ type: 'error', error: err.message });
}
}
if (e.data.type === 'solve_sudoku') {
if (!SolveSudokuStr) {
postMessage({ type: 'error', error: 'Sudoku module not loaded' });
return;
}
try {
const result = SolveSudokuStr(e.data.str);
postMessage({ type: 'result', result: result });
} catch (err) {
postMessage({ type: 'error', error: err.message || 'Solve failed' });
}
}
};