Skip to content

Commit

Permalink
fix(utils): ensure utils exports are always bound to their parent obj…
Browse files Browse the repository at this point in the history
…ect to avoid illegal invocation type errors

Closes #17
  • Loading branch information
garthenweb committed Feb 16, 2020
1 parent 7cb888c commit 4149dcc
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,35 +162,39 @@ export const warnNoResizeObserver = () => {
type RequestAnimationFrameType = (callback: FrameRequestCallback) => number;

export const requestAnimationFrame = ((): RequestAnimationFrameType => {
const fallback = (callback: FrameRequestCallback) =>
(setTimeout(callback, 1000 / 60) as unknown) as number;
if (typeof window !== 'undefined') {
return (
const nativeRAF =
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
(<any>window).mozRequestAnimationFrame ||
fallback
);
(<any>window).mozRequestAnimationFrame;
if (nativeRAF) {
return nativeRAF.bind(window);
}
}
return fallback;
return function requestAnimationFrameFallback(
callback: FrameRequestCallback,
): number {
return (setTimeout(callback, 1000 / 60) as unknown) as number;
};
})();

export const cancelAnimationFrame = ((): ((handle: number) => void) => {
if (typeof window !== 'undefined') {
return (
const nativeCAF =
window.cancelAnimationFrame ||
window.webkitCancelAnimationFrame ||
(<any>window).webkitCancelRequestAnimationFrame ||
clearTimeout
);
(<any>window).webkitCancelRequestAnimationFrame;
if (nativeCAF) {
return nativeCAF.bind(window);
}
}
return clearTimeout;
})();

export const now =
typeof performance !== 'undefined' && performance.now
? performance.now.bind(performance)
: Date.now;
: Date.now.bind(Date);
export const createPerformanceMarker = () => {
const start = now();
return () => now() - start;
Expand Down

0 comments on commit 4149dcc

Please sign in to comment.