mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-01-09 14:32:07 -05:00
Example that uses promises instead of util.report
This commit is contained in:
parent
8f6a88c095
commit
d72cc55c11
3 changed files with 29 additions and 14 deletions
|
@ -15,7 +15,8 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"htmlparser2": "3.9.0",
|
"htmlparser2": "3.9.0",
|
||||||
"memoizee": "0.3.10"
|
"memoizee": "0.3.10",
|
||||||
|
"promise": "7.1.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "2.7.0",
|
"eslint": "2.7.0",
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
var Promise = require('promise');
|
||||||
|
|
||||||
function Scratch3OperatorsBlocks(runtime) {
|
function Scratch3OperatorsBlocks(runtime) {
|
||||||
/**
|
/**
|
||||||
* The runtime instantiating this block package.
|
* The runtime instantiating this block package.
|
||||||
|
@ -81,10 +83,13 @@ Scratch3OperatorsBlocks.prototype.random = function (args, util) {
|
||||||
// @todo Match Scratch 2.0 implementation with int-truncation.
|
// @todo Match Scratch 2.0 implementation with int-truncation.
|
||||||
// See: http://bit.ly/1Qc0GzC
|
// See: http://bit.ly/1Qc0GzC
|
||||||
util.yieldAndBlock();
|
util.yieldAndBlock();
|
||||||
setTimeout(function() {
|
var examplePromise = new Promise(function(resolve) {
|
||||||
var randomValue = (Math.random() * (args.TO - args.FROM)) + args.FROM;
|
setTimeout(function() {
|
||||||
util.report(randomValue);
|
var res = (Math.random() * (args.TO - args.FROM)) + args.FROM;
|
||||||
}, 1000);
|
resolve(res);
|
||||||
|
}, 1000);
|
||||||
|
});
|
||||||
|
return examplePromise;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = Scratch3OperatorsBlocks;
|
module.exports = Scratch3OperatorsBlocks;
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
var Promise = require('promise');
|
||||||
var Thread = require('./thread');
|
var Thread = require('./thread');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -78,21 +79,29 @@ var execute = function (sequencer, thread) {
|
||||||
done: function() {
|
done: function() {
|
||||||
sequencer.proceedThread(thread);
|
sequencer.proceedThread(thread);
|
||||||
},
|
},
|
||||||
report: function(reportedValue) {
|
|
||||||
if (DEBUG_BLOCK_CALLS) {
|
|
||||||
console.log('Reported: ', reportedValue);
|
|
||||||
console.timeEnd('Yielding reporter evaluation');
|
|
||||||
}
|
|
||||||
thread.pushReportedValue(reportedValue);
|
|
||||||
sequencer.proceedThread(thread);
|
|
||||||
},
|
|
||||||
timeout: thread.addTimeout.bind(thread),
|
timeout: thread.addTimeout.bind(thread),
|
||||||
stackFrame: currentStackFrame.executionContext,
|
stackFrame: currentStackFrame.executionContext,
|
||||||
startSubstack: function (substackNum) {
|
startSubstack: function (substackNum) {
|
||||||
sequencer.stepToSubstack(thread, substackNum);
|
sequencer.stepToSubstack(thread, substackNum);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (thread.status === Thread.STATUS_RUNNING) {
|
|
||||||
|
// Deal with any reported value.
|
||||||
|
// If it's a promise, wait until promise resolves.
|
||||||
|
var isPromise = (
|
||||||
|
primitiveReportedValue &&
|
||||||
|
primitiveReportedValue.then &&
|
||||||
|
typeof primitiveReportedValue.then === 'function'
|
||||||
|
);
|
||||||
|
if (isPromise) {
|
||||||
|
primitiveReportedValue.then(function(resolvedValue) {
|
||||||
|
if (DEBUG_BLOCK_CALLS) {
|
||||||
|
console.log('reporting value: ', resolvedValue);
|
||||||
|
}
|
||||||
|
thread.pushReportedValue(resolvedValue);
|
||||||
|
sequencer.proceedThread(thread);
|
||||||
|
});
|
||||||
|
} else if (thread.status === Thread.STATUS_RUNNING) {
|
||||||
if (DEBUG_BLOCK_CALLS) {
|
if (DEBUG_BLOCK_CALLS) {
|
||||||
console.log('reporting value: ', primitiveReportedValue);
|
console.log('reporting value: ', primitiveReportedValue);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue