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

[Feature Request] Debounce states #382

Closed
matanshukry opened this issue Jun 28, 2019 · 5 comments
Closed

[Feature Request] Debounce states #382

matanshukry opened this issue Jun 28, 2019 · 5 comments
Assignees
Labels
enhancement New feature or request
Projects

Comments

@matanshukry
Copy link

matanshukry commented Jun 28, 2019

Is your feature request related to a problem? Please describe.
Imagine the following scenario:

  1. You have a search box where the user will type text, and the results will show automatically as the text changes
  2. The user will type the letter 'm'. then 'a'. then 't'. then 'a'. then 'n'.
  3. Only when the user will finish typing the entire word ('matan'), there will be a result.
  4. If there are no results, nothing is shown.
  5. While the request is made, a circular loading progress bar is shown.

What will usually happen in the above scenario, is that when the user will type the first 4 letters, the user will see a circular loading progress bar quickly, and it will then disappear.

Describe the solution you'd like
I would like to debounce the state. That is, if the state SearchStateLoading is shown for less than 500ms, I don't want it to change at all.
Something like this, possibly:

return BlocBuilder<SearchEvent, SearchState>(
      bloc: searchBloc,
      debounce: const Duration(milliseconds: 500),
      builder: (BuildContext context, SearchState state) {
        if (state is SearchStateHasResults) {
          return _buildResults(state.results);
        }
        if (state is AutoCompleteStateEmpty) {
          return Container();
        }
		return CircularProgressIndicator();
      },
    );

Describe alternatives you've considered
Unless there's a built-in way I'm unaware of, this feature can be implemented as a different widget. I just think it would be a nice addition to BlocBuilder, since it seems like a common use case for search widgets

Additional context

@matanshukry matanshukry changed the title [Feature Request / Example] Debounce states? [Feature Request] Debounce states Jun 28, 2019
@felangel felangel self-assigned this Jun 28, 2019
@felangel felangel added question Further information is requested enhancement candidate Candidate for enhancement but additional research is needed feedback wanted Looking for feedback from the community and removed question Further information is requested labels Jun 28, 2019
@felangel felangel added this to In progress in bloc Jun 28, 2019
@felangel felangel moved this from In progress to To do in bloc Jun 28, 2019
@felangel felangel pinned this issue Jun 29, 2019
@livingmine
Copy link

@matanshukry Can't this be achieved using something like the following example taken from the bloc's Github Search example?
return super.transform( (events as Observable<GithubSearchEvent>).debounceTime( Duration(milliseconds: 500), ), next, );

@felangel
Copy link
Owner

felangel commented Jul 31, 2019

Hi @matanshukry 👋
Thanks for opening an issue!

What are your thoughts on something like this instead:

BlocBuilder<CounterBloc, int>(
    transformer: (state) =>
        (state as Observable<int>).debounceTime(Duration(seconds: 1)),
    builder: (context, count) {
        return Center(
            child: Text(
                '$count',
                style: TextStyle(fontSize: 24.0),
            ),
        );
    },
),

This would allow for more flexibility like filtering the bloc state, debouncing, etc..

transformer would take the stream of states as input and be responsible for returning a stream of states as output which would be consumed by the builder. It would be optional and if omitted would default to the current behavior.

@felangel felangel added the waiting for response Waiting for follow up label Jul 31, 2019
@livingmine
Copy link

Hi @felangel , what's the difference between your suggestion above with the example from my comment above? Is it about the thing being debounced, yours is debouncing the state, and my comment's is debouncing the event?

@felangel
Copy link
Owner

felangel commented Aug 1, 2019

Hey @livingmine yup exactly 👍

@felangel felangel added enhancement New feature or request in progress and removed enhancement candidate Candidate for enhancement but additional research is needed feedback wanted Looking for feedback from the community waiting for response Waiting for follow up labels Aug 18, 2019
@felangel felangel moved this from To do to In progress in bloc Aug 18, 2019
@felangel
Copy link
Owner

published in bloc v0.15.0 and flutter_bloc v0.21.0 🎉

@felangel felangel moved this from In progress to Done in bloc Aug 20, 2019
@felangel felangel unpinned this issue Aug 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
bloc
  
Done
Development

No branches or pull requests

3 participants