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:
parent
43e361727f
commit
2d51af6be3
@ -3,12 +3,10 @@ import type { LensMainExtension } from "./lens-main-extension"
|
|||||||
import type { LensRendererExtension } from "./lens-renderer-extension"
|
import type { LensRendererExtension } from "./lens-renderer-extension"
|
||||||
import type { InstalledExtension } from "./extension-manager";
|
import type { InstalledExtension } from "./extension-manager";
|
||||||
import path from "path"
|
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 { action, computed, observable, reaction, toJS, when } from "mobx"
|
||||||
import logger from "../main/logger"
|
import logger from "../main/logger"
|
||||||
import { app, ipcRenderer, remote } from "electron"
|
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 * as registries from "./registries";
|
||||||
import { extensionsStore } from "./extensions-store";
|
import { extensionsStore } from "./extensions-store";
|
||||||
|
|
||||||
@ -20,32 +18,31 @@ export function extensionPackagesRoot() {
|
|||||||
export class ExtensionLoader {
|
export class ExtensionLoader {
|
||||||
protected extensions = observable.map<LensExtensionId, InstalledExtension>();
|
protected extensions = observable.map<LensExtensionId, InstalledExtension>();
|
||||||
protected instances = observable.map<LensExtensionId, LensExtension>();
|
protected instances = observable.map<LensExtensionId, LensExtension>();
|
||||||
|
protected readonly requestExtensionsChannel = "extensions:loaded"
|
||||||
|
|
||||||
@observable isLoaded = false;
|
@observable isLoaded = false;
|
||||||
whenLoaded = when(() => this.isLoaded);
|
whenLoaded = when(() => this.isLoaded);
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
if (ipcRenderer) {
|
if (ipcRenderer) {
|
||||||
subscribeToBroadcast("extensions:loaded", (event, extensions: [LensExtensionId, InstalledExtension][]) => {
|
const extensionListHandler = ( extensions: [LensExtensionId, InstalledExtension][]) => {
|
||||||
console.log("extensions", extensions)
|
|
||||||
this.isLoaded = true;
|
this.isLoaded = true;
|
||||||
extensions.forEach(([extId, ext]) => {
|
extensions.forEach(([extId, ext]) => {
|
||||||
if (!this.extensions.has(extId)) {
|
if (!this.extensions.has(extId)) {
|
||||||
this.extensions.set(extId, ext)
|
this.extensions.set(extId, ext)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
requestMain(this.requestExtensionsChannel).then(extensionListHandler)
|
||||||
|
subscribeToBroadcast(this.requestExtensionsChannel, (event, extensions: [LensExtensionId, InstalledExtension][]) => {
|
||||||
|
extensionListHandler(extensions)
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
reaction(() => this.extensions.toJS(), () => {
|
reaction(() => this.extensions.toJS(), () => {
|
||||||
this.broadcastExtensions()
|
this.broadcastExtensions()
|
||||||
})
|
})
|
||||||
appEventBus.addListener((ev) => {
|
handleRequest(this.requestExtensionsChannel, () => {
|
||||||
if (ev.name === "app" && ev.action === "dom-ready") {
|
return Array.from(this.toJSON())
|
||||||
this.broadcastExtensions()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
reaction(() => clusterStore.connectedClustersList, () => {
|
|
||||||
this.broadcastExtensions()
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
extensionsStore.manageState(this);
|
extensionsStore.manageState(this);
|
||||||
@ -156,7 +153,7 @@ export class ExtensionLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
broadcastExtensions() {
|
broadcastExtensions() {
|
||||||
broadcastMessage("extensions:loaded", Array.from(this.toJSON()))
|
broadcastMessage(this.requestExtensionsChannel, Array.from(this.toJSON()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user