An adorably small event emitter/pubsub library.
import emy from 'emy';
const [publish, subscribe] = emy();
const unsubscribe = subscribe((event) => {
console.log(`Hello ${event.value}!`);
});
publish({ value: 'world' });
unsubscribe();
Creates a new event emitter.
const [publish, subscribe] = emy();
An array containing 2 functions, publish
and subscribe
.
These can be renamed to whatever you prefer, for example
[emit, on]
or[publishFoo, subscribeFoo]
.
Invokes all subscribed listeners.
publish({ value: 'world' });
-
event
any value to be passed to each listener.
Register a listener.
const unsubscribe = subscribe((event) => {
console.log(`Hello ${event.value}!`);
});
unsubscribe();
-
listener
function to call when event is published.
Returns a function which unregisters the listener when called.