2020-03-25 03:20:17 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useEffect, useRef } from '@wordpress/element';
|
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
|
|
|
|
const useInterval = ( callback, interval ) => {
|
|
|
|
const savedCallback = useRef();
|
2020-07-28 02:32:58 +00:00
|
|
|
useEffect( () => {
|
|
|
|
savedCallback.current = callback;
|
|
|
|
}, [ callback ] );
|
|
|
|
useEffect( () => {
|
|
|
|
const handler = ( ...args ) => savedCallback.current( ...args );
|
|
|
|
if ( interval !== null ) {
|
|
|
|
const id = setInterval( handler, interval );
|
|
|
|
return () => clearInterval( id );
|
|
|
|
}
|
|
|
|
}, [ interval ] );
|
2020-03-25 03:20:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const useSelectWithRefresh = (
|
|
|
|
mapSelectToProps,
|
|
|
|
invalidationCallback,
|
|
|
|
interval,
|
|
|
|
dependencies
|
|
|
|
) => {
|
|
|
|
const result = useSelect( mapSelectToProps, dependencies );
|
|
|
|
useInterval( invalidationCallback, interval );
|
|
|
|
return result;
|
|
|
|
};
|