Skip to content

Instantly share code, notes, and snippets.

@kosich
Created May 21, 2019 08:23
Show Gist options
  • Save kosich/4f281f6ff5b4b52b517196616f3f7dc8 to your computer and use it in GitHub Desktop.
Save kosich/4f281f6ff5b4b52b517196616f3f7dc8 to your computer and use it in GitHub Desktop.
Attempt to create custom Subject
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