Skip to content

Instantly share code, notes, and snippets.

@adolfovj
Forked from Klerith/debouncer.dart
Created October 1, 2021 08:29
Show Gist options
  • Select an option

  • Save adolfovj/f0f6ba6c9468a64c50b116c84c31c728 to your computer and use it in GitHub Desktop.

Select an option

Save adolfovj/f0f6ba6c9468a64c50b116c84c31c728 to your computer and use it in GitHub Desktop.

Revisions

  1. @Klerith Klerith renamed this gist Jul 2, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @Klerith Klerith created this gist Jul 2, 2021.
    26 changes: 26 additions & 0 deletions deobuncer.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    import 'dart:async';
    // Creditos
    // https://stackoverflow.com/a/52922130/7834829

    class Debouncer<T> {

    Debouncer({
    required this.duration,
    this.onValue
    });

    final Duration duration;

    void Function(T value)? onValue;

    T? _value;
    Timer? _timer;

    T get value => _value!;

    set value(T val) {
    _value = val;
    _timer?.cancel();
    _timer = Timer(duration, () => onValue!(_value!));
    }
    }