Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New hook: useSafeTimeout #19109

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 13 additions & 51 deletions packages/compose/src/higher-order/with-safe-timeout/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
/**
* External dependencies
*/
import { without } from 'lodash';

/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';

/**
* Internal dependencies
*/
import createHigherOrderComponent from '../../utils/create-higher-order-component';
import useSafeTimeout from '../../hooks/use-safe-timeout';

/**
* A higher-order component used to provide and manage delayed function calls
Expand All @@ -21,46 +12,17 @@ import createHigherOrderComponent from '../../utils/create-higher-order-componen
*
* @return {WPComponent} Wrapped component.
*/
const withSafeTimeout = createHigherOrderComponent(
( OriginalComponent ) => {
return class WrappedComponent extends Component {
constructor() {
super( ...arguments );
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 = without( this.timeouts, id );
}

render() {
return (
<OriginalComponent
{ ...this.props }
setTimeout={ this.setTimeout }
clearTimeout={ this.clearTimeout }
/>
);
}
};
},
'withSafeTimeout'
);
const withSafeTimeout = createHigherOrderComponent( ( OriginalComponent ) => {
return ( props ) => {
const { setTimeout, clearTimeout } = useSafeTimeout();
return (
<OriginalComponent
{ ...props }
setTimeout={ setTimeout }
clearTimeout={ clearTimeout }
/>
);
};
}, 'withSafeTimeout' );

export default withSafeTimeout;
36 changes: 36 additions & 0 deletions packages/compose/src/hooks/use-safe-timeout/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* External dependencies
*/
import { without } from 'lodash';

/**
* WordPress dependencies
*/
import { useRef, useEffect } from '@wordpress/element';

export default function useSafeTimeout() {
const timeouts = useRef( [] );

function clearTimeout( id ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use useCallback for these?

clearTimeout( id );
timeouts.current = without( timeouts.current, id );
}

function setTimeout( fn, delay ) {
const id = setTimeout( () => {
fn();
clearTimeout( id );
}, delay );
timeouts.current.push( id );
return id;
}

useEffect( () => () => {
timeouts.current.forEach( clearTimeout );
}, [] );

return {
clearTimeout,
setTimeout,
};
}