Struct asynchronous::EventLoop [] [src]

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

Executes a task in background collecting events from other threads

Methods

impl<Ev> EventLoop<Ev> where
    Ev: Send + 'static, 
[src]

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"]);
});

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);
});

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), _=> () };
});

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

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"]);
});

Finishes the event loop immediately.

Finishes the event loop in N milliseconds

Returns true if the event loop is running

Returns a handler to Event Emitter

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