packageDocumentation module import * as React from "react" ; import { Timer } from "../utils/Timer" ; public export interface WithTimeoutProps { /** Timeout duration in milliseconds */ timeout : number ; /** Callback function for timeout */ onTimeout ? : ( ) => void ; } public export const withTimeout = < ComponentProps extends { } > ( Component ) => { return class WithTimeout extends React . PureComponent < ComponentProps & WithTimeoutProps > { public timer : Timer = new Timer ( 0 ) ; public override componentDidMount ( ) { this . timer . setOnExecute ( ( ) => this . props . onTimeout && this . props . onTimeout ( ) ) ; this . startTimer ( this . props . timeout ) ; } public override componentDidUpdate ( _prevProps ) { this . startTimer ( this . props . timeout ) ; } public override componentWillUnmount ( ) { this . timer . stop ( ) ; } public override render ( ) { const { timeout , onTimeout , ... props } = this . props ; return ( < Component { ... props as ComponentProps } { ... this . state } />); } public startTimer ( timeout ) { if ( this . timer . isRunning ) return ; this . timer . delay = timeout ; this . timer . start ( ) ; } } ; } ;