kefirjs / kefir

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

Simplify subscription methods? #151

Closed rpominov closed 8 years ago

rpominov commented 9 years ago

Extracting this from #103 to own issue, see https://github.com/rpominov/kefir/issues/103#issuecomment-100713065 and other comments on that issue.

frankandrobot commented 8 years ago

@rpominov +1

I'm actually already doing something similar. I use FRP in a ReactJS app. We setup stream listeners (aka subscribers) in #componentWillMount. We then need a convenient way to unsubscribe them in #compouentWillUnMount. The notation below just makes it way more convenient.

All I'm doing now is remapping #onValue to return an unsub function.

Add Observable.prototype.observe() that accepts an object with same shape as emitter, and returns a dispose function.

|Before                  |  After                                         |
|------------------------|------------------------------------------|
|obs.onValue(fn);  |    let dispose = obs.observe(fn); |
|obs.offValue(fn);  | dispose();                                 |
rpominov commented 8 years ago

I'd really appreciate a PR for this. I marked it as [breaking] at first meaning that we remove/deprecate current subscription methods at same time. But on the second thought, this is not necessary, they can coexist for some time.

mAAdhaTTah commented 8 years ago

FWIW, I've been making a habit of converting toESObservable before returning a Kefir stream, primarily because I much prefer the semantics of the Subscription token. If this is still something you're looking for, I would be willing to take a look at possibly implementing it. It would also have the added benefit of (maybe) making Kefir directly compatible with the es-observable spec (sans the "unsub on error" difference).

rpominov commented 8 years ago

If this is still something you're looking for, I would be willing to take a look at possibly implementing it.

That would be great.

It would also have the added benefit of (maybe) making Kefir directly compatible with the es-observable spec (sans the "unsub on error" difference).

I prefer a bit more naming proposed in https://github.com/rpominov/kefir/issues/103#issuecomment-100713065 , but probably to use same names with other libs is a better idea indeed.

So, yeah, that would be cool if you want to make a PR. Would you consider also making emitter fit the observer interface (add next and complete to it)? This is partially done already but with names proposed in https://github.com/rpominov/kefir/issues/103#issuecomment-100713065 IIRC.