update method

  1. @override
(TimerModel, Cmd?) update(
  1. Msg msg
)
override

Updates the timer based on incoming messages.

Implementation

@override
(TimerModel, Cmd?) update(Msg msg) {
  switch (msg) {
    case TimerStartStopMsg():
      // Ignore messages for other timer instances.
      if (msg.tag != _tag) {
        return (this, null);
      }
      return (copyWith(running: msg.running), null);

    case TimerTickMsg():
      // Ignore messages for other timer instances.
      if (msg.tag != _tag) {
        return (this, null);
      }

      // If not running, ignore tick.
      if (!_running) {
        return (this, null);
      }

      // Check for timeout.
      if (msg.timeout) {
        return (copyWith(timeout: Duration.zero, running: false), null);
      }

      // Update timeout, ensuring it doesn't go negative.
      final newTimeout = timeout - interval;
      if (newTimeout <= Duration.zero) {
        return (
          copyWith(timeout: Duration.zero, running: false),
          _timedOut(),
        );
      }

      return (copyWith(timeout: newTimeout), _tick());

    default:
      return (this, null);
  }
}