|
- class WBManifestWorkerWrapper {
- constructor() {
- this.worker = new Worker('/js/wb-manifest-worker.js');
- this.worker.onmessage = e => this.onMessage(e);
- this.worker.onerror = e => console.log(e);
- this.queue = [];
- }
-
- onMessage(e) {
- if (this.queue.length === 0)
- throw Error('Unexpected message from worker');
- const [msgType, accept, reject] = this.queue.splice(0, 1)[0];
- if (e.data[0] === msgType + 'Result')
- accept(e);
- else
- reject(e);
- }
-
- postMessage(m) {
- const prom = new Promise((accept, reject) => {
- this.queue.push([ m[0], accept, reject ]);
- this.worker.postMessage(m);
- });
- return prom;
- }
- }
-
- export default WBManifestWorkerWrapper;
|