Async event dispatcher.
yarn add @downpourdigital/dispatcher
npm i @downpourdigital/dispatcher
import { EventDispatcher } from '@downpourdigital/dispatcher';
const trigger = new EventDispatcher<number>();
const subscription = trigger.subscribe(
( callback, payload ) => {
// perform async action, run callback once complete
const t = setTimeout( () => callback(), payload );
// return cleanup function which is called if the event is canceled
return () => clearTimeout( t );
},
);
// or subscribe passively
const subscription2 = trigger.subscribePassive(
( payload ) => {
console.log( 'Event fired!' );
}
);
// dispatch event with payload
const event = trigger.dispatch( 2000 );
// once all callbacks have run, the promise is resolved
event.promise.then( () => console.log( 'all callbacks fired' ) );
// to cancel an event before it finished, run:
event.cancel();
// or
trigger.cancelAll();
// to unsubscribe, run:
subscription.unsubscribe();
// or
trigger.unsubscribe( subscription );