From 87bca25a96d3b5f7184ab97c65c86649f062a18d Mon Sep 17 00:00:00 2001 From: tmorin Date: Wed, 3 Nov 2021 23:02:08 +0100 Subject: [PATCH] feat(ceb-messaging-simple): add support for internal event like `error` or `dispose` --- packages/ceb-messaging-simple/src/bus.spec.ts | 2 +- packages/ceb-messaging-simple/src/bus.ts | 22 ++++++++++++++----- .../ceb-messaging-simple/src/inversion.ts | 2 +- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/packages/ceb-messaging-simple/src/bus.spec.ts b/packages/ceb-messaging-simple/src/bus.spec.ts index 0d44f865..2ee797fa 100644 --- a/packages/ceb-messaging-simple/src/bus.spec.ts +++ b/packages/ceb-messaging-simple/src/bus.spec.ts @@ -44,7 +44,7 @@ describe("messaging/simple/bus", function () { bus = new InMemorySimpleBus() }) afterEach(async function () { - await bus?.destroy() + await bus?.dispose() }) describe("action", function () { it("should execute a command and wait for result", async function () { diff --git a/packages/ceb-messaging-simple/src/bus.ts b/packages/ceb-messaging-simple/src/bus.ts index fee1edc5..bc37f8d7 100644 --- a/packages/ceb-messaging-simple/src/bus.ts +++ b/packages/ceb-messaging-simple/src/bus.ts @@ -19,6 +19,7 @@ import { Subscription, SubscriptionListener } from "@tmorin/ceb-messaging-core"; +import {AbstractBus} from "@tmorin/ceb-messaging-core/src"; class HandlerEntry implements Handler { constructor( @@ -98,7 +99,7 @@ function getGlobalBus() { /** * An very simple implementation of a {@link Bus}. */ -export class InMemorySimpleBus implements Bus { +export class InMemorySimpleBus extends AbstractBus implements Bus { /** * A global instance. @@ -106,13 +107,24 @@ export class InMemorySimpleBus implements Bus { public static readonly GLOBAL: InMemorySimpleBus = getGlobalBus() constructor( + /** + * @ignore + */ private readonly handlers: Map = new Map(), - private readonly subscriptions: Map> = new Map() + /** + * @ignore + */ + private readonly subscriptions: Map> = new Map(), ) { + super() } - destroy() { + async dispose(): Promise { + await super.dispose(); this.handlers.forEach(handler => handler.cancel()) + this.handlers.clear() + this.subscriptions.forEach(entries => entries.forEach(entry => entry.unsubscribe())) + this.subscriptions.clear() } subscribe( @@ -128,7 +140,7 @@ export class InMemorySimpleBus implements Bus { try { value.handle(event) } catch (error: any) { - console.error("a subscription failed to handle the event %o", event, error) + this.emit("error", error) } }) } @@ -173,7 +185,7 @@ export class InMemorySimpleBus implements Bus { entry: HandlerEntry, action: A ): Promise { - entry.handle(action).catch(error => console.error("InMemorySimpleBus - the action failed", error)) + entry.handle(action).catch(error => this.emit("error", error)) } } diff --git a/packages/ceb-messaging-simple/src/inversion.ts b/packages/ceb-messaging-simple/src/inversion.ts index 64e761b2..1f4e5102 100644 --- a/packages/ceb-messaging-simple/src/inversion.ts +++ b/packages/ceb-messaging-simple/src/inversion.ts @@ -25,6 +25,6 @@ export class SimpleModule extends AbstractModule { } async dispose(): Promise { - await this.bus.destroy() + await this.bus.dispose() } }