Unsubscribe from observables through delegate methods.
Using Bacon with traditional Object Oriented frameworks can be hard. These frameworks typically rely methods with side effects. Disposing of your observables based on your chosen framework's component lifecylce can be tedious.
Bacon-unsub is a small utility module that augments Bacon's onValue
, onError
onEnd
, and subscribe
methods with
an unsubOn
method that automatically unsubscribes the observable from the source whenever that method
is called.
Install
$ npm install --save bacon-unsub
Usage
var Bacon = ; // Augments Bacon subscription methods with the 'unsubOn' methodBacon; // Mock object that has a destroy methodvar frameworkObject = { console; }; // Repeat numbers until unsubscribedBacon ; // Call the delegate method after 2 seconds.// Values are no longer printed after 'Framework Destroy'.;
Multiple unsubOn
calls pointing to the same delegate method will all get unsubscribed when the delegate is called.
Bacon ; Bacon ; ;
The following example integrates with Backbone. When the Backbone.View.remove method is called the subscription created in the initialize
method is automatically unsubscribed.
var Bacon = ;Bacon; var FooView = BackboneView; // Create the view and append it to the dom.var view = ;documentbody; // Remove it after 2 seconds.;
License
MIT © Paul LeMarquand