Trait actix_web::actix::dev::AsyncContext
source · pub trait AsyncContext<A>: ActorContextwhere
A: Actor<Context = Self>,{
fn address(&self) -> Addr<A>;
fn spawn<F>(&mut self, fut: F) -> SpawnHandle
where
F: 'static + ActorFuture<Item = (), Error = (), Actor = A>;
fn wait<F>(&mut self, fut: F)
where
F: 'static + ActorFuture<Item = (), Error = (), Actor = A>;
fn waiting(&self) -> bool;
fn cancel_future(&mut self, handle: SpawnHandle) -> bool;
fn add_stream<S>(&mut self, fut: S) -> SpawnHandle
where
S: 'static + Stream,
A: StreamHandler<<S as Stream>::Item, <S as Stream>::Error>,
{ ... }
fn add_message_stream<S>(&mut self, fut: S)
where
S: 'static + Stream<Error = ()>,
<S as Stream>::Item: Message,
A: Handler<<S as Stream>::Item>,
{ ... }
fn notify<M>(&mut self, msg: M)
where
A: Handler<M>,
M: 'static + Message,
{ ... }
fn notify_later<M>(&mut self, msg: M, after: Duration) -> SpawnHandle
where
A: Handler<M>,
M: 'static + Message,
{ ... }
fn run_later<F>(&mut self, dur: Duration, f: F) -> SpawnHandle
where
F: 'static + FnOnce(&mut A, &mut <A as Actor>::Context),
{ ... }
fn run_interval<F>(&mut self, dur: Duration, f: F) -> SpawnHandle
where
F: 'static + FnMut(&mut A, &mut <A as Actor>::Context),
{ ... }
}
Expand description
Asynchronous execution context
Required Methods
sourcefn spawn<F>(&mut self, fut: F) -> SpawnHandlewhere
F: 'static + ActorFuture<Item = (), Error = (), Actor = A>,
fn spawn<F>(&mut self, fut: F) -> SpawnHandlewhere
F: 'static + ActorFuture<Item = (), Error = (), Actor = A>,
Spawn async future into context. Returns handle of the item, could be used for cancelling execution.
All futures cancel during actor stopping stage.
sourcefn wait<F>(&mut self, fut: F)where
F: 'static + ActorFuture<Item = (), Error = (), Actor = A>,
fn wait<F>(&mut self, fut: F)where
F: 'static + ActorFuture<Item = (), Error = (), Actor = A>,
Spawn future into the context. Stop processing any of incoming events until this future resolves.
sourcefn waiting(&self) -> bool
fn waiting(&self) -> bool
Check if context is paused (waiting for future completion or stopping)
sourcefn cancel_future(&mut self, handle: SpawnHandle) -> bool
fn cancel_future(&mut self, handle: SpawnHandle) -> bool
Cancel future. handle is a value returned by spawn
method.
Provided Methods
sourcefn add_stream<S>(&mut self, fut: S) -> SpawnHandlewhere
S: 'static + Stream,
A: StreamHandler<<S as Stream>::Item, <S as Stream>::Error>,
fn add_stream<S>(&mut self, fut: S) -> SpawnHandlewhere
S: 'static + Stream,
A: StreamHandler<<S as Stream>::Item, <S as Stream>::Error>,
This method register stream to an actor context and
allows to handle Stream
in similar way as normal actor messages.
use actix::prelude::*;
use futures::stream::once;
#[derive(Message)]
struct Ping;
struct MyActor;
impl StreamHandler<Ping, io::Error> for MyActor {
fn handle(&mut self, item: Ping, ctx: &mut Context<MyActor>) {
println!("PING");
}
fn finished(&mut self, ctx: &mut Self::Context) {
println!("finished");
}
}
impl Actor for MyActor {
type Context = Context<Self>;
fn started(&mut self, ctx: &mut Context<Self>) {
// add stream
ctx.add_stream(once::<Ping, io::Error>(Ok(Ping)));
}
}
sourcefn add_message_stream<S>(&mut self, fut: S)where
S: 'static + Stream<Error = ()>,
<S as Stream>::Item: Message,
A: Handler<<S as Stream>::Item>,
fn add_message_stream<S>(&mut self, fut: S)where
S: 'static + Stream<Error = ()>,
<S as Stream>::Item: Message,
A: Handler<<S as Stream>::Item>,
This method is similar to add_stream
but it skips stream errors.
use actix::prelude::*;
use futures::stream::once;
#[derive(Message)]
struct Ping;
struct MyActor;
impl Handler<Ping> for MyActor {
type Result = ();
fn handle(&mut self, msg: Ping, ctx: &mut Context<MyActor>) {
println!("PING");
}
}
impl Actor for MyActor {
type Context = Context<Self>;
fn started(&mut self, ctx: &mut Context<Self>) {
// add messages stream
ctx.add_message_stream(once(Ok(Ping)));
}
}
sourcefn notify<M>(&mut self, msg: M)where
A: Handler<M>,
M: 'static + Message,
fn notify<M>(&mut self, msg: M)where
A: Handler<M>,
M: 'static + Message,
Send message msg
to self.
sourcefn notify_later<M>(&mut self, msg: M, after: Duration) -> SpawnHandlewhere
A: Handler<M>,
M: 'static + Message,
fn notify_later<M>(&mut self, msg: M, after: Duration) -> SpawnHandlewhere
A: Handler<M>,
M: 'static + Message,
Send message msg
to self after specified period of time. Returns
spawn handle which could be used for cancellation. Notification get
cancelled if context’s stop method get called.
sourcefn run_later<F>(&mut self, dur: Duration, f: F) -> SpawnHandlewhere
F: 'static + FnOnce(&mut A, &mut <A as Actor>::Context),
fn run_later<F>(&mut self, dur: Duration, f: F) -> SpawnHandlewhere
F: 'static + FnOnce(&mut A, &mut <A as Actor>::Context),
Execute closure after specified period of time within same Actor and Context. Execution get cancelled if context’s stop method get called.
sourcefn run_interval<F>(&mut self, dur: Duration, f: F) -> SpawnHandlewhere
F: 'static + FnMut(&mut A, &mut <A as Actor>::Context),
fn run_interval<F>(&mut self, dur: Duration, f: F) -> SpawnHandlewhere
F: 'static + FnMut(&mut A, &mut <A as Actor>::Context),
Spawns a job to execute the given closure periodically, at a specified fixed interval