Last active
December 22, 2015 02:59
-
-
Save cwharris/6407405 to your computer and use it in GitHub Desktop.
autoPublish
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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