npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bacon-unsub

v0.1.2

Published

Unsubscribe from observables through delegate methods.

Downloads

28

Readme

NPM version

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 = require('baconjs');

// Augments Bacon subscription methods with the 'unsubOn' method
require('bacon-unsub')(Bacon);

// Mock object that has a destroy method
var frameworkObject = {
	destroy: function() {
		console.log('Framework Destroy');
	}
};

// Repeat numbers until unsubscribed
Bacon.repeatedly(200, [1,2,3,4,5])
    .onValue(function(value) {
    	console.log('Value:', value);
    })
    .unsubOn(frameworkObject, 'destroy');

// Call the delegate method after 2 seconds.
// Values are no longer printed after 'Framework Destroy'.
setTimeout(function() {
	frameworkObject.destroy();
}, 2000);

Multiple unsubOn calls pointing to the same delegate method will all get unsubscribed when the delegate is called.

Bacon.repeatedly(200, [1,2,3,4,5])
    .onValue(function(value) {
    	console.log('# Value:', value);
    })
    .unsubOn(frameworkObject, 'destroy');

Bacon.repeatedly(200, ['foo', 'bar'])
    .onValue(function(value) {
    	console.log('String Value:', value);
    })
    .unsubOn(frameworkObject, 'destroy');

setTimeout(function() {
	// Unsubscribes both observables
	frameworkObject.destroy();
}, 2000);

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 = require('baconjs');
require('bacon-unsub')(Bacon);

var FooView = Backbone.View.extend({
    initialize: function() {
		var el = this.el;

		// Update our view every 200ms with a new value until unsubscribed.
        Bacon.repeatedly(200, [1,2,3,4,5])
            .onValue(function(value) {
            	console.log('Value:', value);
                el.innerText = value;
            })
            .unsubOn(this, 'remove');
    },

    remove: function() {
        console.log('Removed!');
    }
});

// Create the view and append it to the dom.
var view = new FooView();
document.body.appendChild(view.el);

// Remove it after 2 seconds.
setTimeout(function() {
	view.remove();
}, 2000);

License

MIT © Paul LeMarquand