-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathworker.js
50 lines (48 loc) · 1.18 KB
/
worker.js
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
import {load} from './utils.js';
let db = null;
addEventListener('message', ({data: {id, action, options}}) => {
if (action === 'init') {
if (!db)
db = load(options.library).then(({init}) => init(options));
db.then(
() => postMessage({id, result: 'OK'}),
({message: error}) => postMessage({id, error})
);
}
else if (action === 'close') {
db.then(module => module.close().then(
() => {
postMessage({id});
},
({message: error}) => {
postMessage({id, error});
}
));
}
else if (action === 'create_function') {
db.then(module => {
try {
const [name, func] = options;
const result = module[action](name, Function('return ' + func)());
postMessage({id, result});
}
catch({message: error}) {
postMessage({id, error});
}
});
}
// action === `all` || `get` || `query`
else {
const {template, values} = options;
db.then((module) => {
module[action](template, ...values).then(
result => {
postMessage({id, result});
},
({message: error}) => {
postMessage({id, error});
}
);
});
}
});