src/services/notifications.service.ts
Properties |
|
Methods |
|
constructor()
|
Defined in src/services/notifications.service.ts:26
|
Public addNotification | ||||||||
addNotification(notification: Notification)
|
||||||||
Defined in src/services/notifications.service.ts:48
|
||||||||
Parameters :
Returns :
void
|
Public create |
create:
|
Type : Subject<Notification>
|
Defined in src/services/notifications.service.ts:26
|
Public newNotifications |
newNotifications:
|
Type : Subject<Notification>
|
Defined in src/services/notifications.service.ts:15
|
Public notifications |
notifications:
|
Type : ReplaySubject<[]>
|
Defined in src/services/notifications.service.ts:18
|
Private notificationsList |
notificationsList:
|
Type : Notification[]
|
Defined in src/services/notifications.service.ts:13
|
Public updates |
updates:
|
Type : Subject<any>
|
Defined in src/services/notifications.service.ts:23
|
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);
}
}