Skip to content

Commit

Permalink
Merge pull request #704 from postmanlabs/feature/avoid-using-undici-m…
Browse files Browse the repository at this point in the history
…essage-event-class

Switch to node style listener to avoid using undici's (node dep) MessageEvent class
  • Loading branch information
appurva21 authored Aug 21, 2024
2 parents fd04626 + 560e509 commit 6494d76
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 4 additions & 4 deletions lib/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ const Flatted = require('flatted'),
__uvm_emit = function (postMessage, args) {
postMessage({__id_uvm: "${id}", __emit_uvm: args});
}.bind(null, __self.postMessage.bind(__self));
__uvm_addEventListener = __self.addEventListener.bind(__self);
__uvm_on = __self.on.bind(__self);
${bridgeClientCode()}
(function (emit, id) {
__uvm_addEventListener("message", function (e) {
const { __emit_uvm, __id_uvm } = e?.data || e || {};
__uvm_on("message", function (e) {
const { __emit_uvm, __id_uvm } = e || {};
if (typeof __emit_uvm === 'string' && __id_uvm === id) {
emit(__emit_uvm);
}
});
}(__uvm_dispatch, "${id}"));
__uvm_dispatch = null; delete __uvm_dispatch;
__uvm_addEventListener = null; delete __uvm_addEventListener;
__uvm_on = null; delete __uvm_on;
(function (self, bridge, setTimeout) {
${Worker.__exceptionHandler}
Expand Down
9 changes: 8 additions & 1 deletion lib/worker.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ class WebWorker {
callback && callback();
}

static __self = 'self';
static __self = `(function () {
self.on = function (name, listener) {
return self.addEventListener(name, function (e) {
listener(e.data);
});
};
return self;
}())`;

static __exceptionHandler = `
((close) => {
Expand Down

0 comments on commit 6494d76

Please sign in to comment.