Skip to content

Instantly share code, notes, and snippets.

@rarmatei
Last active October 22, 2017 20:05
Show Gist options
  • Save rarmatei/c019fd28f3472e74859aa21bb3d2b3e7 to your computer and use it in GitHub Desktop.
Save rarmatei/c019fd28f3472e74859aa21bb3d2b3e7 to your computer and use it in GitHub Desktop.
function refCount(source) {
let length = 0;
const onNewSubscriber = () => {
const onUnsubscribe = () => {
length--;
if(length === 0) {
source.connect().unsubscribe();
}
};
length++;
if(length === 1) {
source.connect();
}
return onUnsubscribe;
};
const obs = () => source;
return Rx.Observable.using(onNewSubscriber, obs);
}
const source = Rx.Observable
.interval(1000)
.take(10)
.publishReplay(1)
.let(refCount);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment