minitask 0.1.1

MiniTask a simple wrapper for async tasks
docs.rs failed to build minitask-0.1.1
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build: minitask-0.1.2

Minitask

Small wrapper for spawning and observing async BackgroundTasks with a "messaging" interface.

The idea is to help with collecting multiple background task updates

use minitask::{BackgroundTask, BackgroundTasks, MessageBus, TaskUpdate};
use smol::stream::StreamExt;

struct Ping;

struct Pong;

struct PingPongTask;

impl BackgroundTask for PingPongTask {
  type Task = smol::Task<Result<(), async_channel::SendError<Pong>>>;

  type MessageIn = Ping;
  type MessageOut = Pong;

  fn run(self, message_bus: MessageBus<Self::MessageIn, Self::MessageOut>) -> Self::Task {
    smol::spawn(async move {
      while let Ok(message) = message_bus.recv().await {
        message_bus.send(Pong).await?;
      }
      Ok(())
    })
  }
}

let mut tasks = BackgroundTasks::default();

let ping_pong = tasks.register("ping-pong", PingPongTask);

smol::spawn(async move { ping_pong.send(Ping).await }).detach();

smol::block_on(async move {
  let first_update = tasks.next().await;
  assert!(matches!(
    first_update,
    Some(("ping-pong", TaskUpdate::Message(Pong)))
  ));

  let second_update = tasks.next().await;
  assert!(matches!(
    second_update,
    Some(("ping-pong", TaskUpdate::Finished(Ok(()))))
  ));
});