Implemented and added usage of eventbus. Added a lot of UI interaction. Added a lot of DB work.
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
use crate::event::Event;
|
||||
use uuid::Uuid;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct Bus<T> {
|
||||
listeners: HashMap<Uuid, Box<dyn FnMut(&Uuid, T) -> bool + Send + Sync>>
|
||||
}
|
||||
|
||||
impl<T: Clone> Bus<T> {
|
||||
pub fn new() -> Self {
|
||||
Bus { listeners: HashMap::new() }
|
||||
}
|
||||
|
||||
pub fn register<F>(&mut self, mut closure: F) -> Uuid where F: FnMut(&Uuid, T) -> bool + Send + Sync + 'static {
|
||||
let uuid = Uuid::new_v4();
|
||||
self.listeners.insert(uuid.clone(), Box::new(closure));
|
||||
uuid
|
||||
}
|
||||
|
||||
pub fn unregister(&mut self, uuid: &Uuid) {
|
||||
self.listeners.remove(&uuid);
|
||||
}
|
||||
|
||||
pub fn post(&mut self, event: T) {
|
||||
self.listeners.retain(|uuid, closure| {
|
||||
closure(uuid, event.clone())
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
mod tests {
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::sync::atomic::{AtomicI32, Ordering};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::Bus;
|
||||
use crate::event::Event;
|
||||
|
||||
#[test]
|
||||
fn test1() {
|
||||
let mut string = Arc::new(Mutex::new(String::from("start")));
|
||||
let mut bus = Arc::new(Mutex::new(Bus::new()));
|
||||
let string_copy = string.clone();
|
||||
{
|
||||
bus.lock().unwrap().register(move |_uuid, e| {
|
||||
println!("Event {:?} received!", e);
|
||||
let mut copy = string_copy.lock().unwrap();
|
||||
copy.clear();
|
||||
copy.push_str("from thread");
|
||||
false
|
||||
});
|
||||
}
|
||||
let bus2 = bus.clone();
|
||||
thread::spawn(move || {
|
||||
bus2.lock().unwrap().post(Event::BlockchainChanged);
|
||||
});
|
||||
|
||||
let mut guard = string.lock().unwrap();
|
||||
thread::sleep(Duration::from_millis(100));
|
||||
println!("string = {}", &guard);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user