Skip to content

Instantly share code, notes, and snippets.

@cwharris
Last active December 22, 2015 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwharris/6407405 to your computer and use it in GitHub Desktop.
Save cwharris/6407405 to your computer and use it in GitHub Desktop.
autoPublish
Rx.Observable.prototype.autoPublish = function () {
var source = this.publish(),
subscribers = 0,
connection = null,
dispose = function () {
if (--subscribers === 0) { connection.dispose(); }
};
return Rx.Observable.createWithDisposable(function (o) {
if (++subscribers === 1) { connection = source.connect(); }
return new Rx.CompositeDisposable(source.subscribe(o), Rx.Disposable.create(dispose));
});
};
Rx.Observable.prototype.autoPublish = function () {
return this.publish().refCount();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment