-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor due to performance optimization
- Loading branch information
Marcin Komorski
authored and
Marcin Komorski
committed
Jun 19, 2024
1 parent
f93f84e
commit ccf7b14
Showing
3 changed files
with
25 additions
and
57 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 |
---|---|---|
@@ -1,54 +1,35 @@ | ||
let activeFocusTimeouts = []; | ||
let outOfFocusStart; | ||
let timeOutOfFocus = 0; | ||
let suspendedTimeouts = []; | ||
|
||
document.addEventListener('visibilitychange', () => { | ||
activeFocusTimeouts.forEach(({ pause, resume }) => document.hidden ? pause() : resume()) | ||
if (document.hidden) { | ||
outOfFocusStart = Date.now() | ||
} else { | ||
timeOutOfFocus += Date.now() - outOfFocusStart | ||
suspendedTimeouts.forEach(({ callback, startTime }) => setFocusTimeout(callback, timeOutOfFocus - startTime)) | ||
outOfFocusStart = null; | ||
} | ||
}); | ||
|
||
function removeFromActiveTimeouts(timerId) { | ||
activeFocusTimeouts = activeFocusTimeouts.filter(timeout => timeout.timerId !== timerId); | ||
} | ||
|
||
export function clearFocusTimeout(timerId) { | ||
clearTimeout(timerId); | ||
removeFromActiveTimeouts(timerId); | ||
} | ||
|
||
/** | ||
* Wraps native setTimeout function in order to count time only when page is focused | ||
* | ||
* @param {function(*): ()} [callback] - A function that will be invoked after passed time | ||
* @param {number} [milliseconds] - Minimum duration (in milliseconds) that the callback will be executed after | ||
* @returns {function(*): (number)} - Getter function for current timer id | ||
* @returns {number} - timer id | ||
*/ | ||
export default function setFocusTimeout(callback, milliseconds) { | ||
let timerId; | ||
|
||
timerId = setTimeout(() => { | ||
removeFromActiveTimeouts(timerId); | ||
callback(); | ||
const startTime = timeOutOfFocus; | ||
const timerId = setTimeout(() => { | ||
if (timeOutOfFocus === startTime && outOfFocusStart == null) { | ||
callback(); | ||
} else if (outOfFocusStart != null) { | ||
// case when timeout ended during page is out of focus | ||
suspendedTimeouts.push({ callback, startTime }) | ||
} else { | ||
setFocusTimeout(callback, timeOutOfFocus - startTime); | ||
} | ||
}, milliseconds); | ||
|
||
let timeLeft = milliseconds; | ||
let start = Date.now(); | ||
|
||
function pause() { | ||
if (!timerId) return; | ||
timeLeft -= Date.now() - start; | ||
clearTimeout(timerId); | ||
} | ||
|
||
function resume() { | ||
start = Date.now(); | ||
removeFromActiveTimeouts(timerId); | ||
const getCurrentTimerId = setFocusTimeout(callback, timeLeft); | ||
timerId = getCurrentTimerId(); | ||
} | ||
|
||
activeFocusTimeouts.push({ | ||
timerId, | ||
resume, | ||
pause | ||
}) | ||
|
||
return () => timerId; | ||
return timerId; | ||
} |
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