Skip to content

ryanve/emoter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

emoter

JavaScript event emitter for state changes

emoter uses energy and skate to emit events for state changes

npm install emoter --save

Create an emoter instance

var emoter = require('emoter')
var app = emoter()

Methods

.state

  • app.state(state) // get
  • app.state(state, value) // set (triggers listeners if change)

.on

  • app.on(state, listener) listen to state changes

.off

  • app.off(state, listener) remove listener
  • app.off(state) remove all state listeners
  • app.off() remove all listeners

.once

  • app.on(state, listener) listen for the next state change

.emit

  • app.emit(state) manually trigger state listeners

Example

var emoter = require('emoter')
var app = emoter()

app.on('user', function() {
  console.log(this.state('user'))
})

app.state('user', 47) // logs 47
app.state('user', 81) // logs 81

app.on('loading', function() {
  if (this.state('loading')) console.log('loading')
}).on('loaded', function() {
  if (this.state('loaded')) console.log('loaded')
})

app.load = function() {
  this.state('loading', true)
  // ...
  this.state('loading', false)
  this.state('loaded', true)
}

app.load()

Compatibility

Works in web browsers and node

License

MIT