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

Fixed failing debounce test and changed its example in the documentation #2

Closed

Conversation

elias8
Copy link

@elias8 elias8 commented Nov 28, 2020

Example code from the doc and test

final listenable = ValueNotifier<int>(0);
listenable
    .debounce(const Duration(milliseconds: 500))
    .listen((x, _) => print(x));

listenable.value = 42;
await Future.delayed(const Duration(milliseconds: 100));
listenable.value = 43;
await Future.delayed(const Duration(milliseconds: 100));
listenable.value = 44;
await Future.delayed(const Duration(milliseconds: 350));
listenable.value = 45;
await Future.delayed(const Duration(milliseconds: 550));
listenable.value = 46;
// will print out 42 ,45,46

But after the execution of the above code, only 45 should be printed out and it does too, because whenever we assign a new value to listenable.value it will be overriding the old value after the debounce time is ended. If we want to print all values, in this example 42, 43, 44, 45, and 46, we need to await for >= debounce time which is (500 in this case) after each assignment. So the code will be like

final listenable = ValueNotifier<int>(0);
listenable
    .debounce(const Duration(milliseconds: 500))
    .listen((x, _) => print(x));

listenable.value = 42;
await Future.delayed(const Duration(milliseconds: 500));
listenable.value = 43;
await Future.delayed(const Duration(milliseconds: 500));
listenable.value = 44;
await Future.delayed(const Duration(milliseconds: 500));
listenable.value = 45;
await Future.delayed(const Duration(milliseconds: 500));
listenable.value = 46;
// will print out 42, 43, 44, 45,46

@elias8 elias8 mentioned this pull request Nov 28, 2020
@escamoteur
Copy link
Owner

Thanks for pointing this out, but in this case the documentation was wrong. Strange that I didn't saw that because I ran the tests.
it really only should return '45'

@escamoteur escamoteur closed this Nov 30, 2020
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

Successfully merging this pull request may close these issues.

None yet

2 participants