UNPKG

@wordpress/compose

Version:
54 lines (53 loc) 1.49 kB
// packages/compose/src/higher-order/with-safe-timeout/index.tsx import { Component } from "@wordpress/element"; import { createHigherOrderComponent } from "../../utils/create-higher-order-component"; import { jsx } from "react/jsx-runtime"; var withSafeTimeout = createHigherOrderComponent( (OriginalComponent) => { return class WrappedComponent extends 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 ( // @ts-ignore /* @__PURE__ */ jsx( OriginalComponent, { ...this.props, setTimeout: this.setTimeout, clearTimeout: this.clearTimeout } ) ); } }; }, "withSafeTimeout" ); var with_safe_timeout_default = withSafeTimeout; export { with_safe_timeout_default as default }; //# sourceMappingURL=index.js.map