import { Notification } from '../models/notification' ; import { Injectable } from '@angular/core' ; import { Subject , ReplaySubject } from 'rxjs' ; import { map } from 'rxjs/operators' ; const initialNotifications = [ ] ; type INotificationsOperation = ( notifications : Notification [ ] ) => Notification [ ] ; @ Injectable ( ) export class NotificationsService { private notificationsList : Notification [ ] = [ ] ; public newNotifications : Subject < Notification > = new Subject < Notification > ( ) ; public notifications : ReplaySubject < Notification [ ] > = new ReplaySubject < Notification [ ] > ( 1 ) ; public updates : Subject < any > = new Subject < any > ( ) ; public create : Subject < Notification > = new Subject < Notification > ( ) ; constructor ( ) { this . updates . subscribe ( ( ope ) => { this . notificationsList = ope ( this . notificationsList ) ; console . log ( this . notificationsList ) ; this . notifications . next ( this . notificationsList ) ; } ) ; this . newNotifications . pipe ( map ( function ( notification ) : INotificationsOperation { return ( notifications ) => { return notifications . concat ( notification ) ; } ; } ) ) . subscribe ( this . updates ) ; } public addNotification ( notification ) { this . newNotifications . next ( notification ) ; } }