Fix error in plugin loading

This error happens when a plugin is loaded after Gerrit.awaitPluginsLoaded
completes. Gerrit._endpoints.getDetails inits the module and then
Gerrit._endpoints.onNewEndpoint inits the module a second time. This renders
the same endpoint on the screen twice.

Change-Id: I65c7430f6d1350d1287b400a9ea1d6e275f8e8da
This commit is contained in:
Thomas Shafer
2019-02-07 13:50:54 -08:00
parent 520d92157a
commit 465d12d8a0

View File

@@ -29,6 +29,16 @@
type: Map, type: Map,
value() { return new Map(); }, value() { return new Map(); },
}, },
/**
* This map prevents importing the same endpoint twice.
* Without caching, if a plugin is loaded after the loaded plugins
* callback fires, it will be imported twice and appear twice on the page.
* @type {!Map}
*/
_initializedPlugins: {
type: Map,
value() { return new Map(); },
},
}, },
detached() { detached() {
@@ -102,6 +112,9 @@
}, },
_initModule({moduleName, plugin, type, domHook}) { _initModule({moduleName, plugin, type, domHook}) {
if (this._initializedPlugins.get(plugin.name)) {
return;
}
let initPromise; let initPromise;
switch (type) { switch (type) {
case 'decorate': case 'decorate':
@@ -115,6 +128,7 @@
console.warn('Unable to initialize module' + console.warn('Unable to initialize module' +
`${moduleName} from ${plugin.getPluginName()}`); `${moduleName} from ${plugin.getPluginName()}`);
} }
this._initializedPlugins.set(plugin.name, true);
initPromise.then(el => { initPromise.then(el => {
domHook.handleInstanceAttached(el); domHook.handleInstanceAttached(el);
this._domHooks.set(el, domHook); this._domHooks.set(el, domHook);