Skip to content

Simple small functional event observer for the browser and node

License

Notifications You must be signed in to change notification settings

davidmoshal/smoke-signal

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status rethink.js js-standard-style

smoke-signal

Simple small functional event observer for the browser and node

Installation

npm install smoke-signal

Usage

var signal = require('smoke-signal')

var onMySignal = signal()

// attach listenerFn to event
var listener = onMySignal.push(listenerFn)

// allow to listen only once
var listener = onMySignal.once(listenerFn)

// trigger event
onMySignal.trigger()

// unlisten to event
onMySignal.pull(listenerFn)

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

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

// remove all listeners
onMySignal.clear()

It's also possible to listen and trigger with args

var signal = require('smoke-signal')

var onMySignal = signal()

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

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

Error handling

There are three ways of handling errors in listener, ignore (default), log, handle

To log the errors initialize with option logExceptions.

var signal = require('smoke-signal')

var 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

var signal = require('smoke-signal')

var 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

Languages

  • JavaScript 100.0%