-
-
Notifications
You must be signed in to change notification settings - Fork 747
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create tick in a more reactive style
- Loading branch information
1 parent
ca34f75
commit 91329fc
Showing
2 changed files
with
19 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,16 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
import { TimeTrackingService } from './time-tracking.service'; | ||
import { DialogIdleComponent } from './dialog-idle/dialog-idle.component'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
CommonModule | ||
], | ||
declarations: [], | ||
declarations: [DialogIdleComponent], | ||
providers: [ | ||
TimeTrackingService | ||
] | ||
}) | ||
export class TimeTrackingModule { | ||
constructor(private readonly _timeTrackingService: TimeTrackingService) { | ||
this._timeTrackingService.init(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,30 @@ | ||
import { Injectable } from '@angular/core'; | ||
import * as moment from 'moment'; | ||
import { TRACKING_INTERVAL, WORKLOG_DATE_STR_FORMAT } from '../../app.constants'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
import { interval, Observable } from 'rxjs'; | ||
import { map, share } from 'rxjs/operators'; | ||
import { Tick } from './time-tracking'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class TimeTrackingService { | ||
public tick$: BehaviorSubject<Tick> = new BehaviorSubject({ | ||
duration: 0, | ||
date: moment().format(WORKLOG_DATE_STR_FORMAT) | ||
}); | ||
private _currentTrackingStart: number; | ||
public tick$: Observable<Tick> = interval(TRACKING_INTERVAL).pipe( | ||
map(() => { | ||
const delta = Date.now() - this._currentTrackingStart; | ||
this._currentTrackingStart = Date.now(); | ||
return { | ||
duration: delta, | ||
date: moment().format(WORKLOG_DATE_STR_FORMAT) | ||
}; | ||
}), | ||
// important because we want the same interval for everyone | ||
share() | ||
); | ||
|
||
init() { | ||
this.initPoll(); | ||
} | ||
|
||
initPoll() { | ||
let currentTrackingStart = moment(); | ||
|
||
setInterval(() => { | ||
const now = moment(); | ||
const realPeriodDuration = moment.duration(now.diff(currentTrackingStart)) | ||
.asMilliseconds(); | ||
this.tick$.next({ | ||
duration: realPeriodDuration, | ||
date: now.format(WORKLOG_DATE_STR_FORMAT) | ||
}); | ||
// set to now | ||
currentTrackingStart = moment(); | ||
}, TRACKING_INTERVAL); | ||
constructor() { | ||
this._currentTrackingStart = Date.now(); | ||
} | ||
} |