Browse Source

feat: shareReplay response and improve callback type

pull/3956/head
Arman Ozak 6 years ago
parent
commit
0744544259
  1. 20
      npm/ng-packs/packages/core/src/lib/services/list.service.ts
  2. 11
      npm/ng-packs/packages/core/src/lib/tests/list.service.spec.ts

20
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<T extends any>(streamCreatorCallback: QueryStreamCreatorCallback<T>): Observable<T> {
hookToQuery<T extends any>(
streamCreatorCallback: QueryStreamCreatorCallback<T>,
): Observable<PagedResultDto<T>> {
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<T> = (query: ABP.PageQueryParams) => Observable<T>;
export type QueryStreamCreatorCallback<T> = (
query: ABP.PageQueryParams,
) => Observable<PagedResultDto<T>>;

11
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<ABP.PageQueryParams> = 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<ABP.PageQueryParams> = query =>
of({ items: [query], totalCount: 1 });
service.isLoading$.pipe(bufferCount(3)).subscribe(([idle, init, end]) => {
expect(idle).toBe(false);

Loading…
Cancel
Save