Break into a setTimeout every once in a while
- Allows the CPU to run other things that used the real `setTimeout` - Fixes #1327 - See #1334 - Fixes jasmine/gulp-jasmine-browser#48
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
getJasmineRequireObj().clearStack = function(j$) {
|
||||
function messageChannelImpl(global) {
|
||||
var maxInlineCallCount = 10;
|
||||
|
||||
function messageChannelImpl(global, setTimeout) {
|
||||
var channel = new global.MessageChannel(),
|
||||
head = {},
|
||||
tail = head;
|
||||
@@ -22,25 +24,43 @@ getJasmineRequireObj().clearStack = function(j$) {
|
||||
}
|
||||
};
|
||||
|
||||
var currentCallCount = 0;
|
||||
return function clearStack(fn) {
|
||||
tail = tail.next = { task: fn };
|
||||
channel.port2.postMessage(0);
|
||||
currentCallCount++;
|
||||
|
||||
if (currentCallCount < maxInlineCallCount) {
|
||||
tail = tail.next = { task: fn };
|
||||
channel.port2.postMessage(0);
|
||||
} else {
|
||||
setTimeout(fn);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function getClearStack(global) {
|
||||
var currentCallCount = 0;
|
||||
var realSetTimeout = global.setTimeout;
|
||||
var setTimeoutImpl = function clearStack(fn) {
|
||||
Function.prototype.apply.apply(realSetTimeout, [global, [fn, 0]]);
|
||||
};
|
||||
|
||||
if (j$.isFunction_(global.setImmediate)) {
|
||||
var realSetImmediate = global.setImmediate;
|
||||
return function(fn) {
|
||||
realSetImmediate(fn);
|
||||
currentCallCount++;
|
||||
|
||||
if (currentCallCount < maxInlineCallCount) {
|
||||
realSetImmediate(fn);
|
||||
} else {
|
||||
currentCallCount = 0;
|
||||
|
||||
setTimeoutImpl(fn);
|
||||
}
|
||||
};
|
||||
} else if (!j$.util.isUndefined(global.MessageChannel)) {
|
||||
return messageChannelImpl(global);
|
||||
return messageChannelImpl(global, setTimeoutImpl);
|
||||
} else {
|
||||
var realSetTimeout = global.setTimeout;
|
||||
return function clearStack(fn) {
|
||||
Function.prototype.apply.apply(realSetTimeout, [global, [fn, 0]]);
|
||||
};
|
||||
return setTimeoutImpl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user