1 | import { extendComponent } from './extendComponent';
|
2 | /**
|
3 | * Generates a function to be attached to a React component, which can be called
|
4 | * as a replacement to setTimeout. 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 | */
|
8 | export var safeSetTimeout = function (component) {
|
9 | var activeTimeouts;
|
10 | return function (cb, duration) {
|
11 | if (!activeTimeouts) {
|
12 | activeTimeouts = new Set();
|
13 | extendComponent(component, {
|
14 | componentWillUnmount: function () {
|
15 | activeTimeouts.forEach(function (id) { return clearTimeout(id); });
|
16 | },
|
17 | });
|
18 | }
|
19 | var timeoutId = setTimeout(function () {
|
20 | activeTimeouts.delete(timeoutId);
|
21 | cb();
|
22 | }, duration);
|
23 | activeTimeouts.add(timeoutId);
|
24 | };
|
25 | };
|
26 | //# sourceMappingURL=safeSetTimeout.js.map |
\ | No newline at end of file |