major refactoring

Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
Sahil Kumar
2021-05-27 19:59:34 +05:30
parent 99afb0ba8e
commit 839ced618b
106 changed files with 4263 additions and 1937 deletions
@@ -0,0 +1,51 @@
import 'dart:async';
import 'package:uuid/uuid.dart';
///
class TimerHelper {
final _uuid = const Uuid();
late final _timers = <String, Timer>{};
///
String setTimer(
Duration duration,
void Function() callback, {
bool immediate = false,
}) {
final id = _uuid.v1();
final timer = Timer(duration, callback);
if (immediate) callback();
_timers[id] = timer;
return id;
}
///
String setPeriodicTimer(
Duration duration,
void Function(Timer) callback, {
bool immediate = false,
}) {
final id = _uuid.v1();
final timer = Timer.periodic(duration, callback);
if (immediate) callback.call(timer);
_timers[id] = timer;
return id;
}
///
void cancelTimer(String id) {
final timer = _timers.remove(id);
return timer?.cancel();
}
///
void cancelAllTimers() {
for (final t in _timers.values) {
t.cancel();
}
_timers.clear();
}
///
bool get hasTimers => _timers.isNotEmpty;
}