Trait actix::prelude::AsyncContext
[−]
[src]
pub trait AsyncContext<A>: ActorContext + ToEnvelope<A> where
A: Actor<Context = Self>, { fn spawn<F>(&mut self, fut: F) -> SpawnHandle
where
F: ActorFuture<Item = (), Error = (), Actor = A> + 'static; fn wait<F>(&mut self, fut: F)
where
F: ActorFuture<Item = (), Error = (), Actor = A> + 'static; fn cancel_future(&mut self, handle: SpawnHandle) -> bool; fn address<Address>(&mut self) -> Address
where
A: ActorAddress<A, Address>, { ... } fn add_future<F>(&mut self, fut: F)
where
F: Future + 'static,
F::Item: ResponseType,
A: Handler<F::Item, F::Error>, { ... } fn add_stream<S>(&mut self, fut: S)
where
S: Stream + 'static,
S::Item: ResponseType,
A: Handler<S::Item, S::Error> + StreamHandler<S::Item, S::Error>, { ... } fn notify<M, E>(&mut self, msg: M, after: Duration) -> SpawnHandle
where
A: Handler<M, E>,
M: ResponseType + 'static,
E: 'static, { ... } fn run_later<F>(&mut self, dur: Duration, f: F) -> SpawnHandle
where
F: FnOnce(&mut A, &mut A::Context) + 'static, { ... } }
Asynchronous execution context
Required Methods
fn spawn<F>(&mut self, fut: F) -> SpawnHandle where
F: ActorFuture<Item = (), Error = (), Actor = A> + 'static,
F: ActorFuture<Item = (), Error = (), Actor = A> + 'static,
Spawn async future into context. Returns handle of the item, could be used for cancelling execution.
fn wait<F>(&mut self, fut: F) where
F: ActorFuture<Item = (), Error = (), Actor = A> + 'static,
F: ActorFuture<Item = (), Error = (), Actor = A> + 'static,
Spawn future into the context. Stop processing any of incoming events until this future resolves.
fn cancel_future(&mut self, handle: SpawnHandle) -> bool
Cancel future. idx is a value returned by spawn
method.
Provided Methods
fn address<Address>(&mut self) -> Address where
A: ActorAddress<A, Address>,
A: ActorAddress<A, Address>,
Get actor address
fn add_future<F>(&mut self, fut: F) where
F: Future + 'static,
F::Item: ResponseType,
A: Handler<F::Item, F::Error>,
F: Future + 'static,
F::Item: ResponseType,
A: Handler<F::Item, F::Error>,
This method allow to handle Future in similar way as normal actor messages.
extern crate actix; use std::time::Duration; use actix::prelude::*; // Message struct Ping; impl ResponseType for Ping { type Item = (); type Error = (); } struct MyActor; impl Handler<Ping, std::io::Error> for MyActor { fn error(&mut self, err: std::io::Error, ctx: &mut Context<MyActor>) { println!("Error: {}", err); } fn handle(&mut self, msg: Ping, ctx: &mut Context<MyActor>) -> Response<Self, Ping> { println!("PING"); Self::empty() } } impl Actor for MyActor { type Context = Context<Self>; fn started(&mut self, ctx: &mut Context<Self>) { // send `Ping` to self. ctx.notify(Ping, Duration::new(0, 1000)); } } fn main() {}
fn add_stream<S>(&mut self, fut: S) where
S: Stream + 'static,
S::Item: ResponseType,
A: Handler<S::Item, S::Error> + StreamHandler<S::Item, S::Error>,
S: Stream + 'static,
S::Item: ResponseType,
A: Handler<S::Item, S::Error> + StreamHandler<S::Item, S::Error>,
This method is similar to add_future
but works with streams.
Information to consider. Actor wont receive next item from a stream
until Response
future resolves to result. Self::reply
and
Self::reply_error
resolves immediately.
fn notify<M, E>(&mut self, msg: M, after: Duration) -> SpawnHandle where
A: Handler<M, E>,
M: ResponseType + 'static,
E: 'static,
A: Handler<M, E>,
M: ResponseType + 'static,
E: 'static,
Send message msg
to self after specified period of time. Returns spawn handle
which could be used for cancelation. Notification get cancelled
if context's stop method get called.
fn run_later<F>(&mut self, dur: Duration, f: F) -> SpawnHandle where
F: FnOnce(&mut A, &mut A::Context) + 'static,
F: FnOnce(&mut A, &mut A::Context) + 'static,
Execute closure after specified period of time within same Actor and Context Execution get cancelled if context's stop method get called.
Implementors
impl<A> AsyncContext<A> for Context<A> where
A: Actor<Context = Self>,impl<A> AsyncContext<A> for FramedContext<A> where
A: Actor<Context = Self> + FramedActor,
A: StreamHandler<<<A as FramedActor>::Codec as Decoder>::Item, <<A as FramedActor>::Codec as Decoder>::Error>,
<<A as FramedActor>::Codec as Decoder>::Item: ResponseType,