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

Fix auto cleanup of Extension's IPC classes (#3505)

This commit is contained in:
Sebastian Malton 2021-07-23 15:14:11 -04:00 committed by GitHub
parent de6560b427
commit 1437f8a945
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 7 deletions

View File

@ -25,11 +25,14 @@ import type { LensMainExtension } from "../lens-main-extension";
import type { Disposer } from "../../common/utils";
import { once } from "lodash";
import { ipcMainHandle } from "../../common/ipc";
import logger from "../../main/logger";
export abstract class IpcMain extends IpcRegistrar {
constructor(extension: LensMainExtension) {
super(extension);
extension[Disposers].push(() => IpcMain.resetInstance());
// Call the static method on the bottom child class.
extension[Disposers].push(() => (this.constructor as typeof IpcMain).resetInstance());
}
/**
@ -40,8 +43,13 @@ export abstract class IpcMain extends IpcRegistrar {
*/
listen(channel: string, listener: (event: Electron.IpcRendererEvent, ...args: any[]) => any): Disposer {
const prefixedChannel = `extensions@${this[IpcPrefix]}:${channel}`;
const cleanup = once(() => ipcMain.removeListener(prefixedChannel, listener));
const cleanup = once(() => {
logger.info(`[IPC-RENDERER]: removing extension listener`, { channel, extension: { name: this.extension.name, version: this.extension.version } });
return ipcMain.removeListener(prefixedChannel, listener);
});
logger.info(`[IPC-RENDERER]: adding extension listener`, { channel, extension: { name: this.extension.name, version: this.extension.version } });
ipcMain.addListener(prefixedChannel, listener);
this.extension[Disposers].push(cleanup);
@ -56,7 +64,12 @@ export abstract class IpcMain extends IpcRegistrar {
handle(channel: string, handler: (event: Electron.IpcMainInvokeEvent, ...args: any[]) => any): void {
const prefixedChannel = `extensions@${this[IpcPrefix]}:${channel}`;
logger.info(`[IPC-RENDERER]: adding extension handler`, { channel, extension: { name: this.extension.name, version: this.extension.version } });
ipcMainHandle(prefixedChannel, handler);
this.extension[Disposers].push(() => ipcMain.removeHandler(prefixedChannel));
this.extension[Disposers].push(() => {
logger.info(`[IPC-RENDERER]: removing extension handler`, { channel, extension: { name: this.extension.name, version: this.extension.version } });
return ipcMain.removeHandler(prefixedChannel);
});
}
}

View File

@ -28,7 +28,9 @@ import { once } from "lodash";
export abstract class IpcRenderer extends IpcRegistrar {
constructor(extension: LensRendererExtension) {
super(extension);
extension[Disposers].push(() => IpcRenderer.resetInstance());
// Call the static method on the bottom child class.
extension[Disposers].push(() => (this.constructor as typeof IpcRenderer).resetInstance());
}
/**
@ -41,8 +43,13 @@ export abstract class IpcRenderer extends IpcRegistrar {
*/
listen(channel: string, listener: (event: Electron.IpcRendererEvent, ...args: any[]) => any): Disposer {
const prefixedChannel = `extensions@${this[IpcPrefix]}:${channel}`;
const cleanup = once(() => ipcRenderer.removeListener(prefixedChannel, listener));
const cleanup = once(() => {
console.info(`[IPC-RENDERER]: removing extension listener`, { channel, extension: { name: this.extension.name, version: this.extension.version } });
return ipcRenderer.removeListener(prefixedChannel, listener);
});
console.info(`[IPC-RENDERER]: adding extension listener`, { channel, extension: { name: this.extension.name, version: this.extension.version } });
ipcRenderer.addListener(prefixedChannel, listener);
this.extension[Disposers].push(cleanup);

View File

@ -61,7 +61,7 @@ function bindClusterManagerRouteEvents() {
// Handle navigation via IPC
ipcRendererOn(IpcRendererNavigationEvents.NAVIGATE_IN_APP, (event, url: string) => {
logger.info(`[IPC]: ${event.type}: ${url}`, { currentLocation: location.href });
logger.info(`[IPC]: navigate to ${url}`, { currentLocation: location.href });
navigate(url);
});
}
@ -69,7 +69,7 @@ function bindClusterManagerRouteEvents() {
// Handle cluster-view renderer process events within iframes
function bindClusterFrameRouteEvents() {
ipcRendererOn(IpcRendererNavigationEvents.NAVIGATE_IN_CLUSTER, (event, url: string) => {
logger.info(`[IPC]: ${event.type}: ${url}`, { currentLocation: location.href });
logger.info(`[IPC]: navigate to ${url}`, { currentLocation: location.href });
navigate(url);
});
}