Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bjarneo/hoki
Lightweight observer and dispatcher.
https://github.com/bjarneo/hoki
Last synced: 3 days ago
JSON representation
Lightweight observer and dispatcher.
- Host: GitHub
- URL: https://github.com/bjarneo/hoki
- Owner: bjarneo
- License: mit
- Created: 2016-03-11T19:37:25.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-10-07T08:46:33.000Z (about 7 years ago)
- Last Synced: 2024-10-03T06:46:21.046Z (about 1 month ago)
- Language: JavaScript
- Homepage:
- Size: 44.9 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
hoki
======
![Travis](https://travis-ci.org/bjarneo/hoki.svg?branch=master)
![Dependencies](https://david-dm.org/bjarneo/hoki.svg)Lightweight observer and dispatcher.
Should cover basic use cases.
No bloat.
Clean code.
Installation
------
It's available on npm.
```
npm install --save hoki
```How can I use this library?
------Usage
------
API
```js
const { register, unregister, observer, dispatcher, events } = require('hoki');// Can register an event or array of events
register(string || []);// Can unregister an event or array of events
unregister(string || []);// Can observe for an event, and fire a callback if the event occurs
observe(string, function);// Dispatch an event with data. Can also dispatch an empty event
dispatch(string, function/object/string/number/etc);// List all events
events();
```Example
```js
const { register, dispatcher, observer } = require('hoki');// Register your event
register('cat-names');// Observe for an event
observer('cat-names', name => {
console.log(name);
});
// output in correct order:
// furguson
// mittens
// bootsdispatcher('cat-names', 'furguson');
dispatcher('cat-names', 'mittens');
dispatcher('cat-names', 'boots');
```You can also add multiple observers for the same event
```js
const { register, observer, dispatcher } = require('hoki');register('cat-names');
observer('cat-names', console.log);
// output in correct order:
// furguson
// mittens
// bootsobserver('cat-names', console.log);
// output in correct order:
// furguson
// mittens
// bootsdispatcher('cat-names', 'furguson');
dispatcher('cat-names', 'mittens');
dispatcher('cat-names', 'boots');
```Listen once?
```js
const { register, unregister, observer, dispatcher } = require('hoki');register('listenOnce');
observer('listenOnce', data => {
unregister('listenOnce');
console.log(data);
});dispatcher('listenOnce', 'should run only once');
dispatcher('listenOnce', 'should run only once');```
List all events available
```js
const { events } = require('hoki');console.log(events());
// [ 'cat-names' ]
```Unregister
```js
const {events, unregister} = require('hoki');unregister('cat-names');
console.log(events());
// [ ]
```Tests
------
```bash
$ npm test
```Contribution
------
Contributions are appreciated.License
------
MIT-licensed. See LICENSE.