@carbon/react
Version:
React components for the Carbon Design System
45 lines (43 loc) • 1.14 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
//#region src/internal/OptimizedResize.ts
/**
* Copyright IBM Corp. 2016, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const win = globalThis;
const OptimizedResize = (() => {
const callbacks = [];
let running = false;
const runCallbacks = () => {
callbacks.forEach((callback) => {
callback();
});
running = false;
};
const handleResize = () => {
if (!running) {
running = true;
win.requestAnimationFrame(runCallbacks);
}
};
const addCallback = (callback) => {
if (callbacks.indexOf(callback) < 0) callbacks.push(callback);
};
return { add: (callback) => {
if (!callbacks.length) win.addEventListener("resize", handleResize);
addCallback(callback);
return { remove: () => {
const index = callbacks.indexOf(callback);
if (index >= 0) callbacks.splice(index, 1);
} };
} };
})();
//#endregion
export { OptimizedResize };