File

src/services/notifications.service.ts

Index

Properties
Methods

Constructor

constructor()

Methods

Public addNotification
addNotification(notification: Notification)
Parameters :
Name Type Optional Description
notification Notification
Returns : void

Properties

Public create
create: Subject<Notification>
Type : Subject<Notification>
Public newNotifications
newNotifications: Subject<Notification>
Type : Subject<Notification>
Public notifications
notifications: ReplaySubject<[]>
Type : ReplaySubject<[]>
Private notificationsList
notificationsList: Notification[]
Type : Notification[]
Public updates
updates: Subject<any>
Type : Subject<any>
import { Notification } from '../models/notification';
import { Injectable } from '@angular/core';
import { Observable, Subject, ReplaySubject } from 'rxjs/Rx';

const initialNotifications: Notification[] = [];

type INotificationsOperation = (notifications: Notification[]) => Notification[];

@Injectable()
export class NotificationsService {
  private notificationsList: Notification[] = [];
  // a stream that publishes new notifications only once
  public newNotifications: Subject<Notification> = new Subject<Notification>();

  // `notifications` is a stream that emits an array of the most up to date notifications
  public notifications: ReplaySubject<Notification[]> = new ReplaySubject<Notification[]>(1);

  // `updates` receives _operations_ to be applied to our `notifications`
  // it's a way we can perform changes on *all* notifications (that are currently
  // stored in `notifications`)
  public updates: Subject<any> = new Subject<any>();

  // action streams
  public create: Subject<Notification> = new Subject<Notification>();
  // public markThreadAsRead: Subject<any> = new Subject<any>();

  constructor() {
    // recois des operation, et les fais sur la liste interne, puis diffuse le resultat sur notifications
    this.updates.subscribe((ope) => {
      this.notificationsList = ope(this.notificationsList);
      console.log(this.notificationsList);
      this.notifications.next(this.notificationsList);
    });

    this.newNotifications
      .map(function(notification: Notification): INotificationsOperation {
        return (notifications: Notification[]) => {
          return notifications.concat(notification);
        };
      })
      .subscribe(this.updates);

  }

  // an imperative function call to this action stream
  public addNotification(notification: Notification): void {
    this.newNotifications.next(notification);
  }

}

results matching ""

    No results matching ""