|
|
|
@ -20,6 +20,8 @@ import { ABP } from '../models/common'; |
|
|
|
import { PagedResultDto } from '../models/dtos'; |
|
|
|
import { LIST_QUERY_DEBOUNCE_TIME } from '../tokens/list.token'; |
|
|
|
|
|
|
|
export type RequestStatus = 'idle' | 'loading' | 'success' | 'error'; |
|
|
|
|
|
|
|
@Injectable() |
|
|
|
export class ListService<QueryParamsType = ABP.PageQueryParams | any> implements OnDestroy { |
|
|
|
private _filter = ''; |
|
|
|
@ -40,7 +42,6 @@ export class ListService<QueryParamsType = ABP.PageQueryParams | any> implements |
|
|
|
return this._maxResultCount; |
|
|
|
} |
|
|
|
|
|
|
|
private _skipCount = 0; |
|
|
|
private _page = 0; |
|
|
|
set page(value: number) { |
|
|
|
if (value === this._page) return; |
|
|
|
@ -91,12 +92,21 @@ export class ListService<QueryParamsType = ABP.PageQueryParams | any> implements |
|
|
|
|
|
|
|
private _isLoading$ = new BehaviorSubject(false); |
|
|
|
|
|
|
|
private _requestStatus = new BehaviorSubject<RequestStatus>('idle'); |
|
|
|
|
|
|
|
private destroy$ = new Subject<void>(); |
|
|
|
|
|
|
|
private delay: MonoTypeOperatorFunction<QueryParamsType>; |
|
|
|
|
|
|
|
/** |
|
|
|
* @deprecated Use `requestStatus$` instead. |
|
|
|
*/ |
|
|
|
get isLoading$(): Observable<boolean> { |
|
|
|
return this._isLoading$.asObservable(); |
|
|
|
return this._isLoading$.asObservable().pipe(takeUntil(this.destroy$)); |
|
|
|
} |
|
|
|
|
|
|
|
get requestStatus$(): Observable<RequestStatus> { |
|
|
|
return this._requestStatus.asObservable().pipe(takeUntil(this.destroy$)); |
|
|
|
} |
|
|
|
|
|
|
|
get = () => { |
|
|
|
@ -119,29 +129,32 @@ export class ListService<QueryParamsType = ABP.PageQueryParams | any> implements |
|
|
|
): Observable<PagedResultDto<T>> { |
|
|
|
return this.query$.pipe( |
|
|
|
tap(() => this._isLoading$.next(true)), |
|
|
|
tap(() => this._requestStatus.next('loading')), |
|
|
|
switchMap(query => streamCreatorCallback(query).pipe(catchError(() => of(null)))), |
|
|
|
filter(Boolean), |
|
|
|
tap(() => this._isLoading$.next(false)), |
|
|
|
tap(() => this._requestStatus.next('success')), |
|
|
|
shareReplay<any>({ bufferSize: 1, refCount: true }), |
|
|
|
takeUntil(this.destroy$), |
|
|
|
catchError(error => { |
|
|
|
this._requestStatus.next('error'); |
|
|
|
throw error; |
|
|
|
}), |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
ngOnDestroy() { |
|
|
|
this.destroy$.next(); |
|
|
|
} |
|
|
|
|
|
|
|
private resetPageWhenUnchanged() { |
|
|
|
const maxPage = Number(Number(this.totalCount / this._maxResultCount).toFixed()); |
|
|
|
const skipCount = this._page * this._maxResultCount; |
|
|
|
|
|
|
|
if (skipCount !== this._totalCount) { |
|
|
|
this._skipCount = skipCount; |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
if (this.page === maxPage && this.page > 0) { |
|
|
|
this._skipCount = skipCount - this._maxResultCount; |
|
|
|
this.page = this.page - 1; |
|
|
|
} |
|
|
|
} |
|
|
|
|