Files
immich/mobile/lib/utils/hooks/timer_hook.dart
T
Thomas Way 0651612d46 fix(mobile): fix stale refs in use timer
The timer hook preserved the values of the original local variables,
which caused issues when hiding controls for videos. The callback can be
changed so that it always sees the latest value with useRef, and it can
be simplified significantly using a function rather than state class.
2026-04-05 04:03:32 +01:00

18 lines
434 B
Dart

import 'package:async/async.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
RestartableTimer useTimer(Duration duration, VoidCallback callback) {
final latest = useRef(callback);
latest.value = callback;
final timer = useMemoized(
() => RestartableTimer(duration, () => latest.value()),
[duration],
);
useEffect(() => timer.cancel, [timer]);
return timer;
}