Refactor ipc module (#1225)

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen authored and GitHub committed 2020-11-19 11:48:13 +02:00
1 parent c4b98534dc
commit 5aaacb21f9
20 files changed
+224 -242

No files matched your search

+3 -2
View File
@@ -6,7 +6,8 @@ import { ResourceApplier } from "../main/resource-applier"
import { Cluster } from "../main/cluster";
import logger from "../main/logger";
import { app } from "electron"
import { clusterIpc } from "../common/cluster-ipc"
import { requestMain } from "../common/ipc";
import { clusterKubectlApplyAllHandler } from "../common/cluster-ipc";
export interface ClusterFeatureStatus {
currentVersion: string;
@@ -39,7 +40,7 @@ export abstract class ClusterFeature {
if (app) {
await new ResourceApplier(cluster).kubectlApplyAll(resources)
} else {
await clusterIpc.kubectlApplyAll.invokeFromRenderer(cluster.id, resources)
await requestMain(clusterKubectlApplyAllHandler, cluster.id, resources)
}
}
+40 -27
View File
@@ -3,7 +3,7 @@ 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 { broadcastIpc } 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"
@@ -18,24 +18,11 @@ 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) {
ipcRenderer.on("extensions:loaded", (event, extensions: [LensExtensionId, InstalledExtension][]) => {
this.isLoaded = true;
extensions.forEach(([extId, ext]) => {
if (!this.extensions.has(extId)) {
this.extensions.set(extId, ext)
}
})
});
}
extensionsStore.manageState(this);
}
@computed get userExtensions(): Map<LensExtensionId, InstalledExtension> {
const extensions = this.extensions.toJS();
extensions.forEach((ext, extId) => {
@@ -47,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() {
@@ -140,16 +161,8 @@ export class ExtensionLoader {
})
}
async broadcastExtensions(frameId?: number) {
await when(() => this.isLoaded);
broadcastIpc({
channel: "extensions:loaded",
frameId: frameId,
frameOnly: !!frameId,
args: [
Array.from(this.toJSON()),
],
})
broadcastExtensions() {
broadcastMessage(this.requestExtensionsChannel, Array.from(this.toJSON()))
}
}