Skip to content

Commit

Permalink
default extensions (#1573)
Browse files Browse the repository at this point in the history
  • Loading branch information
dragazo authored Dec 10, 2024
1 parent a3f970e commit bddaedd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
15 changes: 6 additions & 9 deletions src/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,24 @@
}

load(Extension) {
// First, check if the extension is supported
const supported = Extension.prototype.isSupported();
if (supported !== true) {
if(typeof supported === 'string'){
if (typeof supported === 'string'){
this.ide.showMessage(`Unable to load extension: ${supported}`);
} else {
this.ide.showMessage(`Unable to load extension.`);
}

return;
}

const extension = new Extension(this.ide); // TODO: Replace the IDE with an official API?
if (this.isLoaded(extension.name)) {
// check if already loaded before instantiating to avoid multiple instances with possible background tasks
if (this.registry.find(ext => ext.constructor.name === Extension.name)) {
console.log(`skipping extension ${Extension.name} (already loaded)`);
return;
}

const extension = new Extension(this.ide); // TODO: Replace the IDE with an official API?

try {
this.validate(extension);
} catch (err) {
Expand Down Expand Up @@ -141,10 +142,6 @@
});
}

isLoaded(name) {
return this.registry.find(ext => ext.name === name);
}

getLabelPart(spec) {
const part = this.registry.flatMap(ext => ext.getLabelParts()).find(part => part.spec === spec);
if (part) {
Expand Down
28 changes: 15 additions & 13 deletions src/gui-ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,21 @@ class UrlParams {
async apply(_ide) {}

async applyInitialFlags(ide) {
const extensions = this.params.get('extensions');
if (extensions) {
try {
const extensionUrls = JSON.parse(decodeURIComponent(extensions));
await Promise.all(extensionUrls.map(url => ide.loadExtension(url)));
} catch (err) {
ide.inform(
'Unable to load extensions',
'The following error occurred while trying to load extensions:\n\n' +
err.message + '\n\n' +
'Perhaps the URL is malformed?'
);
}
const defaultExtensions = this.getParameterFlag('noDefaultExtensions') ? [] : [
'https://extensions.netsblox.org/extensions/BeatBlox/index.js',
];

try {
const explicitExtensions = JSON.parse(decodeURIComponent(this.params.get('extensions') || '[]'));
const allExtensions = [...explicitExtensions, ...defaultExtensions]; // explicit must come before default for proper overriding
await Promise.all(allExtensions.map(url => ide.loadExtension(url)));
} catch (err) {
ide.inform(
'Unable to load extensions',
'The following error occurred while trying to load extensions:\n\n' +
err.message + '\n\n' +
'Perhaps the URL is malformed?'
);
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -3871,7 +3871,17 @@ IDE_Morph.prototype.settingsMenu = function () {
return menu;
};

const LOADED_EXTENSIONS = {};
IDE_Morph.prototype.loadExtension = async function (url) {
const metaName = (url.match(/\/([^/]+)\/index.js/) || [])[1] || (url.match(/\/([^/]+).js/) || [])[1] || '';
if (metaName.length > 0) {
if (LOADED_EXTENSIONS[metaName] !== undefined) {
console.log(`skipping extension ${url} (already loaded from ${LOADED_EXTENSIONS[metaName]})`);
return;
}
LOADED_EXTENSIONS[metaName] = url;
}

if (await this.isTrustedExtension(url)) {
const node = document.createElement('script');
node.setAttribute('src', url);
Expand Down

0 comments on commit bddaedd

Please sign in to comment.