This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ceb-messaging-adapter-electron): add an inversion module
- Loading branch information
Showing
7 changed files
with
157 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 31 additions & 22 deletions
53
packages/ceb-messaging-adapter-electron/src/__TEST/main.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,41 @@ | ||
import {ipcMain, webContents} from 'electron' | ||
import {assert} from "chai"; | ||
import {IpcMainBus} from "../bus-main"; | ||
import {InMemorySimpleBus} from "@tmorin/ceb-messaging-simple"; | ||
import {CommandA, CommandB, Converter, FromMainEvent, FromRendererEvent, ResultA, ResultB} from "./fixture"; | ||
|
||
const parent = new InMemorySimpleBus() | ||
const ipcBus = new IpcMainBus(parent, new Converter(), ipcMain) | ||
import {SimpleModule} from "@tmorin/ceb-messaging-simple"; | ||
import {CommandA, CommandB, FromMainEvent, FromRendererEvent, ResultA, ResultB} from "./fixture"; | ||
import {ElectronModule} from "../inversion"; | ||
import {ContainerBuilder} from "@tmorin/ceb-inversion"; | ||
import {Bus, BusSymbol} from "@tmorin/ceb-messaging-core"; | ||
|
||
function log(text: string) { | ||
webContents.getAllWebContents().forEach(webContent => webContent | ||
.send("main-log", `${text}`)) | ||
} | ||
|
||
ipcMain.on("execute-CommandB", async (event) => { | ||
try { | ||
const resultB = await ipcBus.execute(new CommandB("World"), ResultB) | ||
assert.ok(resultB) | ||
assert.equal(resultB.body, "Hello, World!") | ||
event.reply("execute-CommandB-ok") | ||
} catch (error) { | ||
event.reply("execute-CommandB-ko", error) | ||
} | ||
}) | ||
ContainerBuilder.get() | ||
.module(new SimpleModule()) | ||
.module(new ElectronModule()) | ||
.build() | ||
.initialize() | ||
.then(_container => { | ||
const ipcBus = _container.registry.resolve<Bus>(BusSymbol) | ||
|
||
ipcMain.on("execute-CommandB", async (event) => { | ||
try { | ||
const resultB = await ipcBus.execute(new CommandB("World"), ResultB) | ||
assert.ok(resultB) | ||
assert.equal(resultB.body, "Hello, World!") | ||
event.reply("execute-CommandB-ok") | ||
} catch (error) { | ||
event.reply("execute-CommandB-ko", error) | ||
} | ||
}) | ||
|
||
ipcBus.subscribe<FromRendererEvent>(FromRendererEvent.NAME, (message) => { | ||
return ipcBus.publish(new FromMainEvent(`main[ ${message.body} ]`)) | ||
}) | ||
ipcBus.subscribe<FromRendererEvent>(FromRendererEvent, (message) => { | ||
return ipcBus.publish(new FromMainEvent(`main[ ${message.body} ]`)) | ||
}) | ||
|
||
ipcBus.handle<CommandA, ResultA>(CommandA.NAME, ResultA, async (command: CommandA): Promise<ResultA> => { | ||
return ResultA.createFromCommand(command, `Hello, ${command.body}!`) | ||
}) | ||
ipcBus.handle<CommandA, ResultA>(CommandA, ResultA, async (command: CommandA): Promise<ResultA> => { | ||
return ResultA.createFromCommand(command, `Hello, ${command.body}!`) | ||
}) | ||
}) | ||
.catch(error => log(`contaier failed to start: ${error.name} ${error.message}`)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import {AbstractModule, ComponentSymbol} from "@tmorin/ceb-inversion"; | ||
import {Bus, BusSymbol} from "@tmorin/ceb-messaging-core"; | ||
import {ipcMain, ipcRenderer} from "electron"; | ||
import {IpcMessageConverter, IpcMessageConverterSymbol, SimpleIpcMessageConverter} from "./converter"; | ||
import {IpcMainBus, IpcMainBusSymbol} from "./bus-main"; | ||
import {IpcRendererBus, IpcRendererBusSymbol} from "./bus-renderer"; | ||
|
||
/** | ||
* The options of {@link ElectronModule}. | ||
*/ | ||
export interface ElectronModuleOptions { | ||
/** | ||
* When `true`, the `error` internal events (i.e. `bus.on("error", ...)`) are displayed using `console.error(...)`. | ||
*/ | ||
errorToConsole: boolean | ||
} | ||
|
||
/** | ||
* The module registers a {@link IpcMainBus} or a {@link IpcRendererBus} bound with the key {@link BusSymbol} | ||
* | ||
* @example Register the ElectronModule | ||
* ```typescript | ||
* import {ContainerBuilder} from "@tmorin/ceb-inversion" | ||
* import {ElectronModule} from "@tmorin/ceb-messaging-adapter-electron" | ||
* const container = ContainerBuilder.get() | ||
* .module(new ElectronModule()) | ||
* .build() | ||
* ``` | ||
*/ | ||
export class ElectronModule extends AbstractModule { | ||
constructor( | ||
private readonly options: ElectronModuleOptions = { | ||
errorToConsole: false | ||
} | ||
) { | ||
super(); | ||
} | ||
|
||
async configure(): Promise<void> { | ||
this.registry.registerValue(IpcMessageConverterSymbol, new SimpleIpcMessageConverter()) | ||
if (ipcMain) { | ||
this.registry.registerFactory(IpcMainBusSymbol, (registry) => { | ||
const bus = new IpcMainBus( | ||
registry.resolve<Bus>(BusSymbol), | ||
ipcMain, | ||
registry.resolve<IpcMessageConverter>(IpcMessageConverterSymbol) | ||
) | ||
this.registry.registerValue(BusSymbol, bus) | ||
return bus | ||
}) | ||
this.registry.registerFactory(ComponentSymbol, (registry) => ({ | ||
configure: async () => { | ||
const bus = registry.resolve<Bus>(IpcMainBusSymbol) | ||
if (this.options.errorToConsole) { | ||
bus.on("error", error => console.error("IpcMainBus throws an error", error)) | ||
} | ||
}, | ||
async dispose() { | ||
await registry.resolve<Bus>(IpcMainBusSymbol).dispose() | ||
} | ||
})) | ||
} | ||
if (ipcRenderer) { | ||
this.registry.registerFactory(IpcRendererBusSymbol, (registry) => { | ||
const bus = new IpcRendererBus( | ||
registry.resolve<Bus>(BusSymbol), | ||
ipcRenderer, | ||
registry.resolve<IpcMessageConverter>(IpcMessageConverterSymbol) | ||
) | ||
this.registry.registerValue(BusSymbol, bus) | ||
return bus | ||
}) | ||
this.registry.registerFactory(ComponentSymbol, (registry) => ({ | ||
configure: async () => { | ||
const bus = registry.resolve<Bus>(IpcRendererBusSymbol) | ||
if (this.options.errorToConsole) { | ||
bus.on("error", error => console.error("IpcRendererBus throws an error", error)) | ||
} | ||
}, | ||
async dispose() { | ||
await registry.resolve<Bus>(IpcRendererBusSymbol).dispose() | ||
} | ||
})) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters