mirror of https://github.com/Revertron/Alfis
Implemented and added usage of eventbus. Added a lot of UI interaction. Added a lot of DB work.
parent
9e6a01847e
commit
562c004b0d
@ -0,0 +1,10 @@
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub enum Event {
|
||||
MinerStarted,
|
||||
MinerStopped,
|
||||
KeyGeneratorStarted,
|
||||
KeyGeneratorStopped,
|
||||
NewBlockReceived,
|
||||
BlockchainChanged,
|
||||
ActionStopMining,
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
.mining_indicator {
|
||||
position:absolute;
|
||||
right:10px;
|
||||
width:30px;
|
||||
height:30px;
|
||||
display:inline-block;
|
||||
padding:0px;
|
||||
text-align:right;
|
||||
float:right;
|
||||
z-index: 10;
|
||||
}
|
||||
.mining_indicator span {
|
||||
position:absolute;
|
||||
display:inline-block;
|
||||
width:30px;
|
||||
height:30px;
|
||||
border-radius:100%;
|
||||
background:#3273dc;
|
||||
-webkit-animation:mining_indicator 1.6s linear infinite;
|
||||
animation:mining_indicator 1.6s linear infinite;
|
||||
}
|
||||
.mining_indicator span:last-child {
|
||||
animation-delay:-0.8s;
|
||||
-webkit-animation-delay:-0.8s;
|
||||
}
|
||||
@keyframes mining_indicator {
|
||||
0% {transform: scale(0, 0);opacity:0.5;}
|
||||
100% {transform: scale(1, 1);opacity:0;}
|
||||
}
|
||||
@-webkit-keyframes mining_indicator {
|
||||
0% {-webkit-transform: scale(0, 0);opacity:0.5;}
|
||||
100% {-webkit-transform: scale(1, 1);opacity:0;}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue