kefir.handlebytype

2.0.0 • Public • Published

kefir.handlebytype

A tiny helper to facilitate event replication over Kefir.Observable Flux-like event streams. Works best when paired with 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.

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.

source.withHandler(handle({
  value: handleByType({
    FOO(emitter, data, event) { emitter.event(event); },
  }),
}, { only: true }));
 
// Only FOO events will be re-emitted.

Readme

Keywords

Package Sidebar

Install

npm i kefir.handlebytype

Weekly Downloads

1

Version

2.0.0

License

ISC

Last publish

Collaborators

  • 32bitkid