relay_pool_manager.rs (1547B)
1 use enostr::RelayPool; 2 pub use enostr::RelayStatus; 3 4 /// The interface to a RelayPool for UI components. 5 /// Represents all user-facing operations that can be performed for a user's relays 6 pub struct RelayPoolManager<'a> { 7 pub pool: &'a mut RelayPool, 8 } 9 10 pub struct RelayInfo<'a> { 11 pub relay_url: &'a str, 12 pub status: &'a RelayStatus, 13 } 14 15 impl<'a> RelayPoolManager<'a> { 16 pub fn new(pool: &'a mut RelayPool) -> Self { 17 RelayPoolManager { pool } 18 } 19 20 pub fn get_relay_infos(&self) -> Vec<RelayInfo> { 21 self.pool 22 .relays 23 .iter() 24 .map(|relay| RelayInfo { 25 relay_url: &relay.relay.url, 26 status: &relay.relay.status, 27 }) 28 .collect() 29 } 30 31 /// index of the Vec<RelayInfo> from get_relay_infos 32 pub fn remove_relay(&mut self, index: usize) { 33 if index < self.pool.relays.len() { 34 self.pool.relays.remove(index); 35 } 36 } 37 38 /// removes all specified relay indicies shown in get_relay_infos 39 pub fn remove_relays(&mut self, mut indices: Vec<usize>) { 40 indices.sort_unstable_by(|a, b| b.cmp(a)); 41 indices.iter().for_each(|index| self.remove_relay(*index)); 42 } 43 44 pub fn add_relay(&mut self, ctx: &egui::Context, relay_url: String) { 45 let _ = self.pool.add_url(relay_url, create_wakeup(ctx)); 46 } 47 } 48 49 pub fn create_wakeup(ctx: &egui::Context) -> impl Fn() + Send + Sync + Clone + 'static { 50 let ctx = ctx.clone(); 51 move || { 52 ctx.request_repaint(); 53 } 54 }