Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Replace requestIdleCallback with requestAnimationFrame for a more consistent DOM write cycle. #307

Merged
merged 3 commits into from
Aug 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 35 additions & 34 deletions src/HelmetUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,58 +248,59 @@ const reducePropsToState = propsList => ({
)
});

const requestIdleCallback = (() => {
if (
typeof window !== "undefined" &&
typeof window.requestIdleCallback !== "undefined"
) {
return window.requestIdleCallback;
}

return cb => {
const start = Date.now();
return setTimeout(() => {
cb({
didTimeout: false,
timeRemaining() {
return Math.max(0, 50 - (Date.now() - start));
}
});
}, 1);
const rafPolyfill = (() => {
let clock = Date.now();

return (callback: Function) => {
const currentTime = Date.now();

if (currentTime - clock > 16) {
clock = currentTime;
callback(currentTime);
} else {
setTimeout(() => {
rafPolyfill(callback);
}, 0);
}
};
})();

const cancelIdleCallback = (() => {
if (
typeof window !== "undefined" &&
typeof window.cancelIdleCallback !== "undefined"
) {
return window.cancelIdleCallback;
}
const cafPolyfill = (id: string | number) => clearTimeout(id);

return id => clearTimeout(id);
})();
const requestAnimationFrame = typeof window !== "undefined"
? window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
rafPolyfill
: global.requestAnimationFrame || rafPolyfill;

const cancelAnimationFrame = typeof window !== "undefined"
? window.cancelAnimationFrame ||
window.webkitCancelAnimationFrame ||
window.mozCancelAnimationFrame ||
cafPolyfill
: global.cancelAnimationFrame || cafPolyfill;

const warn = msg => {
return console && typeof console.warn === "function" && console.warn(msg);
};

let _helmetIdleCallback = null;
let _helmetCallback = null;

const handleClientStateChange = newState => {
if (_helmetIdleCallback) {
cancelIdleCallback(_helmetIdleCallback);
if (_helmetCallback) {
cancelAnimationFrame(_helmetCallback);
}

if (newState.defer) {
_helmetIdleCallback = requestIdleCallback(() => {
_helmetCallback = requestAnimationFrame(() => {
commitTagChanges(newState, () => {
_helmetIdleCallback = null;
_helmetCallback = null;
});
});
} else {
commitTagChanges(newState);
_helmetIdleCallback = null;
_helmetCallback = null;
}
};

Expand Down Expand Up @@ -637,5 +638,5 @@ export {convertReactPropstoHtmlAttributes};
export {handleClientStateChange};
export {mapStateOnServer};
export {reducePropsToState};
export {requestIdleCallback};
export {requestAnimationFrame};
export {warn};
Loading