-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
108 additions
and
120 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import uuid from 'uuid'; | ||
|
||
/** | ||
* Keeps track on timers created with setTimeout to help clearTimeout | ||
* for all timers when no more delayed actions needed | ||
*/ | ||
export class TimerManager { | ||
private timers: any = {}; | ||
|
||
public setTimer(name: string, delay: number, fn: Function) { | ||
const id = uuid.v4(); | ||
const timer = setTimeout( | ||
timeoutId => { | ||
this.clearTimer(timeoutId); | ||
try { | ||
fn(); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}, | ||
delay, | ||
id, | ||
); | ||
|
||
// XXX only the timer is used, but the | ||
// other fields are useful for | ||
// troubleshooting/debugging | ||
this.timers[id] = { | ||
name, | ||
timer, | ||
}; | ||
|
||
return id; | ||
} | ||
|
||
public clearTimer(id: string) { | ||
const timers = this.timers; | ||
const timer = timers[id]; | ||
if (!timer) { | ||
return; | ||
} | ||
clearTimeout(timer.timer); | ||
delete timers[id]; | ||
} | ||
|
||
public clearAllTimers() { | ||
Object.keys(this.timers).forEach(key => { | ||
this.clearTimer(key); | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.