onStart method

  1. @override
Future<void> onStart(
  1. ZenithRef ref
)
override

Called when this service is registered with a ZenithRef.

Implementation

@override
Future<void> onStart(ZenithRef ref) async {
  _ref = ref;
  _timer?.cancel();
  _timer = Timer.periodic(interval, (_) {
    final currentRef = _ref;
    if (currentRef != null && currentRef.isMounted && _inFlight == null) {
      _inFlight = Future<void>.sync(() => task(currentRef))
          .then(
            (_) {
              lastError = null;
            },
            onError: (Object error, StackTrace _) {
              lastError = error;
            },
          )
          .whenComplete(() {
            _inFlight = null;
          });
    }
  });
}