From 638062e982769553fead0db6cb838a5c1caf7b9c Mon Sep 17 00:00:00 2001 From: Christopher Willis-Ford Date: Mon, 8 Apr 2019 15:20:14 -0700 Subject: [PATCH] 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. --- src/extension-support/extension-manager.js | 27 +++++++++++----------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/extension-support/extension-manager.js b/src/extension-support/extension-manager.js index bf666a28c..e70929b63 100644 --- a/src/extension-support/extension-manager.js +++ b/src/extension-support/extension-manager.js @@ -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; }