31 lines
1.3 KiB
JavaScript
31 lines
1.3 KiB
JavaScript
define(["require", "exports", "./extendComponent"], function (require, exports, extendComponent_1) {
|
|
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.safeSetTimeout = void 0;
|
|
/**
|
|
* Generates a function to be attached to a React component, which can be called
|
|
* as a replacement to setTimeout. In-flight async calls will be auto canceled if the component
|
|
* is unmounting before the async code is executed, preventing bugs where code
|
|
* accesses things within the component after being unmounted.
|
|
*/
|
|
var safeSetTimeout = function (component) {
|
|
var activeTimeouts;
|
|
return function (cb, duration) {
|
|
if (!activeTimeouts) {
|
|
activeTimeouts = new Set();
|
|
(0, extendComponent_1.extendComponent)(component, {
|
|
componentWillUnmount: function () {
|
|
activeTimeouts.forEach(function (id) { return clearTimeout(id); });
|
|
},
|
|
});
|
|
}
|
|
var timeoutId = setTimeout(function () {
|
|
activeTimeouts.delete(timeoutId);
|
|
cb();
|
|
}, duration);
|
|
activeTimeouts.add(timeoutId);
|
|
};
|
|
};
|
|
exports.safeSetTimeout = safeSetTimeout;
|
|
});
|
|
//# sourceMappingURL=safeSetTimeout.js.map
|