https://github.com/32bitkid/kefir.handlebytype
https://github.com/32bitkid/kefir.handlebytype
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/32bitkid/kefir.handlebytype
- Owner: 32bitkid
- License: isc
- Created: 2017-01-28T18:43:20.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-04T22:43:21.000Z (over 9 years ago)
- Last Synced: 2025-10-12T15:51:23.346Z (8 months ago)
- Language: JavaScript
- Size: 7.81 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# kefir.handlebytype
A tiny helper to facilitate event replication over Kefir.Observable Flux-like event streams. Works best when paired with [`kefir.handle`](https://github.com/32bitkid/kefir.handle).
## Installation
### NPM
```
npm install kefir.handlebytype
```
## Usage
Each of handler is invoked with three arguments: an emitter, the value of the event (_without_ the `type` property), and an event object.
```js
import Kefir from 'kefir';
import handle from 'kefir.handle';
import handleByType from 'kefir.handlebytype';
var source = Kefir.sequentally(100, [
{ type: 'FOO', foo: 'hello' },
{ type: 'BAR', bar: 'world' },
]);
source.withHandler(handle({
value: handleByType({
FOO(emitter, data) {
emitter.value({ type: 'BAZ', data: data.foo });
},
BAR(emitter, data) {
emitter.value({ type: 'BAZ', data: data.bar });
}
}),
}));
```
A second argument, `options`, can be passed to further control the handler. A options value of `{ only: true }` will throw away types that are _not_ matched by any handler.
```js
source.withHandler(handle({
value: handleByType({
FOO(emitter, data, event) { emitter.event(event); },
}),
}, { only: true }));
// Only FOO events will be re-emitted.
```