Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/ryanve/emoter
- Owner: ryanve
- Created: 2015-11-08T00:49:28.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2015-11-08T01:02:13.000Z (about 9 years ago)
- Last Synced: 2024-08-31T13:35:57.145Z (2 months ago)
- Topics: event-emitter, eventemitter, javascript, module, state-management
- Language: JavaScript
- Homepage: https://npm.im/emoter
- Size: 0 Bytes
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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 81app.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