From 9ee7b4ee0b60a734f6f4beb7087fc2e766a42d0d Mon Sep 17 00:00:00 2001 From: Steve Gravrock Date: Fri, 16 Jun 2017 09:58:41 -0700 Subject: [PATCH] Don't setTimeout() every time the stack is cleared via MessageChannel() --- lib/jasmine-core/jasmine.js | 1 + spec/core/ClearStackSpec.js | 12 ++++++++++-- src/core/ClearStack.js | 1 + 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 48c00cb0..741077d4 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -1645,6 +1645,7 @@ getJasmineRequireObj().clearStack = function(j$) { tail = tail.next = { task: fn }; channel.port2.postMessage(0); } else { + currentCallCount = 0; setTimeout(fn); } }; diff --git a/spec/core/ClearStackSpec.js b/spec/core/ClearStackSpec.js index 2ea889d3..854d4478 100644 --- a/spec/core/ClearStackSpec.js +++ b/spec/core/ClearStackSpec.js @@ -42,7 +42,11 @@ describe("ClearStack", function() { clearStack(function() { }); expect(setImmediate.calls.count()).toEqual(9); - expect(setTimeout).toHaveBeenCalled(); + expect(setTimeout.calls.count()).toEqual(1); + + clearStack(function() { }); + expect(setImmediate.calls.count()).toEqual(10); + expect(setTimeout.calls.count()).toEqual(1); }); it("uses MessageChannels when available", function() { @@ -89,7 +93,11 @@ describe("ClearStack", function() { clearStack(function() { }); expect(fakeChannel.port2.postMessage.calls.count()).toEqual(9); - expect(setTimeout).toHaveBeenCalled(); + expect(setTimeout.calls.count()).toEqual(1); + + clearStack(function() { }); + expect(fakeChannel.port2.postMessage.calls.count()).toEqual(10); + expect(setTimeout.calls.count()).toEqual(1); }); it("calls setTimeout when onmessage is called recursively", function() { diff --git a/src/core/ClearStack.js b/src/core/ClearStack.js index aeb42b34..df5d8753 100644 --- a/src/core/ClearStack.js +++ b/src/core/ClearStack.js @@ -32,6 +32,7 @@ getJasmineRequireObj().clearStack = function(j$) { tail = tail.next = { task: fn }; channel.port2.postMessage(0); } else { + currentCallCount = 0; setTimeout(fn); } };