Skip to content

Instantly share code, notes, and snippets.

@huntc
Created February 7, 2024 03:59
Show Gist options
  • Save huntc/152e63e5d8fa8a180435ed0aafc77577 to your computer and use it in GitHub Desktop.
Save huntc/152e63e5d8fa8a180435ed0aafc77577 to your computer and use it in GitHub Desktop.
An example of creating a channel for sending commands to self.
let (self_commands, mut self_commands_receiver) = mpsc::unbounded_channel();
let (entity_manager_task, commands) = entity_manager::task(
Behavior {
self_commands,
},
file_log_topic_adapter,
MAX_COMMANDS,
MAX_ENTITIES,
);
let task_commands = commands.clone().downgrade();
let self_commands_forwarder_task = async move {
while let Some(message) = self_commands_receiver.recv().await {
if let Some(task_commands) = task_commands.upgrade() {
let _ = task_commands.send(message).await;
}
}
};
let handle =
tokio::spawn(async { tokio::join!(entity_manager_task, self_commands_forwarder_task).0 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment