escamoteur / functional_listener

MIT License
62 stars 10 forks source link

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

Closed elias8 closed 3 years ago

elias8 commented 3 years ago

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
escamoteur commented 3 years ago

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'