-
-
Notifications
You must be signed in to change notification settings - Fork 228
/
notification.service.ts
54 lines (44 loc) · 1.47 KB
/
notification.service.ts
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
42
43
44
45
46
47
48
49
50
51
52
53
54
import { Injectable } from '@angular/core';
import { Observable, Observer } from 'rxjs';
import { shareReplay } from 'rxjs/operators';
import { Notification } from '../model';
@Injectable({
providedIn: 'root',
})
export class NotificationService {
private observable: Observable<Notification | 'close'>;
private observer: Observer<Notification | 'close'>;
private bootGrace = false;
public notificationStack: Array<Notification> = [];
public constructor() {
this.observable = new Observable((observer: Observer<Notification | 'close'>): void => {
this.observer = observer;
setTimeout((): void => {
this.bootGrace = false;
}, 30000);
}).pipe(shareReplay({ bufferSize: 1, refCount: true }));
}
public closeNotification(): void {
this.observer.next('close');
}
public setNotification(notification: Notification): void {
if (this.observer) {
this.observer.next(notification);
this.notificationStack.push(notification);
if (this.notificationStack.length > 25) {
this.notificationStack.shift();
}
} else {
setTimeout(this.setNotification.bind(this), 1000, notification);
}
}
public removeNotification(notification: Notification) {
this.notificationStack = this.notificationStack.filter(n => n.time !== notification.time);
}
public getObservable(): Observable<Notification | 'close'> {
return this.observable;
}
public getBootGrace(): boolean {
return this.bootGrace;
}
}