2.0.0 • Published 7 years ago

kefir.handlebytype v2.0.0

Weekly downloads
1
License
ISC
Repository
github
Last release
7 years ago

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.