1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

simplify ExtensionLoader

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen 2020-11-19 10:13:10 +02:00
parent 2d51af6be3
commit cdc0af520d
2 changed files with 39 additions and 27 deletions

View File

@ -23,31 +23,6 @@ export class ExtensionLoader {
@observable isLoaded = false;
whenLoaded = when(() => this.isLoaded);
constructor() {
if (ipcRenderer) {
const extensionListHandler = ( extensions: [LensExtensionId, InstalledExtension][]) => {
this.isLoaded = true;
extensions.forEach(([extId, ext]) => {
if (!this.extensions.has(extId)) {
this.extensions.set(extId, ext)
}
})
}
requestMain(this.requestExtensionsChannel).then(extensionListHandler)
subscribeToBroadcast(this.requestExtensionsChannel, (event, extensions: [LensExtensionId, InstalledExtension][]) => {
extensionListHandler(extensions)
});
} else {
reaction(() => this.extensions.toJS(), () => {
this.broadcastExtensions()
})
handleRequest(this.requestExtensionsChannel, () => {
return Array.from(this.toJSON())
})
}
extensionsStore.manageState(this);
}
@computed get userExtensions(): Map<LensExtensionId, InstalledExtension> {
const extensions = this.extensions.toJS();
extensions.forEach((ext, extId) => {
@ -59,11 +34,45 @@ export class ExtensionLoader {
}
@action
async init(extensions: Map<LensExtensionId, InstalledExtension>) {
this.extensions.replace(extensions);
async init(extensions?: Map<LensExtensionId, InstalledExtension>) {
if (extensions) {
this.extensions.replace(extensions);
}
if (ipcRenderer) {
this.initRenderer()
} else {
this.initMain()
}
extensionsStore.manageState(this);
}
protected async initMain() {
this.isLoaded = true;
this.loadOnMain();
this.broadcastExtensions();
reaction(() => this.extensions.toJS(), () => {
this.broadcastExtensions()
})
handleRequest(this.requestExtensionsChannel, () => {
return Array.from(this.toJSON())
})
}
protected async initRenderer() {
const extensionListHandler = ( extensions: [LensExtensionId, InstalledExtension][]) => {
this.isLoaded = true;
extensions.forEach(([extId, ext]) => {
if (!this.extensions.has(extId)) {
this.extensions.set(extId, ext)
}
})
}
requestMain(this.requestExtensionsChannel).then(extensionListHandler)
subscribeToBroadcast(this.requestExtensionsChannel, (event, extensions: [LensExtensionId, InstalledExtension][]) => {
extensionListHandler(extensions)
});
}
loadOnMain() {

View File

@ -14,6 +14,7 @@ import { clusterStore } from "../common/cluster-store";
import { i18nStore } from "./i18n";
import { themeStore } from "./theme.store";
import { extensionsStore } from "../extensions/extensions-store";
import { extensionLoader } from "../extensions/extension-loader";
type AppComponent = React.ComponentType & {
init?(): Promise<void>;
@ -30,6 +31,8 @@ export async function bootstrap(App: AppComponent) {
const rootElem = document.getElementById("app")
rootElem.classList.toggle("is-mac", isMac);
extensionLoader.init()
// preload common stores
await Promise.all([
userStore.load(),