import { DataSource } from '@angular/cdk/collections'; import { BehaviorSubject, from, Observable, of } from 'rxjs'; import { catchError, finalize, map } from 'rxjs/operators'; import { Application } from 'src/app/proto/generated/management_pb'; import { ProjectService } from 'src/app/services/project.service'; /** * Data source for the ProjectMembers view. This class should * encapsulate all logic for fetching and manipulating the displayed data * (including sorting, pagination, and filtering). */ export class ProjectApplicationsDataSource extends DataSource { public totalResult: number = 0; public appsSubject: BehaviorSubject = new BehaviorSubject([]); private loadingSubject: BehaviorSubject = new BehaviorSubject(false); public loading$: Observable = this.loadingSubject.asObservable(); constructor(private projectService: ProjectService) { super(); } public loadApps(projectId: string, pageIndex: number, pageSize: number, sortDirection?: string): void { const offset = pageIndex * pageSize; this.loadingSubject.next(true); from(this.projectService.SearchApplications(projectId, pageSize, offset)).pipe( map(resp => { this.totalResult = resp.toObject().totalResult; return resp.toObject().resultList; }), catchError(() => of([])), finalize(() => this.loadingSubject.next(false)), ).subscribe(apps => { this.appsSubject.next(apps); }); } /** * Connect this data source to the table. The table will only update when * the returned stream emits new items. * @returns A stream of the items to be rendered. */ public connect(): Observable { return this.appsSubject.asObservable(); } /** * Called when the table is being destroyed. Use this function, to clean up * any open connections or free any held resources that were set up during connect. */ public disconnect(): void { this.appsSubject.complete(); this.loadingSubject.complete(); } }