Skip to content

Commit

Permalink
Fix nested worker instantiation breaking from document global
Browse files Browse the repository at this point in the history
This should fix #99, and uses the newer Event API where supported (including workers).
  • Loading branch information
developit authored Jan 9, 2021
1 parent 7f649ef commit 2d8b643
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/rpc-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export default function addMethods(worker, methods) {
let c = 0;
let callbacks = {};
worker.addEventListener('message', (e) => {
let d = e.data;
let d = e.data, evt;
if (d.type!=='RPC') return;
if (d.id) {
let f = callbacks[d.id];
Expand All @@ -17,8 +17,12 @@ export default function addMethods(worker, methods) {
}
}
else {
let evt = document.createEvent('Event');
evt.initEvent(d.method, false, false);
try {
evt = new Event(d.method);
} catch (e) {
evt = document.createEvent('Event');
evt.initEvent(d.method, false, false);
}
evt.data = d.params;
worker.dispatchEvent(evt);
}
Expand Down

0 comments on commit 2d8b643

Please sign in to comment.