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 { ReplayEventListener } from "replay-event-listener";
const eventListener = new ReplayEventListener();
eventListener.emit("exampleEvent", "someData");
eventListener.addListenerWithReplay("exampleEvent", (data) => {
console.log(data);
});
You could also use the shorthand method onWithRepeat
, like so:
eventListener.onWithReplay("exampleEvent", (data) => {
console.log(data);
});
All the original functionality of the EventListener is still intact:
import { ReplayEventListener } from "replay-event-listener";
const eventListener = new ReplayEventListener();
eventListener.emit("exampleEvent", "someData");
eventListener.addListener("exampleEvent", (data) => {
console.log(data);
});