-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: pause timer with multiple impressions (#71)
- Loading branch information
Showing
5 changed files
with
167 additions
and
46 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
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,4 +1,3 @@ | ||
export * from './timer'; | ||
export * from './emitter'; | ||
export * from './parser'; | ||
|
||
|
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,31 +1,34 @@ | ||
import { NotificationItem } from '@/types'; | ||
|
||
export type NotificationItemWithTimer = NotificationItem & { | ||
timer?: number; | ||
interface Timer { | ||
start: () => void; | ||
stop: () => void; | ||
} | ||
|
||
export class Timer { | ||
private start!: number; | ||
private remaining: number; | ||
private notifyItem: NotificationItemWithTimer; | ||
private callback: () => void; | ||
|
||
constructor(callback: () => void, delay: number, notifyItem: NotificationItemWithTimer) { | ||
this.remaining = delay; | ||
this.callback = callback; | ||
this.notifyItem = notifyItem; | ||
this.resume(); | ||
} | ||
|
||
pause(): void { | ||
clearTimeout(this.notifyItem.timer); | ||
this.remaining -= Date.now() - this.start; | ||
} | ||
|
||
resume(): void { | ||
this.start = Date.now(); | ||
clearTimeout(this.notifyItem.timer); | ||
// @ts-ignore FIXME Node.js timer type | ||
this.notifyItem.timer = setTimeout(this.callback, this.remaining); | ||
} | ||
|
||
export type NotificationItemWithTimer = NotificationItem & { | ||
timer?: Timer; | ||
} | ||
|
||
export const createTimer = (callback: () => void, delay: number): Timer => { | ||
let timer: number; | ||
let startTime: number; | ||
let remainingTime = delay; | ||
|
||
const start = () => { | ||
startTime = Date.now(); | ||
timer = setTimeout(callback, remainingTime); | ||
}; | ||
|
||
const stop = () => { | ||
clearTimeout(timer); | ||
remainingTime -= Date.now() - startTime; | ||
}; | ||
|
||
start(); | ||
|
||
return { | ||
start, | ||
stop, | ||
}; | ||
}; |
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