-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Timer class to wrap timeout operations like resume or pause
- Loading branch information
1 parent
ba37e72
commit 89f817d
Showing
3 changed files
with
71 additions
and
0 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
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,44 @@ | ||
import { Timer } from './timer' | ||
|
||
jest.useFakeTimers() | ||
|
||
describe('Timer', () => { | ||
it('should create a timer', () => { | ||
let done = false | ||
|
||
Timer.create(() => { | ||
done = true | ||
}, 1000) | ||
|
||
jest.runAllTimers() | ||
expect(done).toBe(true) | ||
}) | ||
|
||
it('should create a timer and then stop it', () => { | ||
let done = false | ||
|
||
const timer = Timer.create(() => { | ||
done = true | ||
}, 1000) | ||
timer.pause() | ||
jest.runAllTimers() | ||
|
||
expect(done).toBe(false) | ||
}) | ||
|
||
it('should create a timer, stop it and resume it', () => { | ||
let done = false | ||
|
||
const timer = Timer.create(() => { | ||
done = true | ||
}, 1000) | ||
jest.advanceTimersByTime(500) | ||
timer.pause() | ||
jest.advanceTimersByTime(1000) | ||
expect(done).toBe(false) | ||
timer.resume() | ||
jest.advanceTimersByTime(500) | ||
|
||
expect(done).toBe(true) | ||
}) | ||
}) |
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,26 @@ | ||
export class Timer { | ||
private timerId?: number | NodeJS.Timeout | ||
private start?: Date | ||
private remaining: number | ||
|
||
constructor(private readonly callback: () => void, delay: number) { | ||
this.remaining = delay | ||
} | ||
|
||
static create(callback: () => void, delay: number) { | ||
const timer = new Timer(callback, delay) | ||
timer.resume() | ||
return timer | ||
} | ||
|
||
resume() { | ||
this.start = new Date() | ||
clearTimeout(this.timerId as NodeJS.Timeout) | ||
this.timerId = setTimeout(this.callback, this.remaining) | ||
} | ||
|
||
pause() { | ||
clearTimeout(this.timerId as NodeJS.Timeout) | ||
this.remaining -= new Date().valueOf() - (this.start?.valueOf() ?? 0) | ||
} | ||
} |