import { delay } from 'rxjs/operators' ; import { Injectable } from '@angular/core' ; import { DataSource } from '@angular/cdk/collections' ; import { Observable , of } from 'rxjs' ; import { WeatherForecast , weatherForecasts } from './forecast-models' ; @ Injectable ( ) export class ForecastService { delayMs = 500 ; getForecasts ( ) { return of ( weatherForecasts ) . pipe ( delay ( this . delayMs ) ) ; } getForecastByDate ( dateFormatted ) { const existingForcast = weatherForecasts . find ( w => w . dateFormatted === dateFormatted ) ; return of ( existingForcast ) . pipe ( delay ( this . delayMs ) ) ; } updateForecast ( weatherForecast ) { const oldForecast = weatherForecasts . find ( w => w . id === weatherForecast . id ) ; const newForecast = Object . assign ( oldForecast , weatherForecast ) ; return of ( newForecast ) . pipe ( delay ( this . delayMs ) ) ; } } export class ForecastDataSource extends DataSource < any > { /** Connect function called by the table to retrieve one stream containing the data to render. */ delayMs = 500 ; connect ( ) { return of ( weatherForecasts ) . pipe ( delay ( this . delayMs ) ) ; } disconnect ( ) { } }