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.
This commit is contained in:
chrisgarrity 2018-07-11 08:35:22 -04:00
parent 250b164603
commit 849412ba82
2 changed files with 14 additions and 8 deletions

View file

@ -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 () { refreshBlocks () {
this._loadedExtensions.forEach(serviceName => { const allPromises = Array.from(this._loadedExtensions.values()).map(serviceName =>
dispatch.call(serviceName, 'getInfo') dispatch.call(serviceName, 'getInfo')
.then(info => { .then(info => {
info = this._prepareExtensionInfo(serviceName, info);
dispatch.call('runtime', '_refreshExtensionPrimitives', info); dispatch.call('runtime', '_refreshExtensionPrimitives', info);
}) })
.catch(e => { .catch(e => {
log.error(`Failed to refresh buildtin extension primitives: ${JSON.stringify(e)}`); log.error(`Failed to refresh buildtin extension primitives: ${JSON.stringify(e)}`);
}); })
}); );
return Promise.all(allPromises);
} }
allocateWorker () { allocateWorker () {

View file

@ -907,12 +907,15 @@ class VirtualMachine extends EventEmitter {
* set the current locale and builtin messages for the VM * set the current locale and builtin messages for the VM
* @param {[type]} locale current locale * @param {[type]} locale current locale
* @param {[type]} messages builtin messages map for 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) { setLocale (locale, messages) {
if (locale !== formatMessage.setup().locale) { if (locale === formatMessage.setup().locale) {
formatMessage.setup({locale: locale, translations: {[locale]: messages}}); return Promise.resolve();
this.extensionManager.refreshBlocks();
} }
formatMessage.setup({locale: locale, translations: {[locale]: messages}});
return this.extensionManager.refreshBlocks();
} }
/** /**