From 88763012e4fe5342eda32019644a8cd36b1c3d46 Mon Sep 17 00:00:00 2001 From: Steve Gravrock Date: Wed, 10 May 2017 10:14:47 -0700 Subject: [PATCH] Unified async and async queueable function running code --- src/core/QueueRunner.js | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/core/QueueRunner.js b/src/core/QueueRunner.js index 8cafd26f..d986c993 100644 --- a/src/core/QueueRunner.js +++ b/src/core/QueueRunner.js @@ -40,11 +40,10 @@ getJasmineRequireObj().QueueRunner = function(j$) { for(iterativeIndex = recursiveIndex; iterativeIndex < length; iterativeIndex++) { var queueableFn = queueableFns[iterativeIndex]; - if (queueableFn.fn.length > 0) { - attemptAsync(queueableFn); + var completedSynchronously = attempt(queueableFn); + + if (!completedSynchronously) { return; - } else { - attemptSync(queueableFn); } } @@ -53,15 +52,7 @@ getJasmineRequireObj().QueueRunner = function(j$) { self.onComplete(); }); - function attemptSync(queueableFn) { - try { - queueableFn.fn.call(self.userContext); - } catch (e) { - handleException(e, queueableFn); - } - } - - function attemptAsync(queueableFn) { + function attempt(queueableFn) { var clearTimeout = function () { Function.prototype.apply.apply(self.timeout.clearTimeout, [j$.getGlobal(), [timeoutId]]); }, @@ -69,9 +60,12 @@ getJasmineRequireObj().QueueRunner = function(j$) { onException(error); next(); }, - next = once(function () { + cleanup = once(function() { clearTimeout(timeoutId); self.globalErrors.popListener(handleError); + }), + next = once(function () { + cleanup(); self.run(queueableFns, iterativeIndex + 1); }), timeoutId; @@ -92,11 +86,18 @@ getJasmineRequireObj().QueueRunner = function(j$) { } try { - queueableFn.fn.call(self.userContext, next); + if (queueableFn.fn.length === 0) { + queueableFn.fn.call(self.userContext); + } else { + queueableFn.fn.call(self.userContext, next); + return false; + } } catch (e) { handleException(e, queueableFn); - next(); } + + cleanup(); + return true; } function onException(e) {