kefirjs / kefir

A Reactive Programming library for JavaScript
https://kefirjs.github.io/kefir/
MIT License
1.87k stars 97 forks source link

extra `emitter` methods to use with `.withHandler` and `.fromBinder` #51

Closed mhelvens closed 9 years ago

mhelvens commented 9 years ago

It would be quite useful, for example, in a .withHandler handler, to be able to pass an event directly, and / or to plug a stream into an emitter:

stream.withHandler(function (emitter, event) {
    if (event.type === 'end') {
        emitter.plug(Kefir.later(1000, 1)); // a 'last minute concat'
    } else {
        emitter.send(event); // just pass the event
    }
})

Without this, some tedious branching-code would need to be written.

rpominov commented 9 years ago

Yep, something like .emitEvent in emitter object makes sense. As well as in emitter stream, at least for consistency. Will add it soon.

mhelvens commented 9 years ago

How about .plug? (It does seem like Event and Pool and Bus might all bleed together a bit, though I understand they are technically different concepts.)

rpominov commented 9 years ago

Not sure about this, not very easy to add, and will probably make .withHandler (and other methods where emitter object are used) a bit heavier.

You can use .flatMap for this. And, if you need to handle all kind of events, something like this:

stream.withHandler(function(event, emitter) {
  emitter.emit(event); // emitting event object as value, so all events available in next step
}).flatMap(function(event) { ... })