Skip to content
This repository has been archived by the owner on Mar 20, 2021. It is now read-only.

Add sequence ID to emit calls to prevent race #32

Merged
merged 2 commits into from
Mar 17, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = exports = function(options) {
context = Contextify.createContext({}),
window = context.getGlobal(),
_id = windowId++,
requestId = 0,
$;

_.extend(window, {
Expand All @@ -41,7 +42,14 @@ module.exports = exports = function(options) {
emit: function(after) {
function checkComplete() {
if (isComplete()) {
setImmediate(emit);
var associatedRequestId = requestId;
setImmediate(function() {
// Avoid a race condtion where pooled requests may come in while we still have
// pending emit calls. This will generally only happen for very chatty emit callers.
if (requestId === associatedRequestId) {
emit();
}
});
}
}
function isComplete() {
Expand Down Expand Up @@ -242,6 +250,7 @@ module.exports = exports = function(options) {
},

navigate: function(path, _callback) {
requestId++;
window._start = start = process.hrtime();
callback = _callback;
dom.location(window, protocol + '//' + host + path, redirect);
Expand Down