update method

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

Updates the stopwatch based on incoming messages.

Implementation

@override
(StopwatchModel, Cmd?) update(Msg msg) {
  switch (msg) {
    case StopwatchStartStopMsg():
      // Ignore messages for other stopwatch instances.
      if (msg.id != _id) {
        return (this, null);
      }
      return (copyWith(running: msg.running, tag: msg.tag), null);

    case StopwatchResetMsg():
      // Ignore messages for other stopwatch instances.
      if (msg.id != _id) {
        return (this, null);
      }
      return (copyWith(elapsed: Duration.zero, tag: msg.tag), null);

    case StopwatchTickMsg():
      // Ignore messages for other stopwatch instances.
      if (msg.id != _id || msg.tag != _tag) {
        return (this, null);
      }

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

      // Update elapsed time.
      final newTag = _tag + 1;
      return (
        copyWith(elapsed: _elapsed + interval, tag: newTag),
        _tickWithTag(newTag),
      );

    default:
      return (this, null);
  }
}