Skip to content

Commit 7160ae1

Browse files
G07chaKonstantin Azizovdevelopit
authored
feat: switch worker.terminate to log warning instead of throwing error (#13)
* feat: switch worker.terminate to log warning instead of throwing error * Update index.js Co-authored-by: Konstantin Azizov <[email protected]> Co-authored-by: Jason Miller <[email protected]>
1 parent 427eb5c commit 7160ae1

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ global.Worker = function Worker(url) {
5353
let messageQueue = [];
5454
let inside = mitt();
5555
let outside = mitt();
56+
let terminated = false;
5657
let scope = {
5758
onmessage: null,
5859
dispatchEvent: inside.emit,
@@ -65,6 +66,7 @@ global.Worker = function Worker(url) {
6566
importScripts() {}
6667
};
6768
inside.on('message', e => {
69+
if (terminated) return;
6870
let f = scope.onmessage || getScopeVar('onmessage');
6971
if (f) f.call(scope, e);
7072
});
@@ -75,11 +77,14 @@ global.Worker = function Worker(url) {
7577
if (this.onmessage) this.onmessage(e);
7678
});
7779
this.postMessage = data => {
78-
if (messageQueue!=null) messageQueue.push(data);
80+
if (terminated) return;
81+
if (messageQueue != null) messageQueue.push(data);
7982
else inside.emit('message', { data });
8083
};
8184
this.terminate = () => {
82-
throw Error('Not Supported');
85+
console.warn('Worker.prototype.terminate() not supported in jsdom-worker.');
86+
messageQueue = null;
87+
terminated = true;
8388
};
8489
global.fetch(url)
8590
.then(r => r.text())

0 commit comments

Comments
 (0)