From eb4671452ef55378298f3c45d2904961b2a6f4f3 Mon Sep 17 00:00:00 2001 From: Gregg Van Hove Date: Thu, 15 Jun 2017 14:33:23 -0700 Subject: [PATCH] Don't use `window` --- lib/jasmine-core/jasmine.js | 38 ++++++++++++++++++++++++-------- spec/core/integration/EnvSpec.js | 2 +- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index cad6fdbe..b5b8e9ed 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -1600,7 +1600,9 @@ getJasmineRequireObj().CallTracker = function(j$) { }; getJasmineRequireObj().clearStack = function(j$) { - function messageChannelImpl(global) { + var maxInlineCallCount = 10; + + function messageChannelImpl(global, setTimeout) { var channel = new global.MessageChannel(), head = {}, tail = head; @@ -1623,25 +1625,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; } } diff --git a/spec/core/integration/EnvSpec.js b/spec/core/integration/EnvSpec.js index f4040905..f14df663 100644 --- a/spec/core/integration/EnvSpec.js +++ b/spec/core/integration/EnvSpec.js @@ -954,7 +954,7 @@ describe("Env integration", function() { describe("with a mock clock", function() { beforeEach(function() { this.originalTimeout = jasmineUnderTest.DEFAULT_TIMEOUT_INTERVAL; - this.realSetTimeout = window.setTimeout; + this.realSetTimeout = setTimeout; jasmine.clock().install(); });