Skip to content

Commit

Permalink
Merge pull request #12822 from codyrancher/remove-unload
Browse files Browse the repository at this point in the history
Removing unload logic from the plugin load logic
  • Loading branch information
codyrancher authored Jan 30, 2025
2 parents 85c31a4 + d1e2fab commit 76d7af2
Showing 1 changed file with 25 additions and 54 deletions.
79 changes: 25 additions & 54 deletions shell/core/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,72 +78,43 @@ export default function(context, inject, vueApp) {
element.id = id;
element.dataset.purpose = 'extension';

// id is `<product>-<version>`.
const oldPlugin = Object.values(plugins).find((p) => id.startsWith(p.name));

let removed = Promise.resolve();

if (oldPlugin) {
// Uninstall existing plugin if there is one. This ensures that last loaded plugin is not always used
// (nav harv1-->harv2-->harv1 and harv2 would be shown)
removed = this.removePlugin(oldPlugin.name).then(() => {
delete window[oldPlugin.id];

delete plugins[oldPlugin.id];

const oldElement = document.getElementById(oldPlugin.id);

oldElement.parentElement.removeChild(oldElement);
});
}

removed.then(() => {
element.onload = () => {
if (!window[id]) {
return reject(new Error('Could not load plugin code'));
}

// Update the timestamp that new plugins were loaded - may be needed
// to update caches when new plugins are loaded
_lastLoaded = new Date().getTime();

// name is the name of the plugin, including the version number
const plugin = new Plugin(id);

plugins[id] = plugin;
element.onload = () => {
if (!window[id]) {
return reject(new Error('Could not load plugin code'));
}

// Initialize the plugin
window[id].default(plugin, this.internal());
// Update the timestamp that new plugins were loaded - may be needed
// to update caches when new plugins are loaded
_lastLoaded = new Date().getTime();

// Uninstall existing plugin if there is one
this.removePlugin(plugin.name); // Removing this causes the plugin to not load on refresh
// name is the name of the plugin, including the version number
const plugin = new Plugin(id);

// Load all of the types etc from the plugin
this.applyPlugin(plugin);
plugins[id] = plugin;

// Add the plugin to the store
store.dispatch('uiplugins/addPlugin', plugin);
// Initialize the plugin
window[id].default(plugin, this.internal());

resolve();
};
// Load all of the types etc from the plugin
this.applyPlugin(plugin);

element.onerror = (e) => {
element.parentElement.removeChild(element);
// Add the plugin to the store
store.dispatch('uiplugins/addPlugin', plugin);

// Massage the error into something useful
const errorMessage = `Failed to load script from '${ e.target.src }'`;
resolve();
};

console.error(errorMessage, e); // eslint-disable-line no-console
reject(new Error(errorMessage)); // This is more useful where it's used
};
element.onerror = (e) => {
element.parentElement.removeChild(element);

document.head.appendChild(element);
}).catch((e) => {
const errorMessage = `Failed to unload old plugin${ oldPlugin?.id }`;
// Massage the error into something useful
const errorMessage = `Failed to load script from '${ e.target.src }'`;

console.error(errorMessage, e); // eslint-disable-line no-console
reject(new Error(errorMessage)); // This is more useful where it's used
});
};

document.head.appendChild(element);
});
},

Expand Down

0 comments on commit 76d7af2

Please sign in to comment.