Does Functional Reactive Programming In Javascript Cause Bigger Problems With Listener References?
Solution 1:
In RxJs, each Observer
will, by default, have a separate listener on the original event source. So, if you have
var s = $('#textInput').keyupAsObservable()
s.subscribe(subscriber1);
s.map(function() {}).subscribe(subscriber2);
You'll have two keyup listeners. You can use .publish().refCount()
to make an Observable
maintain a single connection to its source.
In Bacon.js, Observables always maintain a single connection to their source.
In both libraries the connection to the source is created lazily (when an Observer
is added) and removed automatically when the (last) Observer is removed. Therefore you don't have to manually manage the listeners.
However, in the case where the subject
has a longer life span than the Observer
, you'll have to make sure the observer stops its subscription when its lifespan ends, or you'll have a leak. Neither libraries have any "magical" way of managing this, because to the library, your Observer
is just a function.
Personally I often create an Observable
called death
or whatever to signal the end-of-life for the Observer and then instead of subscribing to the subject
I subscribe to subject.takeUntil(death)
.
Regarding Elm, my understanding is that you have set up your entire event network at once, so there's no possibility for leak; Observers
cannot be added at a later stage.
Post a Comment for "Does Functional Reactive Programming In Javascript Cause Bigger Problems With Listener References?"