Skip to main content
Deno 2 is finally here 🎉️
Learn more

Event Bus Core (Cross-Compile from Deno)

How to Import in Node

import { EventBusRxJs as EventBus } from "@codemonument/event-bus-core";

How to Import in Deno

Note: Use import url with specific version in production to avoid sudden breaking changes.

import { EventBusRxJs as EventBus } from "https://deno.land/x/event_bus_core/mod.ts";

Usage

import { EventBusRxJs as EventBus } from "https://deno.land/x/event_bus_core";

// Create new EventBus instance
const bus = new EventBus();

// Create an empty bus event
export class PlainEvent extends BusEvent<void> {
  public type = "PlainEvent";
}

// Create a bus event with payload
export interface DemoPayload {
  value: number;
}
export class EventWithPayload extends BusEvent<DemoPayload> {
  public type = "EventWithPayload";
}

// Subscribe to events
const sub1 = bus.on$(PlainEvent).subscribe(() => {
  console.log(`Received PlainEvent!`);
});

const sub2 = bus.on$(EventWithPayload).subscribe((payload: DemoPayload) => {
  console.log(`Received EventWithPayload!`, payload);
});


// Emit events 
bus.emit(new PlainEvent());
bus.emit(new EventWithPayload({ value: 10 }));

// Clean up your rxjs subscriptions!
sub1.unsubscribe();
sub2.unsubscribe();

Advanced Usage

You can subscribe to the underlying RxJS Observable of this event bus, called bus.eventStream$. This exposes the raw events to you. Normally you don’t need this and it’s better to use the bus.on$ method which filters to a specific event.

bus.eventStream$.subscribe((event: unknown) => {
  console.log(`Received Event: `, event);
});

Todos in this Repo

  1. Add test for event-bus.rxjs.ts
  2. Add Code from event-bus-nx package for rx version (esp. EventBus Callback Group)
  3. Rewrite tests for deno
  4. Check if anything still works

Considerations

Switching from rxjs to evt

  • pro: no rxjs dependency
  • contra: weird evt dependency typings (cross-compiled to deno from node via Denoify)
  • steps:
    1. Add Evt as dependency in importMap
    2. Rewrite rxjs usages to evt

Switchting to standard Webstreams

  • pro: standardised spec
  • contra: more difficult to have one stream be sent to mutliple listeners and correctly closing all of them

Repo Log

2022-11-14

2022-09-05