Skip to content

feat(events): typed domain event bus (closes #2258) - #2317

Merged
github-actions[bot] merged 1 commit into
Smartdevs17:mainfrom
IVANLI2412:fix/issue-2258-event-bus
Nov 24, 2026
Merged

github-actions[bot] merged 1 commit into
Smartdevs17:mainfrom
IVANLI2412:fix/issue-2258-event-bus

Conversation

@IVANLI2412

Copy link
Copy Markdown
Contributor

Closes #2258

Summary

Introduces a typed, zero-dependency in-process domain event bus for lifecycle changes between backend services and background workers.

Added files:

  • backend/src/events/bus.ts — typed event bus with:
    • DomainEvent envelope (id, type, occurredAt, aggregateId, payload) and a DomainEventPayloadMap single source of truth for typed payloads.
    • DomainEventType union covering subscription.created, subscription.cancelled, invoice.paid, household.invite.accepted, and payment.failed, plus concrete event aliases.
    • Typed EventBusError with stable code values for config, event, handler, duplicate, unknown-type and max-handler failures.
    • createEventBus(config?, metrics?, logger?) returning publish, subscribe, unsubscribe, handlers, publishBatch, and clear.
    • Deterministic dispatch: synchronous/async handlers awaited in registration order, in-flight handler snapshots (unsubscribe inside a handler cannot corrupt dispatch), isolated handler failures (one bad handler never blocks the rest), injectable clock for measurable durations.
    • Config validation (maxHandlersPerEvent, strict, clock) and full metrics/logger hooks on every path (publish, deliver, failure, no-handlers, reject, subscribe, unsubscribe, batch, clear).
  • workers/src/handlers/index.ts — default worker handlers wired to the same contracts: welcome-email stub for subscription.created, ledger-credit stub for invoice.paid, plus audit handlers for cancellation, invite acceptance and payment failure. Handlers are pure (in-memory context sinks, no external I/O) and production-shaped. registerDefaultHandlers(bus, context?) registers all five.
  • backend/src/events/__tests__/bus.test.ts — 32 unit assertions covering defaults/validation, dispatch order, async handlers, failure isolation, no-handler publishes, injectable clock, snapshot isolation, malformed/unknown events, subscribe/unsubscribe/clear/duplicate/limit, batching and observability.
  • workers/src/handlers/__tests__/index.test.ts — 8 unit assertions covering handler context isolation, individual handlers, registration through the shared bus, dispatch of all lifecycle types and unsubscription.

Verification

  • Node 26 type-stripping harness importing the real .ts source via file:// URLs: 50 pass, 0 fail.
  • Jest test files validated with a shim runner (relative imports rewritten to file:// absolute URLs, output written as .ts so inline type imports are stripped): 40 pass, 0 fail (32 bus + 8 handlers).
  • Secret scan (tools/check_secrets.py): OK, exit 0.
  • Zero new dependencies, no lockfile changes, no binaries, no orphan scripts/TODOs. Documentation lives in JSDoc in the source files.

Payouts

Base Mainnet (USDC/ETH): 0xC5dD2Ea54FF138bb3f71D3466092eA31eF115868
Solana (USDC/SOL): DN3pfYEvdkoTBP4oaEVvemsr98fZPhDSAbfVfmewRGQ5
PayPal: ivanzenteno3@gmail.com

@github-actions
github-actions Bot merged commit 477aed7 into Smartdevs17:main Nov 24, 2026
7 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce domain events bus for lifecycle changes between services

1 participant