Clear the stack if onmessage is called before the previous invocation finishes
This commit is contained in:
@@ -37,6 +37,27 @@ describe("ClearStack", function() {
|
|||||||
expect(called).toBe(true);
|
expect(called).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("calls setTimeout when onmessage is called recursively", function() {
|
||||||
|
var fakeChannel = {
|
||||||
|
port1: {},
|
||||||
|
port2: { postMessage: function() { fakeChannel.port1.onmessage(); } }
|
||||||
|
},
|
||||||
|
setTimeout = jasmine.createSpy('setTimeout'),
|
||||||
|
global = {
|
||||||
|
MessageChannel: function() { return fakeChannel; },
|
||||||
|
setTimeout: setTimeout,
|
||||||
|
},
|
||||||
|
clearStack = jasmineUnderTest.getClearStack(global),
|
||||||
|
fn = jasmine.createSpy("second clearStack function");
|
||||||
|
|
||||||
|
clearStack(function() {
|
||||||
|
clearStack(fn);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(fn).not.toHaveBeenCalled();
|
||||||
|
expect(setTimeout).toHaveBeenCalledWith(fn, 0);
|
||||||
|
});
|
||||||
|
|
||||||
it("falls back to setTimeout", function() {
|
it("falls back to setTimeout", function() {
|
||||||
var setTimeout = jasmine.createSpy('setTimeout').and.callFake(function(fn) { fn() }),
|
var setTimeout = jasmine.createSpy('setTimeout').and.callFake(function(fn) { fn() }),
|
||||||
global = { setTimeout: setTimeout },
|
global = { setTimeout: setTimeout },
|
||||||
|
|||||||
@@ -4,11 +4,22 @@ getJasmineRequireObj().clearStack = function(j$) {
|
|||||||
head = {},
|
head = {},
|
||||||
tail = head;
|
tail = head;
|
||||||
|
|
||||||
|
var taskRunning = false;
|
||||||
channel.port1.onmessage = function() {
|
channel.port1.onmessage = function() {
|
||||||
head = head.next;
|
head = head.next;
|
||||||
var task = head.task;
|
var task = head.task;
|
||||||
delete head.task;
|
delete head.task;
|
||||||
task();
|
|
||||||
|
if (taskRunning) {
|
||||||
|
global.setTimeout(task, 0);
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
taskRunning = true;
|
||||||
|
task();
|
||||||
|
} finally {
|
||||||
|
taskRunning = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return function clearStack(fn) {
|
return function clearStack(fn) {
|
||||||
|
|||||||
Reference in New Issue
Block a user