Skip to content

Commit f3beee0

Browse files
committed
Newly-added raf-based timeouts now fall back to setTimeout for older browsers (eg IE9)
1 parent 741b156 commit f3beee0

File tree

4 files changed

+71
-15
lines changed

4 files changed

+71
-15
lines changed

source/Grid/Grid.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
OverscanIndicesGetter,
1414
Alignment
1515
} from "./types";
16+
import type { AnimationTimeoutId } from "../utils/requestAnimationTimeout";
1617

1718
import React from "react";
1819
import cn from "classnames";
@@ -301,7 +302,7 @@ export default class Grid extends React.PureComponent {
301302
_rowStartIndex: number;
302303
_rowStopIndex: number;
303304

304-
_disablePointerEventsTimeoutId: ?number;
305+
_disablePointerEventsTimeoutId: ?AnimationTimeoutId;
305306

306307
constructor(props: Props) {
307308
super(props);

source/Masonry/Masonry.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
cancelAnimationTimeout
88
} from "../utils/requestAnimationTimeout";
99

10+
import type { AnimationTimeoutId } from "../utils/requestAnimationTimeout";
11+
1012
const emptyObject = {};
1113

1214
/**
@@ -58,6 +60,7 @@ export default class Masonry extends PureComponent {
5860
tabIndex: 0
5961
};
6062

63+
_debounceResetIsScrollingId: AnimationTimeoutId;
6164
_invalidateOnUpdateStartIndex: ?number = null;
6265
_invalidateOnUpdateStopIndex: ?number = null;
6366
_positionCache: PositionCache = new PositionCache();

source/utils/animationFrame.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/** @flow */
2+
3+
type Callback = (timestamp: number) => void;
4+
type CancelAnimationFrame = (requestId: number) => void;
5+
type ClearTimeout = (timeoutId?: any) => void;
6+
type RequestAnimationFrame = (callback: Callback) => number;
7+
8+
// Properly handle server-side rendering.
9+
let win;
10+
if (typeof window !== "undefined") {
11+
win = window;
12+
} else if (typeof self !== "undefined") {
13+
win = self;
14+
} else {
15+
win = this;
16+
}
17+
18+
// requestAnimationFrame() shim by Paul Irish
19+
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
20+
const request =
21+
win.requestAnimationFrame ||
22+
win.webkitRequestAnimationFrame ||
23+
win.mozRequestAnimationFrame ||
24+
win.oRequestAnimationFrame ||
25+
win.msRequestAnimationFrame ||
26+
function(callback: Callback): RequestAnimationFrame {
27+
return (win: any).setTimeout(callback, 1000 / 60);
28+
};
29+
30+
const cancel =
31+
win.cancelAnimationFrame ||
32+
win.webkitCancelAnimationFrame ||
33+
win.mozCancelAnimationFrame ||
34+
win.oCancelAnimationFrame ||
35+
win.msCancelAnimationFrame ||
36+
function(id: number) {
37+
(win: any).clearTimeout(id);
38+
};
39+
40+
export const raf: RequestAnimationFrame = (request: any);
41+
export const caf: CancelAnimationFrame = (cancel: any);
Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
1-
export function cancelAnimationTimeout(frame) {
2-
window.cancelAnimationFrame(frame.id);
3-
}
1+
/** @flow */
2+
3+
import { caf, raf } from "./animationFrame";
4+
5+
export type AnimationTimeoutId = {
6+
id: number
7+
};
8+
9+
export const cancelAnimationTimeout = (frame: AnimationTimeoutId) =>
10+
caf(frame.id);
411

512
/**
6-
* Recursively calls requestAnimationFrame until a specified delay has been met
7-
* or exceeded. When the delay time has been reached the function you're timing
8-
* out will be called.
13+
* Recursively calls requestAnimationFrame until a specified delay has been met or exceeded.
14+
* When the delay time has been reached the function you're timing out will be called.
915
*
1016
* Credit: Joe Lambert (https://gist.github.com/joelambert/1002116#file-requesttimeout-js)
1117
*/
12-
export function requestAnimationTimeout(func, delay) {
18+
export const requestAnimationTimeout = (
19+
callback: Function,
20+
delay: number
21+
): AnimationTimeoutId => {
1322
const start = Date.now();
14-
const frame = new Object();
1523

16-
function timeout() {
24+
const timeout = () => {
1725
if (Date.now() - start >= delay) {
18-
func.call();
26+
callback.call();
1927
} else {
20-
frame.id = window.requestAnimationFrame(timeout);
28+
frame.id = raf(timeout);
2129
}
22-
}
30+
};
31+
32+
const frame: AnimationTimeoutId = {
33+
id: raf(timeout)
34+
};
2335

24-
frame.id = window.requestAnimationFrame(timeout);
2536
return frame;
26-
}
37+
};

0 commit comments

Comments
 (0)