Skip to content

Commit

Permalink
get rid of false timer id
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Komorski authored and Marcin Komorski committed Jun 20, 2024
1 parent ccf7b14 commit 6b12f54
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
18 changes: 12 additions & 6 deletions src/utils/focusTimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ document.addEventListener('visibilitychange', () => {
outOfFocusStart = Date.now()
} else {
timeOutOfFocus += Date.now() - outOfFocusStart
suspendedTimeouts.forEach(({ callback, startTime }) => setFocusTimeout(callback, timeOutOfFocus - startTime))
suspendedTimeouts.forEach(({ callback, startTime, setTimerId }) => setTimerId(setFocusTimeout(callback, timeOutOfFocus - startTime)()))
outOfFocusStart = null;
}
});
Expand All @@ -17,19 +17,25 @@ document.addEventListener('visibilitychange', () => {
*
* @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 {number} - timer id
* @returns {function(*): (number)} - Getter function for current timer id
*/
export default function setFocusTimeout(callback, milliseconds) {
const startTime = timeOutOfFocus;
const timerId = setTimeout(() => {
let 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 })
suspendedTimeouts.push({
callback,
startTime,
setTimerId(newId) {
timerId = newId;
}
})
} else {
setFocusTimeout(callback, timeOutOfFocus - startTime);
timerId = setFocusTimeout(callback, timeOutOfFocus - startTime)();
}
}, milliseconds);
return timerId;
return () => timerId;
}
3 changes: 2 additions & 1 deletion src/utils/ttlCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function ttlCollection(
if (pendingPurge.length > 0) {
const now = timestamp();
nextPurge = Math.max(now, pendingPurge[0].expiry + slack);
task = setFocusTimeout(() => {
const getTaskId = setFocusTimeout(() => {
const now = timestamp();
let cnt = 0;
for (const entry of pendingPurge) {
Expand All @@ -66,6 +66,7 @@ export function ttlCollection(
task = null;
reschedulePurge();
}, nextPurge - now);
task = getTaskId();
} else {
task = null;
}
Expand Down
12 changes: 12 additions & 0 deletions test/spec/unit/utils/focusTimeout_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,16 @@ describe('focusTimeout', () => {
clock.tick(2000);
expect(callback.called).to.be.true;
});

it('should return updated timerId after page was showed again', () => {
let callback = sinon.stub();
const getCurrentTimerId = setFocusTimeout(callback, 4000);
const oldTimerId = getCurrentTimerId();
clock.tick(2000);
setDocumentHidden(true);
clock.tick(2000);
setDocumentHidden(false);
const newTimerId = getCurrentTimerId();
expect(oldTimerId).to.not.equal(newTimerId);
});
});

0 comments on commit 6b12f54

Please sign in to comment.