Added callback to Process

pull/3/merge
Bernat Romagosa 2014-11-17 14:05:13 +01:00
rodzic ea05f7859f
commit f537f62ace
1 zmienionych plików z 10 dodań i 7 usunięć

Wyświetl plik

@ -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);