Last active
October 22, 2017 20:05
-
-
Save rarmatei/c019fd28f3472e74859aa21bb3d2b3e7 to your computer and use it in GitHub Desktop.
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
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