Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/juliendargelos/dispatcher
https://github.com/juliendargelos/dispatcher
Last synced: 4 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/juliendargelos/dispatcher
- Owner: juliendargelos
- Created: 2017-10-29T16:47:33.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2017-10-29T17:05:19.000Z (about 7 years ago)
- Last Synced: 2024-11-11T05:40:01.704Z (2 months ago)
- Language: JavaScript
- Size: 32.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Dispatcher
This package let you make any object acting as an event dispatcher:
```javascript
import Dispatcher from 'dispatcher'class MyApi extends Dispatcher {
constructor () {
super()
this.load()
}load () {
// Loading stuff...
this.dispatch('load', {
message: 'Api loaded!'
})
}get (id, callback) {
// Your api needs to be loaded in order to get data!
// The callback passed to 'require' will be instantly called if
// the event already happened, if not it will wait for it.
this.require('load', () => {
// Getting data...
callback.call(this, data)this.dispatch('get', {
data: data
})
})
}update () {
// Updating stuff...
this.dispatch('update')
}
}var api = new Api()
api.once('load', event => alert(event.message)) // Will be called once before getting removed
api.on('update', event => alert('An update'), 5) // Will be called 5 times before getting removed
api.on('get', function getListener(event) { // Will be called until the listener is removed
console.log(event.data)
})setTimeout(() => api.off('get', getListener), 5000) // Now removed...
```