Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Commit

Permalink
check for presence of cdr before doing a mark for check
Browse files Browse the repository at this point in the history
Signed-off-by: bryanl <bryanliles@gmail.com>
  • Loading branch information
bryanl committed Apr 29, 2020
1 parent 11411f0 commit 9b28b75
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions web/src/app/modules/shared/pipes/relative/relative.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();

Expand All @@ -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;
Expand All @@ -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;
}
}
}

0 comments on commit 9b28b75

Please sign in to comment.