Skip to content

Commit

Permalink
Improve and refactor clearTimeoutRef
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Q committed Mar 23, 2020
1 parent f0d7072 commit 4194cd2
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions packages/block-editor/src/components/block-toolbar/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,21 @@ export function useDebouncedShowMovers( {
return ! isFocused && ! isHovered;
};

const clearTimeoutRef = () => {
const timeout = timeoutRef.current;

if ( timeout && clearTimeout ) {
clearTimeout( timeout );
}
};

const debouncedShowMovers = ( event ) => {
if ( event ) {
event.stopPropagation();
}

const timeout = timeoutRef.current;
clearTimeoutRef();

if ( timeout && clearTimeout ) {
clearTimeout( timeout );
}
if ( ! showMovers ) {
handleOnChange( true );
}
Expand All @@ -58,14 +63,16 @@ export function useDebouncedShowMovers( {
event.stopPropagation();
}

clearTimeoutRef();

timeoutRef.current = setTimeout( () => {
if ( shouldHideMovers() ) {
handleOnChange( false );
}
}, debounceTimeout );
};

useEffect( () => () => clearTimeout( timeoutRef.current ), [] );
useEffect( () => () => clearTimeoutRef(), [] );

return {
showMovers,
Expand Down

0 comments on commit 4194cd2

Please sign in to comment.