import { CollectionViewer, DataSource } from '@angular/cdk/collections'; import { Observable } from 'rxjs/Observable'; import { BehaviorSubject } from 'rxjs/BehaviorSubject'; import { of } from 'rxjs/observable/of'; import { catchError, finalize } from 'rxjs/operators'; import { RegularSubsService } from './regularsubs.service'; import { RegularSub } from '../models/regularsub'; export class RegularSubsDataSource implements DataSource { private regularSubsSubject = new BehaviorSubject([]); private countSubject = new BehaviorSubject(0); private loadingSubject = new BehaviorSubject(false); public count$ = this.countSubject.asObservable(); public loading$ = this.loadingSubject.asObservable(); constructor(private regularSubsService: RegularSubsService) { } loadRegularSubs(filter: string, sortColumn: string, sortDirection: string, pageIndex: number, pageSize: number) { this.loadingSubject.next(true); this.regularSubsService.findRegularSubs(filter, sortColumn, sortDirection, pageIndex, pageSize) .pipe( catchError(() => of([])), finalize(() => this.loadingSubject.next(false)) ) .subscribe(data => { this.regularSubsSubject.next(data.regularSubs); this.countSubject.next(data.totalCount); }); } connect(collectionViewer: CollectionViewer): Observable { console.log('Connecting data source'); return this.regularSubsSubject.asObservable(); } disconnect(collectionViewer: CollectionViewer): void { this.regularSubsSubject.complete(); this.countSubject.complete(); this.loadingSubject.complete(); } }