mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
make exported class abstract, improve guide
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
328117e24c
commit
df700d6b1d
@ -43,11 +43,11 @@ To register either a handler or a listener, you should do something like the fol
|
||||
`main/extension.ts`:
|
||||
```typescript
|
||||
import { LensMainExtension, Interface, Types, Store } from "@k8slens/extensions";
|
||||
import { registerListeners } from "./helper-file";
|
||||
import { registerListeners, IpcMain } from "./helpers/main";
|
||||
|
||||
export class ExampleExtensionMain extends LensMainExtension {
|
||||
onActivate() {
|
||||
Store.MainIpcStore.createInstance(this);
|
||||
IpcMain.createInstance(this);
|
||||
|
||||
registerListeners();
|
||||
}
|
||||
@ -59,16 +59,18 @@ Lens will automatically clean up that store and all the handlers on deactivation
|
||||
|
||||
---
|
||||
|
||||
`main/helper-file.ts`:
|
||||
`helpers/main.ts`:
|
||||
```typescript
|
||||
import { Store } from "@k8slens/extensions";
|
||||
|
||||
export class IpcMain extends Store.MainIpcStore {}
|
||||
|
||||
function onInitialize(event: Types.IpcMainEvent, id: string) {
|
||||
console.log(`starting to initialize: ${id}`);
|
||||
}
|
||||
|
||||
export function registerListeners() {
|
||||
Store.MainIpcStore.getInstance().listenIpc("initialize", onInitialize);
|
||||
IpcMain.getInstance().listenIpc("initialize", onInitialize);
|
||||
}
|
||||
```
|
||||
|
||||
@ -77,15 +79,16 @@ It should be able to just call `getInstance()` everywhere in your extension as n
|
||||
|
||||
---
|
||||
|
||||
`renderer/extension.ts`:
|
||||
`renderer.ts`:
|
||||
```typescript
|
||||
import { LensRendererExtension, Interface, Types } from "@k8slens/extensions";
|
||||
import { IpcRenderer } from "./helpers/renderer";
|
||||
|
||||
export class ExampleExtensionRenderer extends LensRendererExtension {
|
||||
onActivate() {
|
||||
const ipcStore = Store.RendererIpcStore.createInstance(this);
|
||||
const ipc = IpcRenderer.createInstance(this);
|
||||
|
||||
setTimeout(() => ipcStore.broadcastIpc("initialize", "an-id"), 5000);
|
||||
setTimeout(() => ipc.broadcastIpc("initialize", "an-id"), 5000);
|
||||
}
|
||||
}
|
||||
```
|
||||
@ -94,6 +97,17 @@ It is also needed to create an instance to broadcast messages too.
|
||||
|
||||
---
|
||||
|
||||
`helpers/renderer.ts`:
|
||||
```typescript
|
||||
import { Store } from "@k8slens/extensions";
|
||||
|
||||
export class IpcMain extends Store.RendererIpcStore {}
|
||||
```
|
||||
|
||||
It is necessary to create child classes of these `abstract class`'s in your extension before you can use them.
|
||||
|
||||
---
|
||||
|
||||
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.
|
||||
@ -102,7 +116,7 @@ If you want to register a "handler" you would call `Store.MainIpcStore.handleIpc
|
||||
The cleanup of these handlers is handled by Lens itself.
|
||||
|
||||
`Store.RendererIpcStore.broadcastIpc(...)` and `Store.MainIpcStore.broadcastIpc(...)` sends an event to all renderer frames and to main.
|
||||
Because of this, if you have the same channel name in both `main` and `renderer` both will receive the events.
|
||||
Because of this, no matter where you broadcast from, all listeners in `main` and `renderer` will be notified.
|
||||
|
||||
### Allowed Values
|
||||
|
||||
|
||||
@ -20,5 +20,5 @@
|
||||
*/
|
||||
|
||||
export { ExtensionStore } from "../extension-store";
|
||||
export { MainIpcStore } from "../ipc-main-store";
|
||||
export { RendererIpcStore } from "../ipc-renderer-store";
|
||||
export { MainIpcStore } from "../main-ipc-store";
|
||||
export { RendererIpcStore } from "../renderer-ipc-store";
|
||||
|
||||
@ -3,7 +3,7 @@ import { IpcPrefix, IpcStore } from "./ipc-store";
|
||||
import { Disposers } from "./lens-extension";
|
||||
import { LensMainExtension } from "./lens-main-extension";
|
||||
|
||||
export class MainIpcStore extends IpcStore {
|
||||
export abstract class MainIpcStore extends IpcStore {
|
||||
constructor(extension: LensMainExtension) {
|
||||
super(extension);
|
||||
extension[Disposers].push(() => MainIpcStore.resetInstance());
|
||||
@ -3,7 +3,7 @@ import { IpcPrefix, IpcStore } from "./ipc-store";
|
||||
import { Disposers } from "./lens-extension";
|
||||
import { LensRendererExtension } from "./lens-renderer-extension";
|
||||
|
||||
export class RendererIpcStore extends IpcStore {
|
||||
export abstract class RendererIpcStore extends IpcStore {
|
||||
constructor(extension: LensRendererExtension) {
|
||||
super(extension);
|
||||
extension[Disposers].push(() => RendererIpcStore.resetInstance());
|
||||
Loading…
Reference in New Issue
Block a user