diff --git a/npm/ng-packs/packages/core/src/lib/services/list.service.ts b/npm/ng-packs/packages/core/src/lib/services/list.service.ts index 1e0b8e22b9..c41b1f3ff6 100644 --- a/npm/ng-packs/packages/core/src/lib/services/list.service.ts +++ b/npm/ng-packs/packages/core/src/lib/services/list.service.ts @@ -2,6 +2,7 @@ import { Inject, Injectable, Optional } from '@angular/core'; import { BehaviorSubject, Observable, ReplaySubject } from 'rxjs'; import { debounceTime, shareReplay, switchMap, tap } from 'rxjs/operators'; import { ABP } from '../models/common'; +import { PagedResultDto } from '../models/dtos'; import { LIST_QUERY_DEBOUNCE_TIME } from '../tokens/list.token'; import { takeUntilDestroy } from '../utils/rxjs-utils'; @@ -67,25 +68,28 @@ export class ListService { return this._isLoading$.asObservable(); } - constructor(@Optional() @Inject(LIST_QUERY_DEBOUNCE_TIME) private delay: number) { - this.get(); - } - - get() { + get = () => { this._query$.next({ filter: this._filter || undefined, maxResultCount: this._maxResultCount, skipCount: (this._page - 1) * this._maxResultCount, sorting: this._sortOrder ? `${this._sortKey} ${this._sortOrder}` : undefined, }); + }; + + constructor(@Optional() @Inject(LIST_QUERY_DEBOUNCE_TIME) private delay: number) { + this.get(); } - hookToQuery(streamCreatorCallback: QueryStreamCreatorCallback): Observable { + hookToQuery( + streamCreatorCallback: QueryStreamCreatorCallback, + ): Observable> { this._isLoading$.next(true); return this.query$.pipe( switchMap(streamCreatorCallback), tap(() => this._isLoading$.next(false)), + shareReplay({ bufferSize: 1, refCount: true }), takeUntilDestroy(this), ); } @@ -93,4 +97,6 @@ export class ListService { ngOnDestroy() {} } -export type QueryStreamCreatorCallback = (query: ABP.PageQueryParams) => Observable; +export type QueryStreamCreatorCallback = ( + query: ABP.PageQueryParams, +) => Observable>; diff --git a/npm/ng-packs/packages/core/src/lib/tests/list.service.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/list.service.spec.ts index b7e2cc8450..c2118d7f3a 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/list.service.spec.ts +++ b/npm/ng-packs/packages/core/src/lib/tests/list.service.spec.ts @@ -1,7 +1,8 @@ import { createServiceFactory, SpectatorService } from '@ngneat/spectator/jest'; import { of } from 'rxjs'; import { bufferCount, take } from 'rxjs/operators'; -import { ListService } from '../services/list.service'; +import { ABP } from '../models'; +import { ListService, QueryStreamCreatorCallback } from '../services/list.service'; import { LIST_QUERY_DEBOUNCE_TIME } from '../tokens'; describe('ListService', () => { @@ -119,9 +120,10 @@ describe('ListService', () => { describe('#hookToQuery', () => { it('should call given callback with the query', done => { - const callback = query => of(query); + const callback: QueryStreamCreatorCallback = query => + of({ items: [query], totalCount: 1 }); - service.hookToQuery(callback).subscribe(query => { + service.hookToQuery(callback).subscribe(({ items: [query] }) => { expect(query).toEqual({ filter: undefined, maxResultCount: 10, @@ -134,7 +136,8 @@ describe('ListService', () => { }); it('should emit isLoading as side effect', done => { - const callback = query => of(query); + const callback: QueryStreamCreatorCallback = query => + of({ items: [query], totalCount: 1 }); service.isLoading$.pipe(bufferCount(3)).subscribe(([idle, init, end]) => { expect(idle).toBe(false);