emoter uses energy and skate to emit events for state changes
npm install emoter --save
var emoter = require('emoter')
var app = emoter()
app.state(state)
// getapp.state(state, value)
// set (triggers listeners if change)
app.on(state, listener)
listen to state changes
app.off(state, listener)
remove listenerapp.off(state)
remove all state listenersapp.off()
remove all listeners
app.on(state, listener)
listen for the next state change
app.emit(state)
manually trigger state listeners
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()
Works in web browsers and node
MIT