Last active
November 25, 2017 13:56
-
-
Save rarmatei/f35b9eebf4394a6b637b2b619b888386 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class MyService { | |
constructor() { | |
this._producer = Rx.Observable.interval(1000) | |
.publish(); | |
} | |
private _producer: Rx.ConnectableObservable<number>; | |
startItUp(): Rx.Observable<number> { | |
return this._producer.refCount(); | |
} | |
get passiveValues(): Rx.Observable<string> { | |
return this._producer.mapTo("foo"); | |
} | |
} | |
// USAGE | |
const service = new MyService(); | |
let subscription: Rx.Subscription; | |
service.passiveValues | |
.subscribe(x => console.log(`Passive value: ${x}`)); | |
setTimeout(() => { | |
console.log("START TIMER FIRED"); | |
subscription = service.startItUp() | |
.subscribe(x => console.log(`active starter: ${x}`)); | |
}, 5000); | |
setTimeout(() => { | |
console.log("END TIMER FIRED"); | |
subscription.unsubscribe(); | |
}, 8000); | |
// OUTPUT | |
/* | |
START TIMER FIRED | |
Passive value: foo | |
active starter: 0 | |
Passive value: foo | |
active starter: 1 | |
END TIMER FIRED | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment