batch static method

Cmd batch(
  1. List<Cmd> commands
)

A command that runs multiple commands concurrently.

All commands start executing immediately. Messages are collected and sent as they complete.

return (newModel, Cmd.batch([
  fetchUsers(),
  fetchPosts(),
  Cmd.tick(Duration(seconds: 5), (t) => TimeoutMsg()),
]));

Implementation

static Cmd batch(List<Cmd> commands) {
  if (commands.isEmpty) return none();
  if (commands.length == 1) return commands.first;

  return Cmd(() async {
    final futures = commands.map((cmd) => cmd.execute());
    final results = await Future.wait(futures);
    final messages = results.whereType<Msg>().toList();

    if (messages.isEmpty) return null;
    if (messages.length == 1) return messages.first;
    return BatchMsg(messages);
  });
}