From 2d8b643f646a213fd96136fe1fb0ad4665af0fbc Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Fri, 8 Jan 2021 20:36:05 -0500 Subject: [PATCH] Fix nested worker instantiation breaking from document global This should fix #99, and uses the newer Event API where supported (including workers). --- src/rpc-wrapper.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/rpc-wrapper.js b/src/rpc-wrapper.js index 667a127..511f279 100644 --- a/src/rpc-wrapper.js +++ b/src/rpc-wrapper.js @@ -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]; @@ -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); }