Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
feat(ceb-messaging-adapter-electron): add support for internal event …
Browse files Browse the repository at this point in the history
…like `error` or `dispose`
  • Loading branch information
tmorin committed Nov 5, 2021
1 parent f6c38b9 commit 777acc7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
28 changes: 25 additions & 3 deletions packages/ceb-messaging-bus-adapter-ipc/src/bus-main.ts
Expand Up @@ -15,7 +15,8 @@ import {
Subscription,
SubscriptionListener
} from "@tmorin/ceb-messaging-core";
import {IpcHandler, IpcMessageConverter, IpcActionError, IpcMessageMetadata, IpcSubscription} from "./bus";
import {IpcActionError, IpcHandler, IpcMessageConverter, IpcMessageMetadata, IpcSubscription} from "./bus";
import {BusEventListener} from "@tmorin/ceb-messaging-core/src";

/**
* The implementation of {@link Bus} for the Main context of Electron IPC.
Expand All @@ -28,6 +29,27 @@ export class IpcMainBus implements Bus {
) {
}

emit(event: string | symbol, ...args: any[]): void {
// @ts-ignore
this.parentBus.emit.apply(this.parentBus, Array.from(arguments))
}

on(event: string | symbol, listener: BusEventListener): this {
// @ts-ignore
this.parentBus.on.apply(this.parentBus, Array.from(arguments))
return this
}

off(event?: string | symbol, listener?: BusEventListener): this {
// @ts-ignore
this.parentBus.off.apply(this.parentBus, Array.from(arguments))
return this
}

async dispose() {
await this.parentBus.dispose()
}

async execute<A extends MessageAction>(action: A, arg1?: any, arg2?: any): Promise<any> {
if (arg1) {
return this.executeAndWait(action, arg1, arg2)
Expand Down Expand Up @@ -65,7 +87,7 @@ export class IpcMainBus implements Bus {
}
} else {
this.parentBus.execute(message)
.catch(error => console.error("IpcMainBus - the action failed", error))
.catch(error => this.emit("error", error))
}
}
this.ipcMain.on(channel, ipcListener)
Expand Down Expand Up @@ -158,7 +180,7 @@ export class IpcMainBus implements Bus {
}))
// forward to parent
return this.parentBus.execute(action)
.catch(error => console.error("IpcMainBus - the action failed", error))
.catch(error => this.emit("error", error))
}

}
26 changes: 24 additions & 2 deletions packages/ceb-messaging-bus-adapter-ipc/src/bus-renderer.ts
Expand Up @@ -16,6 +16,7 @@ import {
SubscriptionListener
} from "@tmorin/ceb-messaging-core";
import {IpcHandler, IpcMessageConverter, IpcActionError, IpcMessageMetadata, IpcSubscription} from "./bus";
import {BusEventListener} from "@tmorin/ceb-messaging-core/src";

/**
* The implementation of {@link Bus} for the Renderer contexts of Electron IPC.
Expand All @@ -28,6 +29,27 @@ export class IpcRendererBus implements Bus {
) {
}

emit(event: string | symbol, ...args: any[]): void {
// @ts-ignore
this.parentBus.emit.apply(this.parentBus, Array.from(arguments))
}

on(event: string | symbol, listener: BusEventListener): this {
// @ts-ignore
this.parentBus.on.apply(this.parentBus, Array.from(arguments))
return this
}

off(event?: string | symbol, listener?: BusEventListener): this {
// @ts-ignore
this.parentBus.off.apply(this.parentBus, Array.from(arguments))
return this
}

async dispose() {
await this.parentBus.dispose()
}

async execute<A extends MessageAction>(action: A, arg1?: any, arg2?: any): Promise<any> {
if (arg1) {
return this.executeAndWait(action, arg1, arg2)
Expand Down Expand Up @@ -65,7 +87,7 @@ export class IpcRendererBus implements Bus {
}
} else {
this.parentBus.execute(message)
.catch(error => console.error("IpcRendererBus - the action failed", error))
.catch(error => this.emit("error", error))
}
}
this.ipcRenderer.on(channel, ipcListener)
Expand Down Expand Up @@ -148,6 +170,6 @@ export class IpcRendererBus implements Bus {
this.ipcRenderer.send(channel, data, {...metadata, waitForResult: false})
// forward to parent
return this.parentBus.execute(action)
.catch(error => console.error("IpcMainBus - the action failed", error))
.catch(error => this.emit("error", error))
}
}

0 comments on commit 777acc7

Please sign in to comment.