Simon Fletcher is a 24 year old British software engineer currently working with JavaScript at Minbox in San Francisco, CA.
Previously, he was a founder at Y Combinator funded Interstate (acquired by Stripe).
There is a simple event dispatcher pattern which I often find myself reimplementing across different projects so I decided to extract out a common implementation in to a module: create-dispatcher.
The module exports a single function createDispatcher
which, when called, returns three methods: destroy
, dispatch
and subscribe
. In the most simplest use case, you can call subscribe
with a function that will be called whenever events are dispatched with dispatch
:
import createDispatcher from 'create-dispatcher';
const { dispatch, subscribe } = createDispatcher();
const unsubscribe = subscribe(::console.log); // {"type":"helllo"}
dispatch({ type: 'hello' });
unsubscribe();
You can install the module via npm install create-dispatch --save
and the source code/documentation is available on GitHub at https://github.com/Simonify/create-dispatcher.