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 09:52:05 +02:00
parent 43e361727f
commit 2d51af6be3

View File

@ -3,12 +3,10 @@ import type { LensMainExtension } from "./lens-main-extension"
import type { LensRendererExtension } from "./lens-renderer-extension"
import type { InstalledExtension } from "./extension-manager";
import path from "path"
import { broadcastMessage, subscribeToBroadcast } from "../common/ipc"
import { broadcastMessage, handleRequest, requestMain, subscribeToBroadcast } from "../common/ipc"
import { action, computed, observable, reaction, toJS, when } from "mobx"
import logger from "../main/logger"
import { app, ipcRenderer, remote } from "electron"
import { appEventBus } from "./core-api/event-bus"
import { clusterStore } from "./core-api/stores"
import * as registries from "./registries";
import { extensionsStore } from "./extensions-store";
@ -20,32 +18,31 @@ export function extensionPackagesRoot() {
export class ExtensionLoader {
protected extensions = observable.map<LensExtensionId, InstalledExtension>();
protected instances = observable.map<LensExtensionId, LensExtension>();
protected readonly requestExtensionsChannel = "extensions:loaded"
@observable isLoaded = false;
whenLoaded = when(() => this.isLoaded);
constructor() {
if (ipcRenderer) {
subscribeToBroadcast("extensions:loaded", (event, extensions: [LensExtensionId, InstalledExtension][]) => {
console.log("extensions", extensions)
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()
})
appEventBus.addListener((ev) => {
if (ev.name === "app" && ev.action === "dom-ready") {
this.broadcastExtensions()
}
})
reaction(() => clusterStore.connectedClustersList, () => {
this.broadcastExtensions()
handleRequest(this.requestExtensionsChannel, () => {
return Array.from(this.toJSON())
})
}
extensionsStore.manageState(this);
@ -156,7 +153,7 @@ export class ExtensionLoader {
}
broadcastExtensions() {
broadcastMessage("extensions:loaded", Array.from(this.toJSON()))
broadcastMessage(this.requestExtensionsChannel, Array.from(this.toJSON()))
}
}