1
0
mirror of https://github.com/krawieck/lemmur/ synced 2024-12-25 23:53:27 +01:00
lemmur-app-android/lib/hooks/debounce.dart

52 lines
1.0 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'ref.dart';
class Debounce {
final bool loading;
final VoidCallback callback;
const Debounce({
required this.loading,
required this.callback,
});
void call() => callback();
}
/// will run `callback()` after debounce hook hasn't been called for the
/// specified `delayDuration`
Debounce useDebounce(
Future<void> Function() callback, [
Duration delayDuration = const Duration(seconds: 1),
]) {
final loading = useState(false);
final timerHandle = useRef<Timer?>(null);
cancel() {
timerHandle.current?.cancel();
loading.value = false;
}
useEffect(() => () => timerHandle.current?.cancel(), []);
start() {
timerHandle.current = Timer(delayDuration, () async {
loading.value = true;
await callback();
cancel();
});
}
return Debounce(
loading: loading.value,
callback: () {
cancel();
start();
},
);
}