Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to interrupt debounce #874

Open
subzero911 opened this issue Sep 30, 2022 · 2 comments
Open

Add a way to interrupt debounce #874

subzero911 opened this issue Sep 30, 2022 · 2 comments

Comments

@subzero911
Copy link
Contributor

subzero911 commented Sep 30, 2022

I have a city navigation app, to search for interesting places. I have categories (parks, cinemas, nightclubs, etc.) and a search textfield.
I enter the text and do a debounced search with reaction:

reaction((_) => store.searchText.value, (_) => _searchPlaces(query, category), delay: 500);

But what if I started typing and decided to change the category, and rapidly tapped to the another category chip?
In this case, I changed the category, but a search for previous category still has been executed.
I need a way to interrupt debounce.
Consider providing some API like

reactionDisposer.cancelScheduledReaction();
@amondnet
Copy link
Collaborator

amondnet commented Oct 12, 2022

@subzero911
I think _searchPlaces should handle that.

method 1. Shows only the results of the last request

DateTime _lastSearchTime;

Future<void> _searchPlaces() async {
  final searchTime = DateTIme.now();
  _lastSearchTime = searchTime;
  final result = await networkCall();
  if ( _lastSearchTime <= searchTime) {
    // show result
  }
};

method 2. cancel previous request

// use dio.
CancelToken? _token;
Future<void> _searchPlaces() {
  // Cancel if there is a previous request
  _token?.cancel('cancel');
  _token = CancelToken();
  dio.get(url, cancelToken: token)
     .catchError((DioError err){
      if (CancelToken.isCancel(err)) {
        print('Request canceled! '+ err.message)
      }else{
        // handle error.
      }
     });
};

@subzero911
Copy link
Contributor Author

Well, this looks reasonable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants