All files / common/services blink.service.ts

100% Statements 13/13
100% Branches 0/0
100% Functions 7/7
100% Lines 10/10

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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74                                          1x 23x   23x 23x                                   23x 21x       1x               1x             1x             1x      
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { Injectable } from '@angular/core';
import { BehaviorSubject, timer } from 'rxjs';
import {
  distinctUntilChanged,
  endWith,
  filter,
  map,
  repeat,
  share,
  takeUntil
} from 'rxjs/operators';
 
/**
 * A global blink pulse generator for synchronized blinking patterns across an entire application.
 * Use to trigger any blinking by subscribing to `pulse$`.
 */
@Injectable({ providedIn: 'root' })
export class BlinkService {
  private pause$ = new BehaviorSubject<boolean>(false);
 
  private off$ = this.pause$.pipe(filter(v => v));
  private on$ = this.pause$.pipe(filter(v => !v));
 
  /**
   * Blink pulse. Subscribe to it to toggle `on` CSS class when true, `off` CSS class when `false`.
   * Do animations using CSS transitions
   *
   * @defaultValue
   * ```
   * timer(0, 1400).pipe(
   *     map(count => count % 2 === 0),
   *     distinctUntilChanged(),
   *     takeUntil(this.off$),
   *     endWith(false),
   *     repeat({ delay: () => this.on$ }),
   *     share()
   *   )
   * ```
   */
  public readonly pulse$ = timer(0, 1400).pipe(
    map(count => count % 2 === 0),
    distinctUntilChanged(),
    takeUntil(this.off$),
    endWith(false),
    repeat({ delay: () => this.on$ }),
    share()
  );
 
  /**
   * Pause the blinking.
   */
  pause(): void {
    this.pause$.next(true);
  }
 
  /**
   * Resume the blinking.
   */
  resume(): void {
    this.pause$.next(false);
  }
 
  /**
   * Whether the blinking is paused or not.
   */
  isPaused(): boolean {
    return this.pause$.value;
  }
}