Skip to content

Commit

Permalink
Fix condition race in webWorker (#202)
Browse files Browse the repository at this point in the history
* initial fix

* fix hmr 'this' error

* global regexp in replace

* change quote sign
  • Loading branch information
krizzu authored and grabbou committed Jul 31, 2017
1 parent 71c5797 commit 4b69590
Showing 1 changed file with 50 additions and 21 deletions.
71 changes: 50 additions & 21 deletions src/server/assets/debuggerWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

onmessage = (function() {
let visibilityState;

const messageQueue = [];
let shouldQueueMessages = false;

const showVisibilityWarning = (function() {
let hasWarned = false;
return function() {
Expand All @@ -30,19 +34,33 @@ onmessage = (function() {
};
})();

const processEnqueuedMessages = function() {
while (messageQueue.length) {
const messageProcess = messageQueue.shift();
messageProcess();
}
shouldQueueMessages = false;
};

const messageHandlers = {
executeApplicationScript(message, sendReply) {
for (const key in message.inject) {
self[key] = JSON.parse(message.inject[key]);
}

shouldQueueMessages = true;

function evalJS(js) {
try {
eval(js);
eval(
js
.replace(/this\["webpackHotUpdate"\]/g, 'self["webpackHotUpdate"]')
);
} catch (e) {
self.ErrorUtils.reportFatalError(e);
} finally {
self.postMessage({ replyID: message.id });
processEnqueuedMessages();
}
}

Expand All @@ -54,32 +72,43 @@ onmessage = (function() {
};

return function(message) {
if (visibilityState === 'hidden') {
showVisibilityWarning();
}
const processMessage = function() {
if (visibilityState === 'hidden') {
showVisibilityWarning();
}

const obj = message.data;
const obj = message.data;

const sendReply = function(result, error) {
postMessage({ replyID: obj.id, result, error });
};
const sendReply = function(result, error) {
postMessage({ replyID: obj.id, result, error });
};

const handler = messageHandlers[obj.method];
const handler = messageHandlers[obj.method];

// Special cased handlers
if (handler) {
handler(obj, sendReply);
return;
}
// Special cased handlers
if (handler) {
handler(obj, sendReply);
return;
}

// Other methods get called on the bridge
let returnValue = [[], [], [], 0];
try {
if (typeof __fbBatchedBridge === 'object') {
returnValue = __fbBatchedBridge[obj.method].apply(null, obj.arguments);
// Other methods get called on the bridge
let returnValue = [[], [], [], 0];
try {
if (typeof __fbBatchedBridge === 'object') {
returnValue = __fbBatchedBridge[obj.method].apply(
null,
obj.arguments,
);
}
} finally {
sendReply(JSON.stringify(returnValue));
}
} finally {
sendReply(JSON.stringify(returnValue));
};

if (shouldQueueMessages) {
messageQueue.push(processMessage);
} else {
processMessage();
}
};
})();

0 comments on commit 4b69590

Please sign in to comment.