/** Angular Imports */ import { CollectionViewer , DataSource } from '@angular/cdk/collections' ; /** rxjs Imports */ import { Observable , BehaviorSubject } from 'rxjs' ; /** Custom Services */ import { AccountingService } from '../accounting.service' ; /**
 * Journal entries custom data source to implement server side filtering, pagination and sorting.
 */ export class JournalEntriesDataSource implements DataSource < any > { /** Journal entries behavior subject to represent loaded journal entries page. */ private journalEntriesSubject = new BehaviorSubject < any [ ] > ( [ ] ) ; /** Records subject to represent total number of filtered journal entry records. */ private recordsSubject = new BehaviorSubject < number > ( 0 ) ; /** Records observable which can be subscribed to get the value of total number of filtered journal entry records. */ public records$ = this . recordsSubject . asObservable ( ) ; param { AccountingService } accountingService constructor ( private accountingService ) { } param { any } filterBy param { string } orderBy param { string } sortOrder param { number } pageIndex param { number } limit getJournalEntries ( filterBy , orderBy = '' , sortOrder = '' , pageIndex = 0 , limit = 10 ) { this . journalEntriesSubject . next ( [ ] ) ; orderBy = ( orderBy === 'debit' || orderBy === 'credit' ) ? 'amount' : orderBy ; this . accountingService . getJournalEntries ( filterBy , orderBy , sortOrder , pageIndex * limit , limit ) . subscribe ( ( journalEntries ) => { this . recordsSubject . next ( journalEntries . totalFilteredRecords ) ; this . journalEntriesSubject . next ( journalEntries . pageItems ) ; } ) ; } param { CollectionViewer } collectionViewer connect ( collectionViewer ) { return this . journalEntriesSubject . asObservable ( ) ; } param { CollectionViewer } collectionViewer disconnect ( collectionViewer ) { this . journalEntriesSubject . complete ( ) ; this . recordsSubject . complete ( ) ; } }