From 849412ba8248ce291721a75bbc501419badfecac Mon Sep 17 00:00:00 2001 From: chrisgarrity Date: Wed, 11 Jul 2018 08:35:22 -0400 Subject: [PATCH] Switch setLocale to return a promise Convert refreshBlocks to a function returning a promise, and return that from setLocale. Now GUI can ensure that refreshing the toolbox and workspace only happens after all the blocks have refreshed. --- src/extension-support/extension-manager.js | 13 ++++++++----- src/virtual-machine.js | 9 ++++++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/extension-support/extension-manager.js b/src/extension-support/extension-manager.js index 66235b24a..edbf7ceac 100644 --- a/src/extension-support/extension-manager.js +++ b/src/extension-support/extension-manager.js @@ -144,18 +144,21 @@ class ExtensionManager { } /** - * regenerate blockinfo for any loaded extensions - */ + * Regenerate blockinfo for any loaded extensions + * @returns {Promise} resolved once all the extensions have been reinitialized + */ refreshBlocks () { - this._loadedExtensions.forEach(serviceName => { + const allPromises = Array.from(this._loadedExtensions.values()).map(serviceName => dispatch.call(serviceName, 'getInfo') .then(info => { + info = this._prepareExtensionInfo(serviceName, info); dispatch.call('runtime', '_refreshExtensionPrimitives', info); }) .catch(e => { log.error(`Failed to refresh buildtin extension primitives: ${JSON.stringify(e)}`); - }); - }); + }) + ); + return Promise.all(allPromises); } allocateWorker () { diff --git a/src/virtual-machine.js b/src/virtual-machine.js index e95258fb4..822f06ced 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -907,12 +907,15 @@ class VirtualMachine extends EventEmitter { * set the current locale and builtin messages for the VM * @param {[type]} locale current locale * @param {[type]} messages builtin messages map for current locale + * @returns {Promise} Promise that resolves when all the blocks have been + * updated for a new locale (or empty if locale hasn't changed.) */ setLocale (locale, messages) { - if (locale !== formatMessage.setup().locale) { - formatMessage.setup({locale: locale, translations: {[locale]: messages}}); - this.extensionManager.refreshBlocks(); + if (locale === formatMessage.setup().locale) { + return Promise.resolve(); } + formatMessage.setup({locale: locale, translations: {[locale]: messages}}); + return this.extensionManager.refreshBlocks(); } /**