How to make a reference with data?

  • 0
    Now a service has been written that has one method that returns data directories:

    @Injectable()
    export class InteragencyCooperationReferencies {
        public doctypes: ReplaySubject<any> = new ReplaySubject(1);
        public results: ReplaySubject<any> = new ReplaySubject(1);
        public reqtypes: ReplaySubject<any> = new ReplaySubject(1);
        public status: ReplaySubject<any> = new ReplaySubject(1);
    
        constructor(private interagencyCooperationService: InteragencyCooperationService) {
            this.interagencyCooperationService.catalogsInterdepartInteraction().subscribe((response) => {
                this.doctypes.next(response.doctypes);
                this.results.next(response.results);
                this.reqtypes.next(response.reqtypes);
            });
        }
    }


    Next, it packs up the data by preferred ReplaySubject . Where a reference is needed, I subscribe to the corresponding variable. How correct is this approach?

    There is such a doubt - would it not be better to use AsyncSubject together with ReplaySubject, since the directories are loaded once and, accordingly, only the last value needs to be received.
    JavaScript Anonymous, Mar 31, 2020

  • 1 Answers
  • 0
    It will be right this way

    private response = this.interagencyCooperationService.catalogsInterdepartInteraction()
    .pipe(shareReplay(1));
    public doctypes = this.response.pipe(map(r => r.doctypes));
    public results = this.response.pipe(map(r => r.results));
    public reqtypes = this.response.pipe(map(r => r.reqtypes));




    Subjects are not needed, subscription too.
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!