diff --git a/src/core/ClearStack.js b/src/core/ClearStack.js index c8b2ed21..a9f645ed 100644 --- a/src/core/ClearStack.js +++ b/src/core/ClearStack.js @@ -25,6 +25,23 @@ getJasmineRequireObj().clearStack = function(j$) { } function messageChannelImpl(global) { + const { setTimeout } = global; + const postMessage = getPostMessage(global); + + let currentCallCount = 0; + return function clearStack(fn) { + currentCallCount++; + + if (currentCallCount < maxInlineCallCount) { + postMessage(fn); + } else { + currentCallCount = 0; + setTimeout(fn); + } + }; + } + + function getPostMessage(global) { const { MessageChannel, setTimeout } = global; const channel = new MessageChannel(); let head = {}; @@ -48,17 +65,9 @@ getJasmineRequireObj().clearStack = function(j$) { } }; - let currentCallCount = 0; - return function clearStack(fn) { - currentCallCount++; - - if (currentCallCount < maxInlineCallCount) { - tail = tail.next = { task: fn }; - channel.port2.postMessage(0); - } else { - currentCallCount = 0; - setTimeout(fn); - } + return function postMessage(fn) { + tail = tail.next = { task: fn }; + channel.port2.postMessage(0); }; }