|
15 | 15 | let close_after, limit_tmr |
16 | 16 | let sub = null |
17 | 17 | let time_limit = 0 |
| 18 | +let worker = null |
18 | 19 |
|
19 | 20 | let grzDump = (msg) => { |
20 | 21 | console.log(`[grz harness][${new Date().toUTCString()}] ${msg}\n`) |
21 | 22 | } |
22 | 23 |
|
23 | | -let poll = () => { |
24 | | - // poll until sub is closed |
25 | | - // sub should be closed by either itself (test) or time limit |
26 | | - if (!sub || sub.closed) |
27 | | - setTimeout(main) |
28 | | - else |
29 | | - setTimeout(poll, 50) |
30 | | -} |
31 | | - |
32 | 24 | let setMsg = (msgStr) => { |
33 | 25 | try { |
34 | 26 | document.getElementById('msg').innerHTML = msgStr |
|
37 | 29 | } |
38 | 30 | } |
39 | 31 |
|
| 32 | +let startPolling = () => { |
| 33 | + if (worker) { |
| 34 | + worker.terminate() |
| 35 | + } |
| 36 | + |
| 37 | + const workerCode = ` |
| 38 | + let pollInterval = null; |
| 39 | +
|
| 40 | + self.onmessage = function(e) { |
| 41 | + if (e.data.command === 'start') { |
| 42 | + // Start polling every 50ms |
| 43 | + if (pollInterval) clearInterval(pollInterval); |
| 44 | + pollInterval = setInterval(() => { |
| 45 | + self.postMessage({type: 'poll'}); |
| 46 | + }, 50); |
| 47 | + } else if (e.data.command === 'stop') { |
| 48 | + if (pollInterval) { |
| 49 | + clearInterval(pollInterval); |
| 50 | + pollInterval = null; |
| 51 | + } |
| 52 | + } |
| 53 | + }; |
| 54 | + `; |
| 55 | + |
| 56 | + const blob = new Blob([workerCode], {type: 'application/javascript'}) |
| 57 | + worker = new Worker(URL.createObjectURL(blob)) |
| 58 | + |
| 59 | + worker.onmessage = function(e) { |
| 60 | + if (e.data.type === 'poll') { |
| 61 | + // Check window status |
| 62 | + try { |
| 63 | + if (!sub || sub.closed) { |
| 64 | + grzDump('Worker poll: sub is closed, calling main()') |
| 65 | + worker.postMessage({command: 'stop'}) |
| 66 | + setTimeout(main) |
| 67 | + } |
| 68 | + } catch (error) { |
| 69 | + grzDump(`Worker poll error: ${error}`) |
| 70 | + worker.postMessage({command: 'stop'}) |
| 71 | + setTimeout(main, 500) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + worker.onerror = function(error) { |
| 77 | + grzDump(`Worker error: ${error}`) |
| 78 | + setTimeout(main, 500) |
| 79 | + } |
| 80 | + |
| 81 | + worker.postMessage({command: 'start'}) |
| 82 | +} |
| 83 | + |
40 | 84 | let main = () => { |
41 | 85 | // if limit_tmr is set, clear it |
42 | 86 | if (limit_tmr !== undefined) { |
|
98 | 142 | } |
99 | 143 | }, time_limit) |
100 | 144 | } |
101 | | - // wait for test to complete |
102 | | - setTimeout(poll, 50) |
| 145 | + startPolling() |
103 | 146 | } |
104 | 147 | } |
105 | 148 | } |
|
124 | 167 | // begin iterations |
125 | 168 | setTimeout(main) |
126 | 169 | }) |
| 170 | + |
| 171 | +// Clean up worker when page unloads |
| 172 | +window.addEventListener('beforeunload', () => { |
| 173 | + if (worker) { |
| 174 | + worker.terminate() |
| 175 | + } |
| 176 | +}) |
127 | 177 | </script> |
128 | 178 | </head> |
129 | 179 | <body> |
|
0 commit comments