UNPKG

@wordpress/compose

Version:
75 lines (73 loc) 2.49 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/compose/src/higher-order/with-safe-timeout/index.tsx var with_safe_timeout_exports = {}; __export(with_safe_timeout_exports, { default: () => with_safe_timeout_default }); module.exports = __toCommonJS(with_safe_timeout_exports); var import_element = require("@wordpress/element"); var import_create_higher_order_component = require("../../utils/create-higher-order-component"); var import_jsx_runtime = ( // @ts-ignore require("react/jsx-runtime") ); var withSafeTimeout = (0, import_create_higher_order_component.createHigherOrderComponent)( (OriginalComponent) => { return class WrappedComponent extends import_element.Component { timeouts; constructor(props) { super(props); this.timeouts = []; this.setTimeout = this.setTimeout.bind(this); this.clearTimeout = this.clearTimeout.bind(this); } componentWillUnmount() { this.timeouts.forEach(clearTimeout); } setTimeout(fn, delay) { const id = setTimeout(() => { fn(); this.clearTimeout(id); }, delay); this.timeouts.push(id); return id; } clearTimeout(id) { clearTimeout(id); this.timeouts = this.timeouts.filter( (timeoutId) => timeoutId !== id ); } render() { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( OriginalComponent, { ...this.props, setTimeout: this.setTimeout, clearTimeout: this.clearTimeout } ); } }; }, "withSafeTimeout" ); var with_safe_timeout_default = withSafeTimeout; //# sourceMappingURL=index.js.map