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-dom): add options to the inversion module
Browse files Browse the repository at this point in the history
  • Loading branch information
tmorin committed Nov 5, 2021
1 parent 0e64d24 commit dbae0e0
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions packages/ceb-messaging-dom/src/inversion.ts
@@ -1,7 +1,17 @@
import {AbstractModule} from "@tmorin/ceb-inversion";
import {BusSymbol} from "@tmorin/ceb-messaging-core";
import {AbstractModule, ComponentSymbol} from "@tmorin/ceb-inversion";
import {Bus, BusSymbol} from "@tmorin/ceb-messaging-core";
import {DomBus} from "./bus";

/**
* The options of {@link DomModule}.
*/
export interface DomModuleOptions {
/**
* When `true`, the `error` internal events (i.e. `bus.on("error", ...)`) are displayed using `console.error(...)`.
*/
errorToConsole: boolean
}

/**
* The module registers a {@link Bus} bound with the key {@link BusSymbol}
*
Expand All @@ -17,16 +27,26 @@ export class DomModule extends AbstractModule {
constructor(
private readonly global: EventTarget = window,
private readonly requester: EventTarget = global,
private readonly options: DomModuleOptions = {
errorToConsole: false
},
private readonly bus = new DomBus(global, requester)
) {
super();
}

async configure(): Promise<void> {
this.registry.registerValue(BusSymbol, this.bus)
}

async dispose(): Promise<void> {
await this.bus.dispose()
this.registry.registerFactory(ComponentSymbol, (registry) => ({
configure: async () => {
const bus = registry.resolve<Bus>(BusSymbol)
if (this.options.errorToConsole) {
bus.on("error", error => console.error("DomBus throws an error", error))
}
},
async dispose() {
await registry.resolve<Bus>(BusSymbol).dispose()
}
}))
}
}

0 comments on commit dbae0e0

Please sign in to comment.