aboutsummaryrefslogtreecommitdiffstats
path: root/src/angular/notifications/services/notifications.service.ts
blob: 28a645c5df4cb74403890c68f2becb983261d1eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { Injectable } from '@angular/core';
import { NotificationSettings } from '../utilities/notification.config'
import { Subject } from 'rxjs/Subject';
import { Subscription } from 'rxjs/Subscription';


@Injectable()
export class NotificationsService  {

    notifs : NotificationSettings[] = [];

    notifQueue : Subject<any> = new Subject<any>();

    constructor() {}

    public push(notif : NotificationSettings):void{

        if( this.notifQueue.observers.length > 0 ) {
            this.notifQueue.next(notif);
        } else {
            this.notifs.push(notif);
        }
    }



    public getNotifications() : NotificationSettings[] {
        return this.notifs;
    }



    public subscribe(observer): Subscription {
        let s:Subscription = this.notifQueue.subscribe(observer);
        this.notifs.forEach(notif => this.notifQueue.next(notif));
        this.notifs = [];
        return s;
    }


}