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

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 <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-05-21 13:46:54 -04:00
parent fa87d9edbd
commit 80e6df50f2
5 changed files with 15 additions and 18 deletions

View File

@ -61,11 +61,11 @@ Lens will automatically clean up that store and all the handlers on deactivation
```typescript ```typescript
import { Ipc, Types } from "@k8slens/extensions"; import { Ipc, Types } from "@k8slens/extensions";
export class IpcMain extends Ipc.IpcMain { export class IpcMain extends Ipc.Main {
constructor(extension: LensMainExtension) { constructor(extension: LensMainExtension) {
super(extension); super(extension);
this.listenIpc("initialize", onInitialize); this.listen("initialize", onInitialize);
} }
} }
@ -88,7 +88,7 @@ export class ExampleExtensionRenderer extends LensRendererExtension {
onActivate() { onActivate() {
const ipc = IpcRenderer.createInstance(this); 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 ```typescript
import { Ipc } from "@k8slens/extensions"; 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. 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`. 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. 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. 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. Because of this, no matter where you broadcast from, all listeners in `main` and `renderer` will be notified.
### Allowed Values ### 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 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. 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 a request based call from `renderer`, you should do `const res = await IpcRenderer.getInstance().invoke(<channel>, ...<args>));` instead.
If you are meaning to do an event based call, merely call `broadcastIpc(<channel>, ...<args>)` 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(<channel>, ...<args>));` instead.

View File

@ -19,5 +19,5 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
export { IpcMain } from "../ipc/ipc-main"; export { IpcMain as Main } from "../ipc/ipc-main";
export { IpcRegistrar } from "../ipc/ipc-registrar"; export { IpcRegistrar as Registrar } from "../ipc/ipc-registrar";

View File

@ -37,7 +37,7 @@ export abstract class IpcMain extends IpcRegistrar {
* @param listener The function that will be called with the arguments of the broadcast * @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 * @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 prefixedChannel = `extensions@${this[IpcPrefix]}:${channel}`;
const cleanup = once(() => ipcMain.removeListener(prefixedChannel, listener)); 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 channel The name of the RPC
* @param handler The remote procedure that is called * @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}`; const prefixedChannel = `extensions@${this[IpcPrefix]}:${channel}`;
ipcMain.handle(prefixedChannel, handler); ipcMain.handle(prefixedChannel, handler);

View File

@ -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 channel The channel to broadcast to your whole extension, both `main` and `renderer`
* @param args The arguments passed to all listeners * @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); broadcastMessage(`extensions@${this[IpcPrefix]}:${channel}`, ...args);
} }
} }

View File

@ -39,7 +39,7 @@ export abstract class IpcRenderer extends IpcRegistrar {
* @param listener The function that will be called with the arguments of the broadcast * @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 * @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 prefixedChannel = `extensions@${this[IpcPrefix]}:${channel}`;
const cleanup = once(() => ipcRenderer.removeListener(prefixedChannel, listener)); 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 * @param args The arguments to pass to the RPC
* @returns A promise of the resulting value * @returns A promise of the resulting value
*/ */
invokeRpc(channel: string, ...args: any[]): Promise<any> { invoke(channel: string, ...args: any[]): Promise<any> {
const prefixedChannel = `extensions@${this[IpcPrefix]}:${channel}`; const prefixedChannel = `extensions@${this[IpcPrefix]}:${channel}`;
return ipcRenderer.invoke(prefixedChannel, ...args); return ipcRenderer.invoke(prefixedChannel, ...args);