Trait actix::AsyncContext [] [src]

pub trait AsyncContext<A>: ActorContext<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 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

Spawn async future into context. Returns handle of the item, could be used for cancelling execution.

Spawn future into the context. Stop processing any of incoming events until this future resolves.

Cancel future. idx is a value returned by spawn method.

Provided Methods

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() {}

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.

Send message msg to self after specified period of time. Returns spawn handle which could be used for cancelation.

Execute closure after specified period of time within same Actor and Context

Implementors