-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
29 lines (26 loc) · 927 Bytes
/
Copy pathindex.js
File metadata and controls
29 lines (26 loc) · 927 Bytes
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
//import {} from 'https://unpkg.com/@babel/standalone/babel.min.js';
import {} from '/vendor/babel.js';
const assertWorker = (code, assertion, timeout=1000, babelPresets = {}) => new Promise((resolve, reject) => {
try {
code = Babel.transform(code, babelPresets).code
} catch (ex) {
return reject('Syntax error: '+ ex.message??ex)
}
const workerCode = `try{ ${code}; postMessage(${assertion}) } catch(ex) { postMessage(ex?.message??ex)}`
const worker = new Worker(`data:text/javascript,${encodeURIComponent(workerCode)}`)
let timeoutID
worker.onmessage = (e) => {
clearTimeout(timeoutID)
worker.terminate()
e.data === true
? resolve(true)
: e.data === false
? reject(false)
: reject('Runtime Error: '+e.data)
}
timeoutID = setTimeout(() => {
worker.terminate()
reject(`Timeout after ${timeout} ms`)
}, timeout)
})
export default assertWorker;