UNPKG

1.03 kBJavaScriptView Raw
1import { extendComponent } from './extendComponent';
2/**
3 * Generates a function to be attached to a React component, which can be called
4 * as a replacement to RAF. In-flight async calls will be auto canceled if the component
5 * is unmounting before the async code is executed, preventing bugs where code
6 * accesses things within the component after being unmounted.
7 */
8export var safeRequestAnimationFrame = function (component) {
9 var activeTimeouts;
10 return function (cb) {
11 if (!activeTimeouts) {
12 activeTimeouts = new Set();
13 extendComponent(component, {
14 componentWillUnmount: function () {
15 activeTimeouts.forEach(function (id) { return cancelAnimationFrame(id); });
16 },
17 });
18 }
19 var timeoutId = requestAnimationFrame(function () {
20 activeTimeouts.delete(timeoutId);
21 cb();
22 });
23 activeTimeouts.add(timeoutId);
24 };
25};
26//# sourceMappingURL=safeRequestAnimationFrame.js.map
\No newline at end of file