angular - Limit number of subscriptions for an rxjs observable -
i want find observable,subject or event emitter replacement supports 1 , 1 subscription @ given time. i.e. first 1 subscription have right execute subscribe's body , when unsubscribes next subscriber allowed proceed.
is there method inside observable, subject or event emitter supports functionality or replacement available can allow behavior.
alternatively if there technique can perform functionality when ever 1 subscribes/unsubscribes our target emitter can perform functionality using access modifier , boolean.
currently trying this:
private opendialogemitter = new eventemitter(); private isfirstfetch: boolean = true; getdialogemitter(): eventemitter<{}> { if (this.isfirstfetch) { this.isfirstfetch = false; return this.opendialogemitter; } return null; } setfirstfetch(){ this.isfirstfetch = true; } when ever 1 unsubscribes has mark observable available again calling setfirstfetch() method inside service.
is there better , built in approach?
it's hard tell why need because there're operators such share make sure you'll have 1 subscription source observable.
since eventemitter extends subject class can check whether has subscriptions following (see https://github.com/reactivex/rxjs/blob/master/src/subject.ts#l28):
const source = new subject(); ... if (source.observers.length > 0) { ... } if want perform action on subscription/unsubscription can use observable.defer() , add custom tear down functions subscription object add() method:
const source = new subject(); const o = observable.defer(() => { console.log('subscription'); return source; // or can create source inside function if want. }); const subscription = o.subscribe(...); subscription.add(() => { console.log('unsubscription'); });
Comments
Post a Comment