From f537f62ace4092e1f46d0ca54577ea8810e7510a Mon Sep 17 00:00:00 2001 From: Bernat Romagosa Date: Mon, 17 Nov 2014 14:05:13 +0100 Subject: [PATCH] Added callback to Process --- threads.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/threads.js b/threads.js index cf97362c..4592e923 100644 --- a/threads.js +++ b/threads.js @@ -136,7 +136,8 @@ ThreadManager.prototype.toggleProcess = function (block) { ThreadManager.prototype.startProcess = function ( block, isThreadSafe, - exportResult + exportResult, + callback ) { var active = this.findProcess(block), top = block.topBlock(), @@ -149,7 +150,7 @@ ThreadManager.prototype.startProcess = function ( this.removeTerminatedProcesses(); } top.addHighlight(); - newProc = new Process(block.topBlock()); + newProc = new Process(block.topBlock(), callback); newProc.exportResult = exportResult; this.processes.push(newProc); return newProc; @@ -314,6 +315,7 @@ ThreadManager.prototype.findProcess = function (block) { and when the process was paused exportResult boolean flag indicating whether a picture of the top block along with the result bubble shoud be exported + callback a function to be executed when the process is done */ Process.prototype = {}; @@ -321,7 +323,7 @@ Process.prototype.contructor = Process; Process.prototype.timeout = 500; // msecs after which to force yield Process.prototype.isCatchingErrors = true; -function Process(topBlock) { +function Process(topBlock, callback) { this.topBlock = topBlock || null; this.readyToYield = false; @@ -338,6 +340,7 @@ function Process(topBlock) { this.pauseOffset = null; this.frameCount = 0; this.exportResult = false; + this.callback = callback; if (topBlock) { this.homeContext.receiver = topBlock.receiver(); @@ -435,8 +438,8 @@ Process.prototype.pauseStep = function () { Process.prototype.evaluateContext = function () { var exp = this.context.expression; - this.frameCount += 1; + if (exp instanceof Array) { return this.evaluateSequence(exp); } @@ -476,9 +479,9 @@ Process.prototype.evaluateBlock = function (block, argCount) { } if (this.isCatchingErrors) { try { - this.returnValueToParentContext( - rcvr[block.selector].apply(rcvr, inputs) - ); + var result = rcvr[block.selector].apply(rcvr, inputs); + if (this.callback) { this.callback(result) }; + this.returnValueToParentContext(result); this.popContext(); } catch (error) { this.handleError(error, block);