-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
timerVariant
option to choose between native and worker timers (
#1818) * feat: option to choose between native and worker timers * feat: initialize timer once * fix: pingTimer test check correct timerId prop --------- Co-authored-by: Daniel Lando <[email protected]>
- Loading branch information
1 parent
50776a7
commit 547519d
Showing
8 changed files
with
91 additions
and
35 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
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,38 @@ | ||
import isBrowser, { isWebWorker } from './is-browser' | ||
import { clearTimeout as clearT, setTimeout as setT } from 'worker-timers' | ||
import type { TimerVariant } from './shared' | ||
|
||
// dont directly assign globals to class props otherwise this throws in web workers: Uncaught TypeError: Illegal invocation | ||
// See: https://stackoverflow.com/questions/9677985/uncaught-typeerror-illegal-invocation-in-chrome | ||
|
||
export interface Timer { | ||
set: typeof setT | ||
clear: typeof clearT | ||
} | ||
|
||
const workerTimer: Timer = { | ||
set: setT, | ||
clear: clearT, | ||
} | ||
|
||
const nativeTimer: Timer = { | ||
set: (func, time) => setTimeout(func, time), | ||
clear: (timerId) => clearTimeout(timerId), | ||
} | ||
|
||
const getTimer = (variant: TimerVariant): Timer => { | ||
switch (variant) { | ||
case 'native': { | ||
return nativeTimer | ||
} | ||
case 'worker': { | ||
return workerTimer | ||
} | ||
case 'auto': | ||
default: { | ||
return isBrowser && !isWebWorker ? workerTimer : nativeTimer | ||
} | ||
} | ||
} | ||
|
||
export default getTimer |
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 was deleted.
Oops, something went wrong.
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