Skip to content

Commit

Permalink
URLInput: Use debounce() instead of throttle() (#26529)
Browse files Browse the repository at this point in the history
Wait until the user finishes typing before sending an AJAX request. This
ensures that there isn't an AJAX request sent every 200 ms while the
user is typing.
  • Loading branch information
noisysocks authored and talldan committed Oct 29, 2020
1 parent 82eb87f commit d3446be
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { render, unmountComponentAtNode } from 'react-dom';
import { act, Simulate } from 'react-dom/test-utils';
import { first, last, nth, uniqueId } from 'lodash';
import { default as lodash, first, last, nth, uniqueId } from 'lodash';
/**
* WordPress dependencies
*/
Expand All @@ -15,6 +15,12 @@ import { UP, DOWN, ENTER } from '@wordpress/keycodes';
import LinkControl from '../';
import { fauxEntitySuggestions, fetchFauxEntitySuggestions } from './fixtures';

// Mock debounce() so that it runs instantly.
lodash.debounce = jest.fn( ( callback ) => {
callback.cancel = jest.fn();
return callback;
} );

const mockFetchSearchSuggestions = jest.fn();

jest.mock( '@wordpress/data/src/components/use-select', () => () => ( {
Expand Down
4 changes: 2 additions & 2 deletions packages/block-editor/src/components/url-input/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { throttle, isFunction } from 'lodash';
import { debounce, isFunction } from 'lodash';
import classnames from 'classnames';
import scrollIntoView from 'dom-scroll-into-view';

Expand Down Expand Up @@ -39,7 +39,7 @@ class URLInput extends Component {
this.bindSuggestionNode = this.bindSuggestionNode.bind( this );
this.autocompleteRef = props.autocompleteRef || createRef();
this.inputRef = createRef();
this.updateSuggestions = throttle(
this.updateSuggestions = debounce(
this.updateSuggestions.bind( this ),
200
);
Expand Down

0 comments on commit d3446be

Please sign in to comment.