All files / src/services/zd-alert alert-queue.ts

100% Statements 17/17
100% Branches 8/8
100% Functions 4/4
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 4193x     93x 4x   4x     5x   5x 3x     5x               4x 3x     4x 4x 4x       1x   1x 1x        
import { Alert, IAlert } from '../../components';
import { IAlertsManager } from './interfaces';
 
export class AlertQueue implements IAlertsManager {
	protected queue: IAlert[] = [];
 
	public visibleInstances: Alert[] = [];
 
	show(alert: IAlert): number {
		this.queue.push(alert);
 
		if ((this.visibleInstances.length === 0 || !this.visibleInstances[0].isVisible) && this.queue.length === 1) {
			this.display(alert);
		}
 
		return 0;
	}
 
	/**
	 * Displays an alert
	 * @param alert Alert Structure
	 */
	protected display(alert: IAlert) {
		if (this.visibleInstances.length === 0) {
			this.visibleInstances.push(new Alert({ name: 'alert-instance', id: 0 }));
		}
 
		this.visibleInstances[0].assignAlertProperties(alert);
		this.visibleInstances[0].show();
		return 0;
	}
 
	remove(index: number): void {
		this.queue.splice(index, 1);
 
		if (index === 0 && this.queue[0]) {
			setTimeout(() => this.display(this.queue[0]), 200);
		}
	}
}