all files / src/app/services/ carousel.service.ts

100% Statements 13/13
100% Branches 4/4
100% Functions 4/4
100% Lines 11/11
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               164×               164× 20675× 5167×                   205×               33×      
import { Injectable } from '@angular/core';
import { NavigationStart, Router } from '@angular/router';
import { Observable, Subject } from 'rxjs';
import { CarouselModel } from '../models';
 
/**
 * Carousel Service
 */
@Injectable()
export class CarouselService {
    /** collection of carousel */
    private readonly subject = new Subject<CarouselModel>();
 
    /**
     * constructor of CarouselService
     * @param router: Router
     */
    constructor(router: Router) {
        // clear carousel on route change
        router.events.subscribe(event => {
            if (event instanceof NavigationStart) {
                this.subject.next();
            }
        });
    }
 
    /**
     * init carousel
     * @param carouselModel: CarouselModel
     */
    init(carouselModel: CarouselModel): void {
        if (carouselModel) {
            this.subject.next(carouselModel);
        }
    }
 
    /**
     * get current carousel
     */
    getCarousel(): Observable<CarouselModel> {
        return this.subject.asObservable();
    }
}