mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Merge branch 'refactor-ipc' into split-cluster-to-managed-cluster
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
commit
1d910739e2
@ -37,12 +37,12 @@ export class BaseStore<T = any> extends Singleton {
|
|||||||
return path.basename(this.storeConfig.path);
|
return path.basename(this.storeConfig.path);
|
||||||
}
|
}
|
||||||
|
|
||||||
get syncRendererChannel() {
|
protected get syncRendererChannel() {
|
||||||
return `store-sync-renderer:${this.name}`
|
return `store-sync-renderer:${this.path}`
|
||||||
}
|
}
|
||||||
|
|
||||||
get syncMainChannel() {
|
protected get syncMainChannel() {
|
||||||
return `store-sync-main:${this.name}`
|
return `store-sync-main:${this.path}`
|
||||||
}
|
}
|
||||||
|
|
||||||
get path() {
|
get path() {
|
||||||
|
|||||||
3
src/common/cluster-frames.ts
Normal file
3
src/common/cluster-frames.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import { observable } from "mobx"
|
||||||
|
|
||||||
|
export const clusterFrameMap = observable.map<string, number>();
|
||||||
@ -4,6 +4,7 @@ import { appEventBus } from "./event-bus"
|
|||||||
import { ResourceApplier } from "../main/resource-applier";
|
import { ResourceApplier } from "../main/resource-applier";
|
||||||
import { ipcMain } from "electron";
|
import { ipcMain } from "electron";
|
||||||
import { ClusterManager } from "../main/cluster-manager";
|
import { ClusterManager } from "../main/cluster-manager";
|
||||||
|
import { clusterFrameMap } from "./cluster-frames"
|
||||||
|
|
||||||
export const clusterActivateHandler = "cluster:activate"
|
export const clusterActivateHandler = "cluster:activate"
|
||||||
export const clusterSetFrameIdHandler = "cluster:set-frame-id"
|
export const clusterSetFrameIdHandler = "cluster:set-frame-id"
|
||||||
@ -23,7 +24,7 @@ if (ipcMain) {
|
|||||||
handleRequest(clusterSetFrameIdHandler, (event, clusterId: ClusterId, frameId?: number) => {
|
handleRequest(clusterSetFrameIdHandler, (event, clusterId: ClusterId, frameId?: number) => {
|
||||||
const managedCluster = getById(clusterId);
|
const managedCluster = getById(clusterId);
|
||||||
if (managedCluster) {
|
if (managedCluster) {
|
||||||
if (frameId) managedCluster.cluster.frameId = frameId; // save cluster's webFrame.routingId to be able to send push-updates
|
clusterFrameMap.set(managedCluster.cluster.id, frameId)
|
||||||
return managedCluster.cluster.pushState();
|
return managedCluster.cluster.pushState();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -34,7 +35,11 @@ if (ipcMain) {
|
|||||||
|
|
||||||
handleRequest(clusterDisconnectHandler, (event, clusterId: ClusterId) => {
|
handleRequest(clusterDisconnectHandler, (event, clusterId: ClusterId) => {
|
||||||
appEventBus.emit({name: "cluster", action: "stop"});
|
appEventBus.emit({name: "cluster", action: "stop"});
|
||||||
return getById(clusterId)?.disconnect();
|
const managedCluster = getById(clusterId);
|
||||||
|
if (managedCluster) {
|
||||||
|
managedCluster.disconnect();
|
||||||
|
clusterFrameMap.delete(managedCluster.id)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
handleRequest(clusterKubectlApplyAllHandler, (event, clusterId: ClusterId, resources: string[]) => {
|
handleRequest(clusterKubectlApplyAllHandler, (event, clusterId: ClusterId, resources: string[]) => {
|
||||||
|
|||||||
@ -2,8 +2,9 @@
|
|||||||
// https://www.electronjs.org/docs/api/ipc-main
|
// https://www.electronjs.org/docs/api/ipc-main
|
||||||
// https://www.electronjs.org/docs/api/ipc-renderer
|
// https://www.electronjs.org/docs/api/ipc-renderer
|
||||||
|
|
||||||
import { ipcMain, ipcRenderer, webContents, remote } from "electron"
|
import { ipcMain, ipcRenderer, webContents, remote } from "electron";
|
||||||
import logger from "../main/logger";
|
import logger from "../main/logger";
|
||||||
|
import { clusterFrameMap } from "./cluster-frames";
|
||||||
|
|
||||||
export function handleRequest(channel: string, listener: (...args: any[]) => any) {
|
export function handleRequest(channel: string, listener: (...args: any[]) => any) {
|
||||||
ipcMain.handle(channel, listener)
|
ipcMain.handle(channel, listener)
|
||||||
@ -15,11 +16,8 @@ export async function requestMain(channel: string, ...args: any[]) {
|
|||||||
|
|
||||||
async function getSubFrames(): Promise<number[]> {
|
async function getSubFrames(): Promise<number[]> {
|
||||||
const subFrames: number[] = [];
|
const subFrames: number[] = [];
|
||||||
const { clusterStore } = await import("./cluster-store");
|
clusterFrameMap.forEach((frameId, _) => {
|
||||||
clusterStore.clustersList.forEach(cluster => {
|
subFrames.push(frameId)
|
||||||
if (cluster.frameId) {
|
|
||||||
subFrames.push(cluster.frameId)
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
return subFrames;
|
return subFrames;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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,37 +18,11 @@ 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() {
|
|
||||||
if (ipcRenderer) {
|
|
||||||
subscribeToBroadcast("extensions:loaded", (event, extensions: [LensExtensionId, InstalledExtension][]) => {
|
|
||||||
console.log("extensions", extensions)
|
|
||||||
this.isLoaded = true;
|
|
||||||
extensions.forEach(([extId, ext]) => {
|
|
||||||
if (!this.extensions.has(extId)) {
|
|
||||||
this.extensions.set(extId, ext)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
});
|
|
||||||
} 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()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
extensionsStore.manageState(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@computed get userExtensions(): Map<LensExtensionId, InstalledExtension> {
|
@computed get userExtensions(): Map<LensExtensionId, InstalledExtension> {
|
||||||
const extensions = this.extensions.toJS();
|
const extensions = this.extensions.toJS();
|
||||||
extensions.forEach((ext, extId) => {
|
extensions.forEach((ext, extId) => {
|
||||||
@ -62,11 +34,45 @@ export class ExtensionLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
async init(extensions: Map<LensExtensionId, InstalledExtension>) {
|
async init(extensions?: Map<LensExtensionId, InstalledExtension>) {
|
||||||
this.extensions.replace(extensions);
|
if (extensions) {
|
||||||
|
this.extensions.replace(extensions);
|
||||||
|
}
|
||||||
|
if (ipcRenderer) {
|
||||||
|
this.initRenderer()
|
||||||
|
} else {
|
||||||
|
this.initMain()
|
||||||
|
}
|
||||||
|
extensionsStore.manageState(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async initMain() {
|
||||||
this.isLoaded = true;
|
this.isLoaded = true;
|
||||||
this.loadOnMain();
|
this.loadOnMain();
|
||||||
this.broadcastExtensions();
|
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() {
|
loadOnMain() {
|
||||||
@ -156,7 +162,7 @@ export class ExtensionLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
broadcastExtensions() {
|
broadcastExtensions() {
|
||||||
broadcastMessage("extensions:loaded", Array.from(this.toJSON()))
|
broadcastMessage(this.requestExtensionsChannel, Array.from(this.toJSON()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -33,7 +33,6 @@ export interface ClusterState {
|
|||||||
|
|
||||||
export class Cluster implements ClusterModel, ClusterState {
|
export class Cluster implements ClusterModel, ClusterState {
|
||||||
public id: ClusterId;
|
public id: ClusterId;
|
||||||
public frameId: number;
|
|
||||||
public kubeCtl: Kubectl
|
public kubeCtl: Kubectl
|
||||||
public ownerRef: string;
|
public ownerRef: string;
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
import type { ClusterId } from "../common/cluster-store";
|
import type { ClusterId } from "../common/cluster-store";
|
||||||
import { clusterStore } from "../common/cluster-store";
|
|
||||||
import { observable } from "mobx";
|
import { observable } from "mobx";
|
||||||
import { app, BrowserWindow, dialog, shell, webContents } from "electron"
|
import { app, BrowserWindow, dialog, shell, webContents } from "electron"
|
||||||
import windowStateKeeper from "electron-window-state"
|
import windowStateKeeper from "electron-window-state"
|
||||||
@ -8,6 +7,7 @@ import { subscribeToBroadcast } from "../common/ipc"
|
|||||||
import { initMenu } from "./menu";
|
import { initMenu } from "./menu";
|
||||||
import { initTray } from "./tray";
|
import { initTray } from "./tray";
|
||||||
import { Singleton } from "../common/utils";
|
import { Singleton } from "../common/utils";
|
||||||
|
import { clusterFrameMap } from "../common/cluster-frames";
|
||||||
|
|
||||||
export class WindowManager extends Singleton {
|
export class WindowManager extends Singleton {
|
||||||
protected mainWindow: BrowserWindow;
|
protected mainWindow: BrowserWindow;
|
||||||
@ -130,7 +130,7 @@ export class WindowManager extends Singleton {
|
|||||||
}
|
}
|
||||||
|
|
||||||
reload() {
|
reload() {
|
||||||
const frameId = clusterStore.getById(this.activeClusterId)?.frameId;
|
const frameId = clusterFrameMap.get(this.activeClusterId)
|
||||||
if (frameId) {
|
if (frameId) {
|
||||||
this.sendToView({ channel: "renderer:reload", frameId });
|
this.sendToView({ channel: "renderer:reload", frameId });
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -14,6 +14,7 @@ import { clusterStore } from "../common/cluster-store";
|
|||||||
import { i18nStore } from "./i18n";
|
import { i18nStore } from "./i18n";
|
||||||
import { themeStore } from "./theme.store";
|
import { themeStore } from "./theme.store";
|
||||||
import { extensionsStore } from "../extensions/extensions-store";
|
import { extensionsStore } from "../extensions/extensions-store";
|
||||||
|
import { extensionLoader } from "../extensions/extension-loader";
|
||||||
|
|
||||||
type AppComponent = React.ComponentType & {
|
type AppComponent = React.ComponentType & {
|
||||||
init?(): Promise<void>;
|
init?(): Promise<void>;
|
||||||
@ -30,6 +31,8 @@ export async function bootstrap(App: AppComponent) {
|
|||||||
const rootElem = document.getElementById("app")
|
const rootElem = document.getElementById("app")
|
||||||
rootElem.classList.toggle("is-mac", isMac);
|
rootElem.classList.toggle("is-mac", isMac);
|
||||||
|
|
||||||
|
extensionLoader.init()
|
||||||
|
|
||||||
// preload common stores
|
// preload common stores
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
userStore.load(),
|
userStore.load(),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user