K
K
Konstantin2020-07-14 18:01:18
Angular
Konstantin, 2020-07-14 18:01:18

How to separate calls?

I have an input function `loadPage()` which is called in two ways: on initialization of the component and on the occurrence of events (subject).

The problem is that now in the constructor it calls loadPage() by default + if evens come.

In this case, multiple requests are sent. And there should be one. How to differentiate default call from event calls?

My code:

ngOnInit() {
        this.getPage();

        /* Событие вызывается при сбросе фильтра */

        this.eventsService.subjectFilterDistributionReset.subscribe((value) => {
            if (value) {
                this.paginationService.reset();
                this.loadPage();
            }
        });

        /* Событие вызывается при переходе с поиска */

        this.eventsService.searchFilterDistribution.pipe(filter((querySearch) => querySearch)).subscribe((querySearch) => {
            this.paginationService.setQuery(querySearch);
            this.paginationService.reset();
            this.loadPage();
        });

        /* Событие вызывается при фильтрации */

        this.eventsService.subjectFilterDistribution.pipe(filter((filterUrl) => filterUrl)).subscribe((filterUrl) => {
            const page = 1;
            this.paginationService.setFilterBy(filterUrl);
            this.paginationService.setCurrentPage(page);
            this.paginationService.calculateOffsetLimit(page);
            this.loadPage();
        });

        /* Событие вызывается при сортировке */

        this.eventsService.subjectSortingDistribution.pipe(filter(Boolean)).subscribe((sortedList: ListItem[]) => {
            this.paginationService.setSortBy(getSortingString(sortedList));
            this.loadPage();
        });
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2020-07-14
@Junart1

Do not call any functions on inite, but send an event to a service that listens to them and makes a request.
It's the same with filtering and searching, everything should be merged into one source of data query changes.
Well, debounce it all in time, plus cancel the previous request if the requirements have changed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question