Skip to content

Commit 1c15ca3

Browse files
committed
Remove JSON stringification (not a big enough performance difference to matter, and JSON vs structured clone is an annoying difference) and restore error reporting
1 parent 78c8d6c commit 1c15ca3

File tree

4 files changed

+11
-19
lines changed

4 files changed

+11
-19
lines changed

index.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@ function isPromise(obj) {
1010
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
1111
}
1212

13-
function parseJsonSafely(str) {
14-
try {
15-
return JSON.parse(str);
16-
} catch (e) {
17-
return false;
18-
}
19-
}
20-
2113
function tryCatchFunc(callback, message) {
2214
try {
2315
return {res: callback(message)};
@@ -43,9 +35,9 @@ PromiseWorker.prototype.register = function (cb) {
4335

4436
PromiseWorker.prototype._postMessageBi = function (obj) {
4537
if (this._worker) {
46-
this._worker.postMessage(JSON.stringify(obj));
38+
this._worker.postMessage(obj);
4739
} else {
48-
self.postMessage(JSON.stringify(obj));
40+
self.postMessage(obj);
4941
}
5042
};
5143

@@ -68,12 +60,12 @@ PromiseWorker.prototype.postMessage = function (userMessage) {
6860
PromiseWorker.prototype._postResponse = function (messageId, error, result) {
6961
if (error) {
7062
/* istanbul ignore else */
71-
/*if (typeof console !== 'undefined' && 'error' in console) {
63+
if (typeof console !== 'undefined' && 'error' in console) {
7264
// This is to make errors easier to debug. I think it's important
7365
// enough to just leave here without giving the user an option
7466
// to silence it.
75-
console.error('Worker caught an error:', error);
76-
}*/
67+
console.error('Error when generating response:', error);
68+
}
7769
this._postMessageBi([MSGTYPE_RESPONSE, messageId, {
7870
message: error.message
7971
}]);
@@ -99,8 +91,8 @@ PromiseWorker.prototype._handleQuery = function (messageId, query) {
9991
};
10092

10193
PromiseWorker.prototype._onMessage = function (e) {
102-
var message = parseJsonSafely(e.data);
103-
if (!message) {
94+
var message = e.data;
95+
if (!Array.isArray(message) || message.length < 3 || message.length > 4) {
10496
// Ignore - this message is not for us.
10597
return;
10698
}

test/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ describe('host -> worker', function () {
147147
worker.removeEventListener('error', onError);
148148
}
149149
function onMessage(e) {
150-
if (typeof e.data === 'string') {
150+
if (Array.isArray(e.data)) {
151151
return;
152152
}
153153
cleanup();
@@ -424,7 +424,7 @@ describe('worker -> host', function () {
424424
});
425425

426426
worker.addEventListener('message', function (e) {
427-
if (typeof e.data !== 'string') { // custom message
427+
if (!Array.isArray(e.data)) { // custom message
428428
worker.postMessage(e.data);
429429
}
430430
});

test/worker-echo-custom.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ promiseWorker.register(function (msg) {
77
});
88

99
self.addEventListener('message', function (e) {
10-
if (typeof e.data !== 'string') { // custom message
10+
if (!Array.isArray(e.data)) { // custom message
1111
self.postMessage(e.data);
1212
}
1313
});

test/worker-host-echo-custom.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ promiseWorker.postMessage('ping'),
88

99
new Promise(function (resolve, reject) {
1010
function onMessage(e) {
11-
if (typeof e.data === 'string') {
11+
if (Array.isArray(e.data)) {
1212
return;
1313
}
1414
resolve(e.data);

0 commit comments

Comments
 (0)