Search code examples
kefir.js

Do I need to unsubscribe a listener to stream that ends in Kefir?


I'm moving from BaconJS where this isn't an issue because you can just return Bacon.noMore in the subscriber to unsubscribe. In Kefir, unsubscribing is more boilerplate and so the natural question is---for streams that end, do you need to unsubscribe the listener (aka callback) or does Kefir handle that for you automagically?

const oneTimeStream = Kefir.constant('foo') // this ends after firing foo
const listener = function() { //... }

oneTimeStream.onValue(listener)
// do I need to unsubscribe #listener?

Solution

  • As per the library author, the answer is "No": https://github.com/rpominov/kefir/issues/182

    Yeah, you don't need to. All listeners automatically removed on end. Also when you subscribe to an ended stream it's a noop.

    Btw, because of that you can often avoid manual unsubscribing by limiting stream with take/takeWhile/takeUntilBy/etc.

    stream.take(1).onValue(fn)