Created
May 21, 2019 08:23
-
-
Save kosich/4f281f6ff5b4b52b517196616f3f7dc8 to your computer and use it in GitHub Desktop.
Attempt to create custom Subject
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
const { rxObserver } = require('api/v0.3'); | |
const { Subject, of, timer } = require('rxjs'); | |
const { finalize, takeUntil } = require('rxjs/operators'); | |
class CountSubject extends Subject { | |
constructor(){ | |
super(); | |
this.subscribersCount = 0; | |
} | |
subscribe(...args) { | |
this.subscribersCount++; | |
console.log('count', this.subscribersCount); | |
const subscription = super.subscribe(...args); | |
const unsubscribe = subscription.unsubscribe; | |
subscription.unsubscribe = (...args)=>{ | |
this.subscribersCount--; | |
console.log('count', this.subscribersCount); | |
return unsubscribe.call(subscription, ...args); | |
}; | |
return subscription; | |
} | |
} | |
const a$ = new CountSubject(); | |
const subscription1 = a$.pipe( | |
takeUntil(timer(500)) | |
).subscribe(rxObserver()); | |
// emit an event | |
setTimeout(()=>{ | |
a$.next(1); | |
}, 250); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment