Skip to content

Commit 5920fad

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

File tree

4 files changed

+47
-25
lines changed

4 files changed

+47
-25
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: 8 additions & 23 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,10 @@ 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);
113-
}
114-
115-
/**
116-
* Fetch the cached information about an extension. Does not call `getInfo` on the extension
117-
* @see {@link isExtensionLoaded} for details about registration timing.
118-
* @param {string} extensionID - the ID of the extension.
119-
* @return {ExtensionInfo} - cached information about the extension, or `undefined` if the extension is not loaded.
120-
*/
121-
getExtensionInfo (extensionID) {
122-
return this._loadedExtensions.get(extensionID);
106+
return this.runtime.isExtensionLoaded(extensionID);
123107
}
124108

125109
/**
@@ -133,7 +117,7 @@ class ExtensionManager {
133117
return;
134118
}
135119

136-
if (this.isExtensionLoaded(extensionId)) {
120+
if (this.runtime.isExtensionLoaded(extensionId)) {
137121
const message = `Rejecting attempt to load a second extension with ID ${extensionId}`;
138122
log.warn(message);
139123
return;
@@ -171,16 +155,17 @@ class ExtensionManager {
171155
* @returns {Promise} resolved once all the extensions have been reinitialized
172156
*/
173157
refreshBlocks () {
158+
const loadedExtensions = this.runtime.getLoadedExtensions();
174159
const allPromises = [];
175-
this._loadedExtensions.forEach((extensionInfo, oldId) => {
160+
loadedExtensions.forEach((extensionInfo, oldId) => {
176161
const serviceName = extensionInfo.serviceName;
177162
allPromises.push(dispatch.call(serviceName, 'getInfo')
178163
.then(info => {
179164
info = this._prepareExtensionInfo(serviceName, info);
180-
this._loadedExtensions.set(info.id, info);
165+
loadedExtensions.set(info.id, info);
181166
if (oldId !== info.id) {
182167
log.warn(`Extension changed ID from ${oldId} to ${info.id}!`);
183-
this._loadedExtensions.delete(oldId);
168+
loadedExtensions.delete(oldId);
184169
}
185170
dispatch.call('runtime', '_refreshExtensionPrimitives', info);
186171
})
@@ -244,7 +229,7 @@ class ExtensionManager {
244229
dispatch.setServiceSync(serviceName, extensionObject);
245230
this.registerExtensionServiceSync(serviceName);
246231
const extensionInfo = this._prepareExtensionInfo(serviceName, rawExtensionInfo);
247-
this._loadedExtensions.set(extensionInfo.id, extensionInfo);
232+
this.runtime.getLoadedExtensions().set(extensionInfo.id, extensionInfo);
248233
}
249234

250235
/**

src/serialization/sb3.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ const deserializeBlocks = function (runtime, blocks) {
853853
}
854854
const block = blocks[blockId];
855855
const {extensionId, extendedOpcode} = decodeBlock(block);
856-
const extensionInfo = runtime.extensionManager.getExtensionInfo(extensionId);
856+
const extensionInfo = runtime.getExtensionInfo(extensionId);
857857
const customDeserialize = getSerializationInfo(extensionInfo, extendedOpcode).deserialize;
858858
if (customDeserialize) {
859859
const newBlock = customDeserialize(block);

0 commit comments

Comments
 (0)