efemoney
06/15/2024, 10:34 AM// Handle commands
LaunchedEffect(state, player) {
coroutineScope {
val groups = PlayerCommandGroup.entries.map {
// Create a channel per group and launch a coroutine to consume commands from it.
// Within a command group, commands run sequentially.
Channel<PlayerCommand>(Channel.BUFFERED).also {
launch {
for (command in it) command.execute(player, state)
}
}
}
// Command groups, however, run parallel to each other.
// When a command is received, identify its group and send the command to group's channel.
for (command in state.commands) groups[command.group.ordinal].trySend(command)
}
}
Does this make sense?
Is there another way to say: as commands come into this channel send them into “different buckets” for processing. where within each bucket they are processed sequentially?kevin.cianfarini
06/15/2024, 12:55 PMentries.associateWith
versus using the ordinal as an implicit key.
Also, it seems strange that you're using trySend
versus send.kevin.cianfarini
06/15/2024, 12:56 PMcoroutineScope
since the receiver on the lambda in the LaunchedEffect is a scopeefemoney
06/15/2024, 12:59 PMefemoney
06/15/2024, 1:02 PMtrySend
I am trying to make the distinction that the iteration is synchronous. In this case its the same behavior as send
since the state.commands channel is buffered, but thats not clear at the call site.
send would have a slightly different connotation, ie: for each command, suspend until its “processed” (which could mean add to buffer & just return OR wait until destination handles it),
vs trySend which I understand as: for each command, just try to hand it over to the channelefemoney
06/15/2024, 3:26 PMkevin.cianfarini
06/15/2024, 3:37 PM