-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from charlesribeiro/feat/create-timer-service
create timer service, add @ngneat/until-destroy
- Loading branch information
Showing
8 changed files
with
128 additions
and
3 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 |
---|---|---|
|
@@ -113,6 +113,7 @@ | |
"cli": { | ||
"schematicCollections": [ | ||
"@angular-eslint/schematics" | ||
] | ||
], | ||
"analytics": false | ||
} | ||
} |
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { TestBed } from "@angular/core/testing"; | ||
import { take } from "rxjs/operators"; | ||
import { TimerService } from "./timer.service"; | ||
|
||
describe("TimerService", () => { | ||
let service: TimerService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
TimerService, | ||
], | ||
}); | ||
|
||
service = TestBed.inject(TimerService); | ||
}); | ||
|
||
it("should be created", () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
it("should start with a paused timer", (done) => { | ||
service.timerPaused$.pipe(take(1)).subscribe((isPaused) => { | ||
expect(isPaused).toBeTruthy(); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("should start timer and update the current time", (done) => { | ||
const initialTime = 10; | ||
service.startTimer(initialTime); | ||
|
||
service.currentTimer$.pipe(take(1)).subscribe((time) => { | ||
expect(time).toBe(initialTime + 1); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("should toggle the timer state", (done) => { | ||
service.startTimer(0); | ||
service.toggleTimer(); | ||
|
||
service.timerPaused$.pipe(take(1)).subscribe((isPaused) => { | ||
expect(isPaused).toBeTruthy(); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("should indicate if the timer is active", () => { | ||
service.startTimer(0); | ||
expect(service.isTimerActive).toBeTruthy(); | ||
|
||
service.toggleTimer(); | ||
expect(service.isTimerActive).toBeFalsy(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Injectable } from "@angular/core"; | ||
import { BehaviorSubject, EMPTY, interval, Observable } from "rxjs"; | ||
import { map, switchMap } from "rxjs/operators"; | ||
import { UntilDestroy, untilDestroyed } from "@ngneat/until-destroy"; | ||
|
||
@UntilDestroy() | ||
@Injectable() | ||
export class TimerService { | ||
private timer$: Observable<number>; | ||
|
||
private timerState$ = new BehaviorSubject<boolean>(true); | ||
|
||
private currentTime = 0; | ||
|
||
private isTimerRunning = false; | ||
|
||
constructor() { | ||
this.timer$ = this.timerState$.pipe( | ||
switchMap((isPaused) => | ||
isPaused ? EMPTY : interval(1000).pipe(map(() => this.currentTime++)), | ||
), | ||
); | ||
|
||
this.timerState$ | ||
.pipe(untilDestroyed(this)) | ||
.subscribe((isPaused) => (this.isTimerRunning = !isPaused)); | ||
} | ||
|
||
get isTimerActive(): boolean { | ||
return this.isTimerRunning; | ||
} | ||
|
||
get currentTimer$(): Observable<number> { | ||
return this.timer$; | ||
} | ||
|
||
get timerPaused$(): Observable<boolean> { | ||
return this.timerState$; | ||
} | ||
|
||
startTimer(initialTime: number): void { | ||
this.currentTime = (initialTime ?? 0) + 1; | ||
this.timerState$.next(false); | ||
} | ||
|
||
toggleTimer(): void { | ||
this.timerState$.next(this.isTimerRunning); | ||
} | ||
} |
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