@twilio/replay-event-emitter
TypeScript icon, indicating that this package has built-in type declarations

0.3.7 • Public • Published

EventReplayEmitter

An extension to Node's EventEmitter that supports event replay. In other words, if a listener is added using either addListenerWithReplay or onWithReplay, then it will instantly trigger the listener with previous event data (if no data was previously emitted, then the listener will not get triggered on registration).

Usage Examples

In order to get event replay on listener registration, simply utilize the addListenerWithReplay method like so:

import { ReplayEventEmitter } from "replay-event-emitter";

// Instantiate the event emitter
const eventEmitter = new ReplayEventEmitter();

// Emit an event before adding a listener
eventEmitter.emit("exampleEvent", "someData");

// Subscribe using addListenerWithReplay
eventEmitter.addListenerWithReplay("exampleEvent", (data) => {
  // This handler will get triggered despite the data emission happening BEFORE
  // the event registration.
  console.log(data);
});

You could also use the shorthand method onWithRepeat, like so:

eventEmitter.onWithReplay("exampleEvent", (data) => {
  console.log(data);
});

All the original functionality of the EventEmitter is still intact:

import { ReplayEventEmitter } from "replay-event-emitter";

const eventEmitter = new ReplayEventEmitter();

eventEmitter.emit("exampleEvent", "someData");

eventEmitter.addListener("exampleEvent", (data) => {
  // This handler will NOT get triggered, as this is not a replay method and the
  // data emission happened BEFORE the event registration.
  console.log(data);
});

Readme

Keywords

none

Package Sidebar

Install

npm i @twilio/replay-event-emitter

Weekly Downloads

62,541

Version

0.3.7

License

MIT

Unpacked Size

144 kB

Total Files

18

Last publish

Collaborators

  • twilio-messaging