From 80e6df50f29db593163621c462735ba1ca777bdb Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Fri, 21 May 2021 13:46:54 -0400 Subject: [PATCH] Review comment changes - Remove Rpc and Ipc suffixes from methods - Remove Ipc prefix from exported class names - Fixed up docs to be correct Signed-off-by: Sebastian Malton --- docs/extensions/guides/ipc.md | 19 ++++++++----------- src/extensions/core-api/ipc.ts | 4 ++-- src/extensions/ipc/ipc-main.ts | 4 ++-- src/extensions/ipc/ipc-registrar.ts | 2 +- src/extensions/ipc/ipc-renderer.ts | 4 ++-- 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/docs/extensions/guides/ipc.md b/docs/extensions/guides/ipc.md index 6e81f87901..fc919ca02f 100644 --- a/docs/extensions/guides/ipc.md +++ b/docs/extensions/guides/ipc.md @@ -61,11 +61,11 @@ Lens will automatically clean up that store and all the handlers on deactivation ```typescript import { Ipc, Types } from "@k8slens/extensions"; -export class IpcMain extends Ipc.IpcMain { +export class IpcMain extends Ipc.Main { constructor(extension: LensMainExtension) { super(extension); - this.listenIpc("initialize", onInitialize); + this.listen("initialize", onInitialize); } } @@ -88,7 +88,7 @@ export class ExampleExtensionRenderer extends LensRendererExtension { onActivate() { const ipc = IpcRenderer.createInstance(this); - setTimeout(() => ipc.broadcastIpc("initialize", "an-id"), 5000); + setTimeout(() => ipc.broadcast("initialize", "an-id"), 5000); } } ``` @@ -101,7 +101,7 @@ It is also needed to create an instance to broadcast messages too. ```typescript import { Ipc } from "@k8slens/extensions"; -export class IpcRenderer extends Ipc.IpcRenderer {} +export class IpcRenderer extends Ipc.Renderer {} ``` It is necessary to create child classes of these `abstract class`'s in your extension before you can use them. @@ -112,10 +112,10 @@ As this example shows: the channel names *must* be the same. It should also be noted that "listeners" and "handlers" are specific to either `LensRendererExtension` and `LensMainExtension`. There is no behind the scenes transfer of these functions. -If you want to register a "handler" you would call `Ipc.IpcMain.handleRpc(...)` instead. +If you want to register a "handler" you would call `IpcMain.getInstance().handle(...)` instead. The cleanup of these handlers is handled by Lens itself. -`Ipc.IpcRenderer.broadcastIpc(...)` and `Ipc.IpcMain.broadcastIpc(...)` sends an event to all renderer frames and to main. +Calling either `IpcRenderer.getInstance().broadcast(...)` or `IpcMain.getInstance().broadcast(...)` sends an event to all renderer frames and to main. Because of this, no matter where you broadcast from, all listeners in `main` and `renderer` will be notified. ### Allowed Values @@ -123,9 +123,6 @@ Because of this, no matter where you broadcast from, all listeners in `main` and This IPC mechanism utilizes the [Structured Clone Algorithm](developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm) for serialization. This means that more types than what are JSON serializable can be used, but not all the information will be passed through. -## Using IPC +## Using RPC -Calling IPC is very simple. -If you are meaning to do an event based call, merely call `broadcastIpc(, ...)` from within your extension. - -If you are meaning to do a request based call from `renderer`, you should do `const res = await Ipc.IpcRenderer.invokeRpc(, ...));` instead. +If you are meaning to do a request based call from `renderer`, you should do `const res = await IpcRenderer.getInstance().invoke(, ...));` instead. diff --git a/src/extensions/core-api/ipc.ts b/src/extensions/core-api/ipc.ts index 3f967e3bb0..7654fa7f27 100644 --- a/src/extensions/core-api/ipc.ts +++ b/src/extensions/core-api/ipc.ts @@ -19,5 +19,5 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -export { IpcMain } from "../ipc/ipc-main"; -export { IpcRegistrar } from "../ipc/ipc-registrar"; +export { IpcMain as Main } from "../ipc/ipc-main"; +export { IpcRegistrar as Registrar } from "../ipc/ipc-registrar"; diff --git a/src/extensions/ipc/ipc-main.ts b/src/extensions/ipc/ipc-main.ts index c77709501b..094ed48a1c 100644 --- a/src/extensions/ipc/ipc-main.ts +++ b/src/extensions/ipc/ipc-main.ts @@ -37,7 +37,7 @@ export abstract class IpcMain extends IpcRegistrar { * @param listener The function that will be called with the arguments of the broadcast * @returns An optional disopser, Lens will cleanup when the extension is disabled or uninstalled even if this is not called */ - listenIpc(channel: string, listener: (event: Electron.IpcRendererEvent, ...args: any[]) => any): Disposer { + listen(channel: string, listener: (event: Electron.IpcRendererEvent, ...args: any[]) => any): Disposer { const prefixedChannel = `extensions@${this[IpcPrefix]}:${channel}`; const cleanup = once(() => ipcMain.removeListener(prefixedChannel, listener)); @@ -52,7 +52,7 @@ export abstract class IpcMain extends IpcRegistrar { * @param channel The name of the RPC * @param handler The remote procedure that is called */ - handleRpc(channel: string, handler: (event: Electron.IpcMainInvokeEvent, ...args: any[]) => any): void { + handle(channel: string, handler: (event: Electron.IpcMainInvokeEvent, ...args: any[]) => any): void { const prefixedChannel = `extensions@${this[IpcPrefix]}:${channel}`; ipcMain.handle(prefixedChannel, handler); diff --git a/src/extensions/ipc/ipc-registrar.ts b/src/extensions/ipc/ipc-registrar.ts index f80030ddca..84d713ace7 100644 --- a/src/extensions/ipc/ipc-registrar.ts +++ b/src/extensions/ipc/ipc-registrar.ts @@ -38,7 +38,7 @@ export abstract class IpcRegistrar extends Singleton { * @param channel The channel to broadcast to your whole extension, both `main` and `renderer` * @param args The arguments passed to all listeners */ - broadcastIpc(channel: string, ...args: any[]): void { + broadcast(channel: string, ...args: any[]): void { broadcastMessage(`extensions@${this[IpcPrefix]}:${channel}`, ...args); } } diff --git a/src/extensions/ipc/ipc-renderer.ts b/src/extensions/ipc/ipc-renderer.ts index 87c552a76d..59a42f85a0 100644 --- a/src/extensions/ipc/ipc-renderer.ts +++ b/src/extensions/ipc/ipc-renderer.ts @@ -39,7 +39,7 @@ export abstract class IpcRenderer extends IpcRegistrar { * @param listener The function that will be called with the arguments of the broadcast * @returns An optional disopser, Lens will cleanup even if this is not called */ - listenIpc(channel: string, listener: (event: Electron.IpcRendererEvent, ...args: any[]) => any): Disposer { + listen(channel: string, listener: (event: Electron.IpcRendererEvent, ...args: any[]) => any): Disposer { const prefixedChannel = `extensions@${this[IpcPrefix]}:${channel}`; const cleanup = once(() => ipcRenderer.removeListener(prefixedChannel, listener)); @@ -57,7 +57,7 @@ export abstract class IpcRenderer extends IpcRegistrar { * @param args The arguments to pass to the RPC * @returns A promise of the resulting value */ - invokeRpc(channel: string, ...args: any[]): Promise { + invoke(channel: string, ...args: any[]): Promise { const prefixedChannel = `extensions@${this[IpcPrefix]}:${channel}`; return ipcRenderer.invoke(prefixedChannel, ...args);