notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

handler.rs (2378B)


      1 use hashbrown::HashSet;
      2 use nostrdb::{Filter, Note};
      3 
      4 use crate::relay::outbox::OutboxPool;
      5 use crate::relay::{NormRelayUrl, OutboxSubId, RelayId, RelayUrlPkgs};
      6 use crate::{relay::outbox::OutboxSession, Wakeup};
      7 
      8 /// OutboxSessionHandler is the RAII wrapper apps use to stage subscription
      9 /// updates; dropping it flushes the recorded operations into the OutboxPool.
     10 pub struct OutboxSessionHandler<'a, W>
     11 where
     12     W: Wakeup,
     13 {
     14     pub outbox: &'a mut OutboxPool,
     15     pub(crate) session: OutboxSession,
     16     pub(crate) wakeup: W,
     17 }
     18 
     19 impl<'a, W> Drop for OutboxSessionHandler<'a, W>
     20 where
     21     W: Wakeup,
     22 {
     23     fn drop(&mut self) {
     24         let session = std::mem::take(&mut self.session);
     25         self.outbox.ingest_session(session, &self.wakeup);
     26     }
     27 }
     28 
     29 impl<'a, W> OutboxSessionHandler<'a, W>
     30 where
     31     W: Wakeup,
     32 {
     33     pub fn new(outbox: &'a mut OutboxPool, wakeup: W) -> Self {
     34         Self {
     35             outbox,
     36             session: OutboxSession::default(),
     37             wakeup,
     38         }
     39     }
     40 
     41     pub fn subscribe(&mut self, filters: Vec<Filter>, urls: RelayUrlPkgs) -> OutboxSubId {
     42         let new_id = self.outbox.registry.next();
     43         self.session.subscribe(new_id, filters, urls);
     44         new_id
     45     }
     46 
     47     pub fn oneshot(&mut self, filters: Vec<Filter>, urls: RelayUrlPkgs) {
     48         let new_id = self.outbox.registry.next();
     49         self.session.oneshot(new_id, filters, urls);
     50     }
     51 
     52     pub fn modify_filters(&mut self, id: OutboxSubId, filters: Vec<Filter>) {
     53         self.session.new_filters(id, filters);
     54     }
     55 
     56     pub fn modify_relays(&mut self, id: OutboxSubId, relays: HashSet<NormRelayUrl>) {
     57         self.session.new_relays(id, relays);
     58     }
     59 
     60     pub fn unsubscribe(&mut self, id: OutboxSubId) {
     61         self.session.unsubscribe(id);
     62     }
     63 
     64     pub fn broadcast_note(&mut self, note: &Note, relays: Vec<RelayId>) {
     65         self.outbox.broadcast_note(note, relays, &self.wakeup);
     66     }
     67 
     68     /// Eject the session from the handler.
     69     /// This is only necessary between initialization of the app and the first frame
     70     pub fn export(mut self) -> OutboxSession {
     71         let session = std::mem::take(&mut self.session);
     72         drop(self);
     73         session
     74     }
     75 
     76     pub fn import(outbox: &'a mut OutboxPool, session: OutboxSession, wakeup: W) -> Self {
     77         Self {
     78             outbox,
     79             session,
     80             wakeup,
     81         }
     82     }
     83 }