ankurah_core/
subscription.rs

1use crate::{changes::ChangeSet, model::Entity, node::TNodeErased};
2use ankurah_proto as proto;
3use std::sync::{Arc, Mutex};
4
5/// A callback function that receives subscription updates
6pub type Callback<R> = Box<dyn Fn(ChangeSet<R>) + Send + Sync + 'static>;
7
8/// A subscription that can be shared between indexes
9pub struct Subscription<R: Clone> {
10    #[allow(unused)]
11    pub(crate) id: proto::SubscriptionId,
12    pub(crate) collection_id: proto::CollectionId,
13    pub(crate) predicate: ankql::ast::Predicate,
14    pub(crate) callback: Arc<Callback<R>>,
15    // Track which entities currently match this subscription
16    // TODO make this a ResultSet so we can clone it cheaply
17    pub(crate) matching_entities: Mutex<Vec<Arc<Entity>>>,
18}
19
20/// A handle to a subscription that can be used to register callbacks
21pub struct SubscriptionHandle {
22    pub(crate) id: proto::SubscriptionId,
23    pub(crate) node: Box<dyn TNodeErased>,
24    pub(crate) peers: Vec<proto::NodeId>,
25}
26
27impl SubscriptionHandle {
28    pub fn new(node: Box<dyn TNodeErased>, id: proto::SubscriptionId) -> Self { Self { id, node, peers: Vec::new() } }
29}
30
31impl Drop for SubscriptionHandle {
32    fn drop(&mut self) { self.node.unsubscribe(self); }
33}