K
K
Konstantin2020-11-19 16:44:38
Angular
Konstantin, 2020-11-19 16:44:38

How to make a data guide?

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

@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 packages the data by name ReplaySubject . In a place where the directory is necessary - 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 directories are loaded once and, accordingly, only the last value needs to be obtained.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2020-11-19
@Junart1

It will be right

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.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question