pub struct EventLoop<Ev> { /* private fields */ }
Expand description
Executes a task in background collecting events from other threads
Implementations§
Source§impl<Ev> EventLoop<Ev>where
Ev: Send + 'static,
impl<Ev> EventLoop<Ev>where
Ev: Send + 'static,
Sourcepub fn new() -> EventLoop<Ev>
pub 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"]);
});
Sourcepub fn on<F>(f: F) -> EventLoop<Ev>
pub fn on<F>(f: F) -> EventLoop<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);
});
Sourcepub fn on_managed<F>(f: F) -> EventLoop<Ev>
pub fn on_managed<F>(f: F) -> EventLoop<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), _=> () };
});
Sourcepub fn emit(&self, event: Ev) -> Result<(), Ev>
pub fn emit(&self, event: Ev) -> Result<(), Ev>
Triggers an event “Ev” once. Returns the same event if the event loop is not active.
Sourcepub fn emit_until<F>(&self, f: F)
pub fn emit_until<F>(&self, f: F)
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"]);
});
Sourcepub fn finish_in_ms(self, duration_ms: u32) -> EventLoop<Ev>
pub fn finish_in_ms(self, duration_ms: u32) -> EventLoop<Ev>
Finishes the event loop in N milliseconds
Sourcepub fn get_handler(&self) -> EventLoopHandler<Ev>
pub fn get_handler(&self) -> EventLoopHandler<Ev>
Returns a handler to Event Emitter
Sourcepub fn to_promise(self) -> Promise<Vec<Ev>, ()>
pub fn to_promise(self) -> Promise<Vec<Ev>, ()>
Once the event loop is finished, the promise collects the results.
Auto Trait Implementations§
impl<Ev> Freeze for EventLoop<Ev>
impl<Ev> RefUnwindSafe for EventLoop<Ev>
impl<Ev> Send for EventLoop<Ev>where
Ev: Send,
impl<Ev> !Sync for EventLoop<Ev>
impl<Ev> Unpin for EventLoop<Ev>
impl<Ev> UnwindSafe for EventLoop<Ev>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more