1.0.1 • Published 5 years ago
@reasonink/eventual v1.0.1
Eventual
Eventual is a compositional alternative to EventEmitter.
In contrast with EventEmitter, Eventual removes the need to inherit or wrap an EventEmitter and instead handles events at the variable or property level.
How it works
Create an Eventual instance using the eventual()
factory function.
let loaded = eventual();
Add handlers with on(callback)
or once(callback)
.
loaded.on(e => console.log(`Data loaded: ${e.data}`));
Fire the event with fire(event)
.
loaded.fire({ data: "the data" });
Callbacks can be removed with off()
.
function myHandler() {
// Do something
}
loaded.on(myHandler);
// ...
loaded.off(myHandler);
Type safety
Eventual is built with (but doesn't require) TypeScript. Event types can be
specified as a type argument to eventual<T>()
. Callbacks will expect the
given type as their only argument.
Example
interface DataEvent {
data: string;
}
let loaded = eventual<DataEvent>();
loaded.on((e: DataEvent) => {
// Do something with e
});