Struct asynchronous::EventLoop [] [src]

pub struct EventLoop<Ev> {
    // some fields omitted
}

Executes a task in background collecting events from other threads

Methods

impl<Ev> EventLoop<Ev> where Ev: Send + 'static

fn new() -> EventLoop<Ev>

Creates a new Event Loop and collects all the events into a vector

use asynchronous::EventLoop;
 
let el = EventLoop::new().finish_in_ms(100);
el.emit("Event1");
el.emit("Event2");
// Do something here
el.to_promise().finally_sync(|res| {  // res: Result<Vec<Ev>,()>
    assert_eq!(res.unwrap(), vec!["Event1", "Event2"]);
});

fn on<F>(f: F) -> EventLoop<Ev> where F: Send + 'static + Fn(Ev)

Creates a new Event Loop and parses all the events produced

use asynchronous::EventLoop;
enum MiEvents { Hello(String), Goodbye(u32)}

let el = EventLoop::on(|event| {
   match event {
      MiEvents::Hello(s) => println!("Hello {}", s),
      MiEvents::Goodbye(v) => println!("Goodbye {}", v),
   }
});
el.emit(MiEvents::Hello("World".to_string()));
el.emit(MiEvents::Goodbye(3));
// Do something here
el.finish().to_promise().finally_sync(|res| {  // res: Result<Vec<Ev>,()>
    assert_eq!(res.unwrap().len(), 0);
});

fn on_managed<F>(f: F) -> EventLoop<Ev> where F: Send + 'static + Fn(Ev) -> Emit<Ev>

Creates a new Event Loop, parses all the events produced and collects what you want into a vector

use asynchronous::{EventLoop, Emit};
enum MiEvents { Hello(String), Goodbye(u32)}
 
let el = EventLoop::on_managed(|event| {
   match event {
      MiEvents::Hello(s) => { println!("Hello {}", s); Emit::Continue },
      MiEvents::Goodbye(v) => Emit::Event(MiEvents::Goodbye(v)),
   }
});
el.emit(MiEvents::Hello("World".to_string()));
el.emit(MiEvents::Goodbye(555));
// Do something here
el.finish().to_promise().finally_sync(|res| {  // res: Result<Vec<Ev>,()>
    let vec_res = res.unwrap();
    assert_eq!(vec_res.len(), 1);
    match vec_res[0] { MiEvents::Goodbye(vec_res) => assert_eq!(vec_res, 555), _=> () };
});

fn emit(&self, event: Ev) -> Result<(), Ev>

Triggers an event "Ev" once. Returns the same event if the event loop is not active.

fn emit_until<F>(&self, f: F) where F: Send + 'static + Fn() -> Emit<Ev>

Triggers an event "Ev" until the return of the "clousure" is None

use asynchronous::{EventLoop, Emit};
 
let el = EventLoop::new();
let x = std::sync::Arc::new(std::sync::Mutex::new(0));
el.emit_until(move || {
   let mut lock_x = x.lock().unwrap(); *lock_x += 1;
   if *lock_x <= 3 { Emit::Event("Event Test") } else { Emit::Stop }
});    
// Do something here
el.finish_in_ms(100).to_promise().finally_sync(|res| {  // res: Result<Vec<Ev>,()>
    assert_eq!(res.unwrap(), vec!["Event Test", "Event Test", "Event Test"]);
});

fn finish(self) -> EventLoop<Ev>

Finishes the event loop immediately.

fn finish_in_ms(self, duration_ms: u32) -> EventLoop<Ev>

Finishes the event loop in N milliseconds

fn is_active(&self) -> bool

Returns true if the event loop is running

fn get_handler(&self) -> EventLoopHandler<Ev>

Returns a handler to Event Emitter

fn to_promise(self) -> Promise<Vec<Ev>, ()>

Once the event loop is finished, the promise collects the results.