Skip to content

StephanHoyer/smoke-signal

Repository files navigation

Build Status rethink.js code style: prettier

smoke-signal

Simple small functional event observer for the browser and node

Installation

npm install smoke-signal

Usage

const signal = require('smoke-signal')

const onMySignal = signal()

// attach listenerFn to event
const listenAll = onMySignal.push(listenerFn)

// allow to listen only once
const listenOnce = onMySignal.once(listenerFn)

// trigger event
onMySignal.trigger()

// unlisten to event
onMySignal.pull(listenerFn)

// pause listening (pretty much the same as `onMySignal.pull(listenerFn)`)
listenAll.pause()

// resume listening (pretty much the same as `onMySignal.push(listenerFn)`)
listenAll.resume()

// remove all listeners
onMySignal.clear()

It's also possible to listen and trigger with args

const signal = require('smoke-signal')

const onMySignal = signal()

// attach listenerFn to event
onMySignal.push(function (arg) {
  // arg === 'foo'
})

// trigger event
onMySignal.trigger('foo')

Async trigger

It's also possible to have async handlers and wait for all handlers to settle.

const signal = require('smoke-signal')

const onMySignal = signal()

// attach async listenerFn to event
onMySignal.push(async function (arg) {
  // do async stuff
})

// trigger event and wait for all handlers to finish
// this resolves when all promises are settled, think `Promise.all`, no matter what outcome
await onMySignal.triggerAsync('foo')

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 = require('smoke-signal')

const onMySignal = signal({
  logExceptions: true,
})

// attach listenerFn to event
onMySignal.push(function () {
  throw new Error('BOOM!')
})

// trigger event
onMySignal.trigger()
// logs error to std.error

To handle errors initialize with option onError

const signal = require('smoke-signal')

const onMySignal = signal({
  onError: function (err) {
    // do something about the error here
  },
})

// attach listenerFn to event
onMySignal.push(function () {
  throw new Error('BOOM!')
})

// trigger event
onMySignal.trigger()

About

Simple small functional event observer for the browser and node

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published