@downpourdigital/dispatcher

Async event dispatcher.

Usage no npm install needed!

<script type="module">
  import downpourdigitalDispatcher from 'https://cdn.skypack.dev/@downpourdigital/dispatcher';
</script>

README

@downpourdigital/dispatcher

npm bundle size Dependencies npm version

Async event dispatcher.

Installation

yarn add @downpourdigital/dispatcher
npm i @downpourdigital/dispatcher

Usage

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 );