FaridSafi/react-native-google-places-autocomplete

Bug Report - Debounce is just delaying requests, not really debouncing them

iuricernov opened this issue · 4 comments

Describe the bug

The debounce feature is just delaying the execution of the request code, and not eliminating the unnecessary requests.

For example, if I set a debounce interval and type "Los Angeles" quick enough on the field, it is doing 11 requests ("L", "Lo", "Los", ..., "Los Angeles") after an interval. The expected behavior for a debounced operation would be to do only one request ("Los Angeles") after the interval.

Reproduction - (required - issue will be closed without this)

To reproduce:

  • Add a GooglePlacesAutocomplete inside your app, with debounce of ~200ms
  • Use Flipper (or some alternative) to monitor the API calls done by the app
  • Type "Los Angeles" quick on the auto complete field
  • Notice that Flipper will show 11 calls to Google API: "L", "Lo", "Los", "Los ", "Los A", ...
  • The expected would be only one call with "Los Angeles" after the last letter was typed, and the debounce time was passed.

Additional context

  • Library Version: 2.5.1

  • React Native Version: not relevant

  • iOS

  • Android

  • Web

If you are using expo please indicate here:

  • I am using expo

Fix

This is happening because we are calling _.debounce many times with different contexts. I fixed it using setTimeout on my own fork:
https://github.com/iuricernov/react-native-google-places-autocomplete/tree/debounce-fix

The diff that fixes the issue:
master...iuricernov:react-native-google-places-autocomplete:debounce-fix

hi! i tested the fix, and work fine! thanks!