pub async fn join<A, F, R>(mailbox: &Mailbox<A>, actor: &mut A, fut: F) -> RExpand description
Joins on a future by handling all incoming messages whilst polling it. The future will
always be polled to completion, even if the actor is stopped. If the actor is stopped,
handling of messages will cease, and only the future will be polled. It is somewhat
analagous to futures::join, but it will not wait for the incoming stream of messages
from addresses to end before returning - it will return as soon as the provided future does.
ยงExample
use futures_util::FutureExt;
struct Stop;
struct Joining;
impl Handler<Stop> for MyActor {
type Return = ();
async fn handle(&mut self, _msg: Stop, ctx: &mut Context<Self>) {
ctx.stop_self();
}
}
impl Handler<Joining> for MyActor {
type Return = bool;
async fn handle(&mut self, _msg: Joining, ctx: &mut Context<Self>) -> bool {
let addr = ctx.mailbox().address();
let join = xtra::join(ctx.mailbox(), self, future::ready::<()>(()));
let _ = addr.send(Stop).detach().await;
// Actor is stopping, but the join should still evaluate correctly
join.now_or_never().is_some()
}
}
smol::block_on(async {
let addr = xtra::spawn_smol(MyActor, Mailbox::unbounded());
assert!(addr.is_connected());
assert_eq!(addr.send(Joining).await, Ok(true)); // Assert that the join did evaluate the future
})