use std::{
cmp::Ordering,
hash::{self, Hash},
sync::{Arc, Weak},
};
use crate::{
ditto::{Ditto, DittoFields},
error::DittoError,
store::{Query, QueryArguments},
utils::extension_traits::FfiResultIntoRustResult,
};
pub struct SyncSubscription {
ditto: Weak<DittoFields>,
pub(crate) query: Query,
pub(crate) query_args: Option<QueryArguments>,
}
impl ::core::fmt::Display for SyncSubscription {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
self.query.inner_string.fmt(f)
}
}
impl SyncSubscription {
pub(crate) fn new(
ditto: &Ditto,
query: Query,
query_args: Option<QueryArguments>,
) -> Result<Self, DittoError> {
ffi_sdk::dittoffi_try_add_sync_subscription(
&ditto.ditto,
query.prepare_ffi(),
query_args.as_ref().map(|qa| qa.cbor().into()),
)
.into_rust_result()?;
Ok(Self {
ditto: Arc::downgrade(&ditto.fields),
query,
query_args,
})
}
pub fn cancel(&self) {
if let Ok(ditto) = Ditto::upgrade(&self.ditto) {
ditto.sync.unregister_subscription(self);
}
}
pub fn is_cancelled(&self) -> bool {
Ditto::upgrade(&self.ditto).map_or(true, |ditto| ditto.sync.subscriptions().contains(self))
}
}
impl Drop for SyncSubscription {
fn drop(&mut self) {
self.cancel()
}
}
impl SyncSubscription {
fn comparable_parts(&self) -> impl '_ + Ord + Hash {
(&self.query, &self.query_args)
}
}
impl Ord for SyncSubscription {
fn cmp(self: &Self, other: &Self) -> Ordering {
Ord::cmp(&self.comparable_parts(), &other.comparable_parts())
}
}
impl PartialOrd for SyncSubscription {
fn partial_cmp(self: &Self, other: &Self) -> Option<Ordering> {
Some(Ord::cmp(self, other))
}
}
impl Eq for SyncSubscription {}
impl PartialEq for SyncSubscription {
fn eq(self: &Self, other: &Self) -> bool {
self.comparable_parts() == other.comparable_parts()
}
}
impl Hash for SyncSubscription {
fn hash<H: hash::Hasher>(&self, h: &mut H) {
self.comparable_parts().hash(h)
}
}