Separate clear stack and run it after each spec

[finishes #50197985][fix #109418332]
- Fixes #985
- Fixes #945
- Fixes #366
This commit is contained in:
Gregg Van Hove
2016-10-14 09:21:49 -07:00
parent d85d6dd4b8
commit 17c89c69d6
7 changed files with 125 additions and 25 deletions

36
src/core/ClearStack.js Normal file
View File

@@ -0,0 +1,36 @@
getJasmineRequireObj().clearStack = function(j$) {
function messageChannelImpl(global) {
var channel = new global.MessageChannel(),
head = {},
tail = head;
channel.port1.onmessage = function() {
head = head.next;
var task = head.task;
delete head.task;
task();
};
return function clearStack(fn) {
tail = tail.next = { task: fn };
channel.port2.postMessage(0);
};
}
function getClearStack(global) {
if (global && global.process && j$.isFunction_(global.process.nextTick)) {
return global.process.nextTick;
} else if (j$.isFunction_(global.setImmediate)) {
return global.setImmediate;
} else if (!j$.util.isUndefined(global.MessageChannel)) {
return messageChannelImpl(global);
} else if (j$.isFunction_(global.setTimeout)) {
var realSetTimeout = global.setTimeout;
return function clearStack(fn) {
realSetTimeout(fn, 0);
}
}
}
return getClearStack;
};