| 
1 | 1 | import { Component, OnInit } from '@angular/core';  | 
2 | 2 | import { TetrisQuery } from '@trungk18/state/tetris/tetris.query';  | 
3 |  | -import { Observable } from 'rxjs';  | 
 | 3 | +import { Observable, of, timer } from 'rxjs';  | 
 | 4 | +import { switchMap, map } from 'rxjs/operators';  | 
 | 5 | +import { untilDestroyed, UntilDestroy } from '@ngneat/until-destroy';  | 
4 | 6 | 
 
  | 
 | 7 | +const REFRESH_LABEL_INTERVAL = 3000;  | 
 | 8 | +@UntilDestroy()  | 
5 | 9 | @Component({  | 
6 | 10 |   selector: 't-point',  | 
7 | 11 |   templateUrl: './point.component.html',  | 
8 | 12 |   styleUrls: ['./point.component.scss']  | 
9 | 13 | })  | 
10 | 14 | export class PointComponent implements OnInit {  | 
11 |  | -  points$: Observable<number>;  | 
 | 15 | +  labelAndPoints$: Observable<LabelAndNumber>;  | 
12 | 16 |   constructor(private _query: TetrisQuery) {}  | 
13 | 17 | 
 
  | 
14 | 18 |   ngOnInit(): void {  | 
15 |  | -    this.points$ = this._query.points$;  | 
 | 19 | +    this.renderLabelAndPoints();  | 
16 | 20 |   }  | 
 | 21 | + | 
 | 22 | +  private renderLabelAndPoints() {  | 
 | 23 | +    this.labelAndPoints$ = this._query.hasCurrent$.pipe(  | 
 | 24 | +      untilDestroyed(this),  | 
 | 25 | +      switchMap((hasCurrent) => {  | 
 | 26 | +        if (hasCurrent) {  | 
 | 27 | +          return of(new LabelAndNumber('Score', this._query.raw.points));  | 
 | 28 | +        }  | 
 | 29 | +        return timer(0, REFRESH_LABEL_INTERVAL).pipe(  | 
 | 30 | +          map((val) => {  | 
 | 31 | +            let isOdd = val % 2 === 0;  | 
 | 32 | +            let { points, max } = this._query.raw;  | 
 | 33 | +            return isOdd ? new LabelAndNumber('Score', points) : new LabelAndNumber('Max ', max);  | 
 | 34 | +          })  | 
 | 35 | +        );  | 
 | 36 | +      })  | 
 | 37 | +    );  | 
 | 38 | +  }  | 
 | 39 | +}  | 
 | 40 | + | 
 | 41 | +class LabelAndNumber {  | 
 | 42 | +  constructor(public label: string, public points: number) {}  | 
17 | 43 | }  | 
0 commit comments