diff --git a/web/src/app/modules/shared/pipes/relative/relative.pipe.ts b/web/src/app/modules/shared/pipes/relative/relative.pipe.ts index d41c79f9a1..a3fb67a419 100644 --- a/web/src/app/modules/shared/pipes/relative/relative.pipe.ts +++ b/web/src/app/modules/shared/pipes/relative/relative.pipe.ts @@ -4,13 +4,26 @@ */ import { - Pipe, - PipeTransform, ChangeDetectorRef, - OnDestroy, NgZone, + OnDestroy, + Pipe, + PipeTransform, } from '@angular/core'; +const changeDetectionFrequency = (seconds: number) => { + switch (true) { + case seconds < 60: + return 1; + case seconds < 3600: + return 60; + case seconds < 86400: + return 600; + default: + return 3600; + } +}; + @Pipe({ name: 'relative', pure: false, @@ -25,10 +38,12 @@ import { */ export class RelativePipe implements PipeTransform, OnDestroy { private timer: number; + constructor( private changeDetectorRef: ChangeDetectorRef, private ngZone: NgZone ) {} + transform(ts: number, base?: Date): string { this.removeTimer(); @@ -41,12 +56,16 @@ export class RelativePipe implements PipeTransform, OnDestroy { const then = now.getTime() / 1000 - ts; - const updateInterval = this.changeDetectionFrequency(then) * 1000; + const updateInterval = changeDetectionFrequency(then) * 1000; this.timer = this.ngZone.runOutsideAngular(() => { if (typeof window !== 'undefined') { return window.setTimeout(() => { - this.ngZone.run(() => this.changeDetectorRef.markForCheck()); + this.ngZone.run(() => { + if (this.changeDetectorRef) { + this.changeDetectorRef.markForCheck(); + } + }); }, updateInterval); } return null; @@ -73,17 +92,4 @@ export class RelativePipe implements PipeTransform, OnDestroy { this.timer = null; } } - - private changeDetectionFrequency(seconds: number) { - switch (true) { - case seconds < 60: - return 1; - case seconds < 3600: - return 60; - case seconds < 86400: - return 600; - default: - return 3600; - } - } }