Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ryanve/emoter

JavaScript event emitter for state changes
https://github.com/ryanve/emoter

event-emitter eventemitter javascript module state-management

Last synced: about 1 month ago
JSON representation

JavaScript event emitter for state changes

Awesome Lists containing this project

README

        

# emoter
#### JavaScript event emitter for state changes
emoter uses [energy](https://github.com/ryanve/energy) and [skate](https://github.com/ryanve/skate) to emit events for state changes

```sh
npm install emoter --save
```

#### Create an emoter instance

```js
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

```js
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