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:
Christopher Willis-Ford 2019-04-08 15:20:14 -07:00
parent 9135780c55
commit 638062e982

View file

@ -391,23 +391,22 @@ class ExtensionManager {
} }
break; break;
default: default:
if (!blockInfo.opcode) { if (blockInfo.opcode) {
throw new Error('Missing opcode for block'); const funcName = blockInfo.func ? this._sanitizeID(blockInfo.func) : blockInfo.opcode;
}
blockInfo.func = blockInfo.func ? this._sanitizeID(blockInfo.func) : blockInfo.opcode; // Avoid promise latency unless necessary
if (dispatch._isRemoteService(serviceName)) {
// Avoid promise overhead if possible blockInfo.func = (args, util) => dispatch.call(serviceName, funcName, args, util, blockInfo);
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);
} else { } 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; break;
} }