this repo has no description
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

Created a runtime that allows systems to register themselves with it, and verified that the thing compiles to WASM

+131 -71
+3
Cargo.toml
··· 9 9 10 10 [dependencies] 11 11 rmp = "0.8.15" 12 + rmp-serde = "1.3.1" 12 13 rmpv = { version = "1.3.1", features = ["serde"] } 14 + serde = { version = "1.0.228", features = ["derive"] } 15 + uuid = { version = "1.22.0", features = ["v4", "js"] } 13 16 wasm-bindgen = "0.2.114"
+86
src/hooks.rs
··· 1 + use std::collections::HashMap; 2 + use wasm_bindgen::prelude::*; 3 + use rmpv::Value; 4 + 5 + #[wasm_bindgen] 6 + pub struct DiceWire { 7 + hooks: Hooks 8 + } 9 + 10 + impl DiceWire { 11 + pub fn new() -> Self { 12 + Self { 13 + hooks: Hooks::new() 14 + } 15 + } 16 + } 17 + 18 + #[wasm_bindgen] 19 + pub struct Hooks { 20 + events: HashMap<String, Vec<Hook>> 21 + } 22 + 23 + impl Hooks { 24 + pub fn new() -> Self { 25 + Self { 26 + events: HashMap::new() 27 + } 28 + } 29 + 30 + pub fn on<F>(&mut self, event: String, callback: F) 31 + where 32 + F: Fn(&Value) + 'static + Send + Sync 33 + { 34 + self.events.entry(event) 35 + .or_insert_with(Vec::new) 36 + .push(Hook::new(callback, false)); 37 + } 38 + 39 + pub fn once<F>(&mut self, event: String, callback: F) 40 + where 41 + F: Fn(&Value) + 'static + Send + Sync 42 + { 43 + self.events.entry(event) 44 + .or_insert_with(Vec::new) 45 + .push(Hook::new(callback, true)); 46 + } 47 + 48 + pub fn emit(&mut self, event: String, data: &Value) { 49 + if let Some(event_list) = self.events.get_mut(&event) { 50 + for event in event_list.iter_mut() { 51 + event.call(data); 52 + } 53 + } 54 + } 55 + } 56 + 57 + #[wasm_bindgen] 58 + pub struct Hook { 59 + func: Box<dyn Fn(&Value) -> () + Sync + Send>, 60 + once: bool, 61 + called: bool 62 + } 63 + 64 + impl Hook { 65 + pub fn new<F>(callback: F, once: bool) -> Self 66 + where 67 + F: Fn(&Value) -> () + 'static + Send + Sync 68 + { 69 + Self { 70 + func: Box::new(callback), 71 + once, 72 + called: false 73 + } 74 + } 75 + 76 + pub fn call(&mut self, data: &Value) { 77 + if self.once { 78 + if !self.called { 79 + (self.func)(data); 80 + self.called = true; 81 + } 82 + } else { 83 + (self.func)(data); 84 + } 85 + } 86 + }
+28 -71
src/lib.rs
··· 1 - use std::collections::HashMap; 1 + pub mod hooks; 2 + pub mod traits; 3 + 4 + use hooks::Hooks; 5 + use traits::DiceWireSystem; 6 + use uuid::Uuid; 7 + use std::{collections::HashMap, str::FromStr, sync::{Mutex, OnceLock}}; 2 8 use wasm_bindgen::prelude::*; 3 - use rmpv::Value; 4 9 5 - #[wasm_bindgen] 6 - pub struct DiceWire { 7 - hooks: Hooks 8 - } 10 + static RUNTIME: OnceLock<Mutex<Runtime>> = OnceLock::new(); 9 11 10 - impl DiceWire { 11 - pub fn new() -> Self { 12 - Self { 13 - hooks: Hooks::new() 14 - } 15 - } 12 + fn get_runtime() -> &'static Mutex<Runtime> { 13 + RUNTIME.get_or_init(|| Mutex::new(Runtime::new())) 16 14 } 17 15 18 - #[wasm_bindgen] 19 - pub struct Hooks { 20 - events: HashMap<String, Vec<Hook>> 16 + pub struct Runtime { 17 + pub systems: HashMap<String, Box<dyn DiceWireSystem>>, 18 + hooks: Hooks 21 19 } 22 20 23 - impl Hooks { 24 - pub fn new() -> Self { 21 + impl Runtime { 22 + fn new() -> Self { 25 23 Self { 26 - events: HashMap::new() 27 - } 28 - } 29 - 30 - pub fn on<F>(&mut self, event: String, callback: F) 31 - where 32 - F: Fn(&Value) + 'static + Send + Sync 33 - { 34 - self.events.entry(event) 35 - .or_insert_with(Vec::new) 36 - .push(Hook::new(callback, false)); 37 - } 38 - 39 - pub fn once<F>(&mut self, event: String, callback: F) 40 - where 41 - F: Fn(&Value) + 'static + Send + Sync 42 - { 43 - self.events.entry(event) 44 - .or_insert_with(Vec::new) 45 - .push(Hook::new(callback, true)); 46 - } 47 - 48 - pub fn emit(&mut self, event: String, data: &Value) { 49 - if let Some(event_list) = self.events.get_mut(&event) { 50 - for event in event_list.iter_mut() { 51 - event.call(data); 52 - } 24 + systems: HashMap::new(), 25 + hooks: Hooks::new() 53 26 } 54 27 } 55 28 } 56 29 57 30 #[wasm_bindgen] 58 - pub struct Hook { 59 - func: Box<dyn Fn(&Value) -> () + Sync + Send>, 60 - once: bool, 61 - called: bool 62 - } 31 + pub fn get_system_actor(sys_id: String, actor_id: String) -> Vec<u8> { 32 + let runtime = get_runtime() 33 + .lock() 34 + .unwrap(); 35 + 36 + let system_data = runtime 37 + .systems 38 + .get(&sys_id) 39 + .unwrap(); 63 40 64 - impl Hook { 65 - pub fn new<F>(callback: F, once: bool) -> Self 66 - where 67 - F: Fn(&Value) -> () + 'static + Send + Sync 68 - { 69 - Self { 70 - func: Box::new(callback), 71 - once, 72 - called: false 73 - } 74 - } 75 - 76 - pub fn call(&mut self, data: &Value) { 77 - if self.once { 78 - if !self.called { 79 - (self.func)(data); 80 - self.called = true; 81 - } 82 - } else { 83 - (self.func)(data); 84 - } 85 - } 41 + let actor_data = system_data.get_actor(Uuid::from_str(&actor_id).unwrap()); 42 + actor_data.serialize() 86 43 }
+14
src/traits.rs
··· 1 + use uuid::Uuid; 2 + 3 + pub trait Serializable: Send + Sync { 4 + fn serialize(&self) -> Vec<u8>; 5 + } 6 + 7 + pub trait DiceWireSystem: Send + Sync { 8 + fn name(&self) -> &str; 9 + fn get_actor(&self, uuid: Uuid) -> Box<dyn DiceWireActor>; 10 + } 11 + 12 + pub trait DiceWireActor: Send + Sync + Serializable { 13 + 14 + }