mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 14:32:59 -05:00
pass blockInfo
to extension methods
Sometimes a single extension method needs to service several different instances of the same opcode. This can happen with variables or custom procedures, for example. This change allows the extension method to inspect the `blockInfo` for instance data, including arbitrary extension-specific properties if necessary.
This commit is contained in:
parent
9135780c55
commit
638062e982
1 changed files with 13 additions and 14 deletions
|
@ -391,23 +391,22 @@ class ExtensionManager {
|
|||
}
|
||||
break;
|
||||
default:
|
||||
if (!blockInfo.opcode) {
|
||||
throw new Error('Missing opcode for block');
|
||||
}
|
||||
if (blockInfo.opcode) {
|
||||
const funcName = blockInfo.func ? this._sanitizeID(blockInfo.func) : blockInfo.opcode;
|
||||
|
||||
blockInfo.func = blockInfo.func ? this._sanitizeID(blockInfo.func) : blockInfo.opcode;
|
||||
|
||||
// Avoid promise overhead if possible
|
||||
if (dispatch._isRemoteService(serviceName)) {
|
||||
blockInfo.func = dispatch.call.bind(dispatch, serviceName, blockInfo.func);
|
||||
} else {
|
||||
const serviceObject = dispatch.services[serviceName];
|
||||
const func = serviceObject[blockInfo.func];
|
||||
if (func) {
|
||||
blockInfo.func = func.bind(serviceObject);
|
||||
// Avoid promise latency unless necessary
|
||||
if (dispatch._isRemoteService(serviceName)) {
|
||||
blockInfo.func = (args, util) => dispatch.call(serviceName, funcName, args, util, blockInfo);
|
||||
} else {
|
||||
throw new Error(`Could not find extension block function called ${blockInfo.func}`);
|
||||
const serviceObject = dispatch.services[serviceName];
|
||||
if (!serviceObject[funcName]) {
|
||||
// The function might show up later as a dynamic property of the service object
|
||||
log.warn(`Could not find extension block function called ${funcName}`);
|
||||
}
|
||||
blockInfo.func = (args, util) => serviceObject[funcName](args, util, blockInfo);
|
||||
}
|
||||
} else {
|
||||
throw new Error('Missing opcode for block');
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue