Skip to content

Commit 51b840e

Browse files
author
Christopher Willis-Ford
committed
move extension info map to Runtime
1 parent c71a2eb commit 51b840e

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

src/engine/blocks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ class Blocks {
551551
const categoryId = block.categoryId ||
552552
(block.mutation && block.mutation.blockInfo && block.mutation.blockInfo.categoryId);
553553
if (categoryId) {
554-
block.extensionInfo = this.runtime.extensionManager.getExtensionInfo(categoryId);
554+
block.extensionInfo = this.runtime.getExtensionInfo(categoryId);
555555
}
556556

557557
// Push block id to scripts array.

src/engine/runtime.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ class Runtime extends EventEmitter {
202202
*/
203203
this._primitives = {};
204204

205+
/**
206+
* Map of loaded extension URLs/IDs to sanitized extension info.
207+
* @type {Map.<string, ExtensionInfo>}
208+
* @private
209+
*/
210+
this._loadedExtensions = new Map();
211+
205212
/**
206213
* Map to look up all block information by extended opcode.
207214
* @type {Array.<CategoryInfo>}
@@ -824,6 +831,36 @@ class Runtime extends EventEmitter {
824831
this.emit(Runtime.EXTENSION_ADDED, categoryInfo);
825832
}
826833

834+
/**
835+
* Check whether an extension is registered or is in the process of loading. This is intended to control loading or
836+
* adding extensions so it may return `true` before the extension is ready to be used. Use the promise returned by
837+
* `loadExtensionURL` if you need to wait until the extension is truly ready.
838+
* @param {string} extensionID - the ID of the extension.
839+
* @returns {boolean} - true if loaded, false otherwise.
840+
*/
841+
isExtensionLoaded (extensionID) {
842+
return this._loadedExtensions.has(extensionID);
843+
}
844+
845+
/**
846+
* Fetch the cached information about an extension. Does not call `getInfo` on the extension
847+
* @see {@link isExtensionLoaded} for details about registration timing.
848+
* @param {string} extensionID - the ID of the extension.
849+
* @returns {ExtensionInfo} - cached information about the extension, or `undefined` if the extension is not loaded.
850+
*/
851+
getExtensionInfo (extensionID) {
852+
return this._loadedExtensions.get(extensionID);
853+
}
854+
855+
/**
856+
* Retrieve the whole map of extension ID to extension information.
857+
* Callers other than the extension manager should probably not use this.
858+
* @returns {Map.<string, ExtensionInfo>} - a map of extension ID to `ExtensionInfo` for each loaded extension.
859+
*/
860+
getLoadedExtensions () {
861+
return this._loadedExtensions;
862+
}
863+
827864
/**
828865
* Reregister the primitives for an extension
829866
* @param {ExtensionMetadata} extensionInfo - new info (results of running getInfo) for an extension

src/extension-support/extension-manager.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,6 @@ class ExtensionManager {
8282
*/
8383
this.pendingWorkers = [];
8484

85-
/**
86-
* Map of loaded extension URLs/IDs to sanitized extension info.
87-
* @type {Map.<string, ExtensionInfo>}
88-
* @private
89-
*/
90-
this._loadedExtensions = new Map();
91-
9285
/**
9386
* Keep a reference to the runtime so we can construct internal extension objects.
9487
* TODO: remove this in favor of extensions accessing the runtime as a service.
@@ -107,19 +100,21 @@ class ExtensionManager {
107100
* `loadExtensionURL` if you need to wait until the extension is truly ready.
108101
* @param {string} extensionID - the ID of the extension.
109102
* @returns {boolean} - true if loaded, false otherwise.
103+
* @deprecated Please use `isExtensionLoaded` on the Runtime instead.
110104
*/
111105
isExtensionLoaded (extensionID) {
112-
return this._loadedExtensions.has(extensionID);
106+
return this.runtime.isExtensionLoaded(extensionID);
113107
}
114108

115109
/**
116110
* Fetch the cached information about an extension. Does not call `getInfo` on the extension
117111
* @see {@link isExtensionLoaded} for details about registration timing.
118112
* @param {string} extensionID - the ID of the extension.
119113
* @return {ExtensionInfo} - cached information about the extension, or `undefined` if the extension is not loaded.
114+
* @deprecated Please use `getExtensionInfo` on the Runtime instead.
120115
*/
121116
getExtensionInfo (extensionID) {
122-
return this._loadedExtensions.get(extensionID);
117+
return this.runtime.getExtensionInfo(extensionID);
123118
}
124119

125120
/**
@@ -133,7 +128,7 @@ class ExtensionManager {
133128
return;
134129
}
135130

136-
if (this.isExtensionLoaded(extensionId)) {
131+
if (this.runtime.isExtensionLoaded(extensionId)) {
137132
const message = `Rejecting attempt to load a second extension with ID ${extensionId}`;
138133
log.warn(message);
139134
return;
@@ -171,16 +166,17 @@ class ExtensionManager {
171166
* @returns {Promise} resolved once all the extensions have been reinitialized
172167
*/
173168
refreshBlocks () {
169+
const loadedExtensions = this.runtime.getLoadedExtensions();
174170
const allPromises = [];
175-
this._loadedExtensions.forEach((extensionInfo, oldId) => {
171+
loadedExtensions.forEach((extensionInfo, oldId) => {
176172
const serviceName = extensionInfo.serviceName;
177173
allPromises.push(dispatch.call(serviceName, 'getInfo')
178174
.then(info => {
179175
info = this._prepareExtensionInfo(serviceName, info);
180-
this._loadedExtensions.set(info.id, info);
176+
loadedExtensions.set(info.id, info);
181177
if (oldId !== info.id) {
182178
log.warn(`Extension changed ID from ${oldId} to ${info.id}!`);
183-
this._loadedExtensions.delete(oldId);
179+
loadedExtensions.delete(oldId);
184180
}
185181
dispatch.call('runtime', '_refreshExtensionPrimitives', info);
186182
})
@@ -244,7 +240,7 @@ class ExtensionManager {
244240
dispatch.setServiceSync(serviceName, extensionObject);
245241
this.registerExtensionServiceSync(serviceName);
246242
const extensionInfo = this._prepareExtensionInfo(serviceName, rawExtensionInfo);
247-
this._loadedExtensions.set(extensionInfo.id, extensionInfo);
243+
this.runtime.getLoadedExtensions().set(extensionInfo.id, extensionInfo);
248244
}
249245

250246
/**

0 commit comments

Comments
 (0)