Outlook_Addin_LLM/node_modules/@fluentui/utilities/lib-commonjs/safeRequestAnimationFrame.js

30 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.safeRequestAnimationFrame = void 0;
var extendComponent_1 = require("./extendComponent");
/**
* Generates a function to be attached to a React component, which can be called
* as a replacement to RAF. 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 safeRequestAnimationFrame = function (component) {
var activeTimeouts;
return function (cb) {
if (!activeTimeouts) {
activeTimeouts = new Set();
(0, extendComponent_1.extendComponent)(component, {
componentWillUnmount: function () {
activeTimeouts.forEach(function (id) { return cancelAnimationFrame(id); });
},
});
}
var timeoutId = requestAnimationFrame(function () {
activeTimeouts.delete(timeoutId);
cb();
});
activeTimeouts.add(timeoutId);
};
};
exports.safeRequestAnimationFrame = safeRequestAnimationFrame;
//# sourceMappingURL=safeRequestAnimationFrame.js.map