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 tellthemachines committed Oct 30, 2020
1 parent d110989 commit e677f5a
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 @@ -4,7 +4,7 @@
import { render, unmountComponentAtNode } from 'react-dom';
import { act, Simulate } from 'react-dom/test-utils';
import { queryByText, queryByRole } from '@testing-library/react';
import { first, last, nth, uniqueId } from 'lodash';
import { default as lodash, first, last, nth, uniqueId } from 'lodash';
/**
* WordPress dependencies
*/
Expand All @@ -16,6 +16,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 @@ -40,7 +40,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 e677f5a

Please sign in to comment.