kefirjs / kefir

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

Allow off[Value|Error|Any|End] functions to have a zero argument that clears all callbacks #191

Closed indigo0086 closed 8 years ago

indigo0086 commented 8 years ago

For the off~ equivalent to on~ functions, a callback is needed to unregister the function used as the onValue callback. This requires a seperate definition of the callback or function definition as a reference to use in the offValue function. a simple preferred scenario is something like

let mouseClick = Kefir.fromEvents(document.body, 'click').onValue((evt) => console.log(evt.target))
mouseClick.onValue() //<--This should unregister the anonymous function

For a simple scenario where it would be more terse to use an anonymous function in the on value, onValue should provide a zero argument equivalent that clears the callback (and possibly others).

rpominov commented 8 years ago

I don't think this is a good idea. This will break the guarantee that only the module that have added a subscription can remove it.

But we have plans for better subscription methods: https://github.com/rpominov/kefir/issues/151

It will look like this:

let unsub = Kefir.fromEvents(document.body, 'click').observe((evt) => console.log(evt.target))
unsub()

What do you think?

indigo0086 commented 8 years ago

That would be a good solution. I guess in the mean time I can write up a simple unsubscribe function for my implementation via clickTest.offValue(mouseClick._dispatcher._items[0].fn).

rpominov commented 8 years ago

Ok, I think we can close this then.