smoke-signal
Simple small functional event observer for the browser and node
Installation
npm install smoke-signal
Usage
const signal = const onMySignal = // attach listenerFn to eventconst listenAll = onMySignal // allow to listen only onceconst listenOnce = onMySignal // trigger eventonMySignal // unlisten to eventonMySignal // pause listening (pretty much the same as `onMySignal.pull(listenerFn)`)listenAll // resume listening (pretty much the same as `onMySignal.push(listenerFn)`)listenAll // remove all listenersonMySignalclear
It's also possible to listen and trigger with args
const signal = const onMySignal = // attach listenerFn to eventonMySignal // trigger eventonMySignal
Async trigger
It's also possible to have async handlers and wait for all handlers to settle.
const signal = const onMySignal = // attach async listenerFn to eventonMySignal // trigger event and wait for all handlers to finish// this resolves when all promises are settled, think `Promise.all`, no matter what outcomeawait onMySignal
Error handling is the same as in synchronous version.
Error handling
There are three ways of handling errors in listener, ignore (default), log, handle
To log the errors initialize with option logExceptions
.
const signal = const onMySignal = // attach listenerFn to eventonMySignal // trigger eventonMySignal// logs error to std.error
To handle errors initialize with option onError
const signal = const onMySignal = // attach listenerFn to eventonMySignal // trigger eventonMySignal