forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prebid Core: TTL counts only when page is active (prebid#11803)
* 11268 TTL counts only when page is active * refactor * refactor due to performance optimization * get rid of false timer id --------- Co-authored-by: Marcin Komorski <marcinkomorski@Marcins-MacBook-Pro.local>
- Loading branch information
Showing
4 changed files
with
118 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
let outOfFocusStart; | ||
let timeOutOfFocus = 0; | ||
let suspendedTimeouts = []; | ||
|
||
document.addEventListener('visibilitychange', () => { | ||
if (document.hidden) { | ||
outOfFocusStart = Date.now() | ||
} else { | ||
timeOutOfFocus += Date.now() - outOfFocusStart | ||
suspendedTimeouts.forEach(({ callback, startTime, setTimerId }) => setTimerId(setFocusTimeout(callback, timeOutOfFocus - startTime)())) | ||
outOfFocusStart = null; | ||
} | ||
}); | ||
|
||
/** | ||
* 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 | ||
*/ | ||
export default function setFocusTimeout(callback, milliseconds) { | ||
const startTime = timeOutOfFocus; | ||
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, | ||
setTimerId(newId) { | ||
timerId = newId; | ||
} | ||
}) | ||
} else { | ||
timerId = setFocusTimeout(callback, timeOutOfFocus - startTime)(); | ||
} | ||
}, milliseconds); | ||
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
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,60 @@ | ||
import setFocusTimeout from '../../../../src/utils/focusTimeout'; | ||
|
||
export const setDocumentHidden = (hidden) => { | ||
Object.defineProperty(document, 'hidden', { | ||
configurable: true, | ||
get: () => hidden, | ||
}); | ||
document.dispatchEvent(new Event('visibilitychange')); | ||
}; | ||
|
||
describe('focusTimeout', () => { | ||
let clock; | ||
|
||
beforeEach(() => { | ||
clock = sinon.useFakeTimers(); | ||
}); | ||
|
||
afterEach(() => { | ||
clock.restore(); | ||
}) | ||
|
||
it('should invoke callback when page is visible', () => { | ||
let callback = sinon.stub(); | ||
setFocusTimeout(callback, 2000); | ||
clock.tick(2000); | ||
expect(callback.called).to.be.true; | ||
}); | ||
|
||
it('should not invoke callback if page was hidden', () => { | ||
let callback = sinon.stub(); | ||
setFocusTimeout(callback, 2000); | ||
setDocumentHidden(true); | ||
clock.tick(3000); | ||
expect(callback.called).to.be.false; | ||
}); | ||
|
||
it('should defer callback execution when page is hidden', () => { | ||
let callback = sinon.stub(); | ||
setFocusTimeout(callback, 4000); | ||
clock.tick(2000); | ||
setDocumentHidden(true); | ||
clock.tick(2000); | ||
setDocumentHidden(false); | ||
expect(callback.called).to.be.false; | ||
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); | ||
}); | ||
}); |