dittolive_ditto/store/query_builder/live_query/event.rs
1use_prelude!();
2use super::*;
3
4/// Use [`pending_op.observe_local(...)`] to receive `LiveQueryEvent`s describing document
5/// changes.
6///
7/// Describes the different types of event that you can receive when dealing with live queries.
8///
9/// [`pending_op.observe_local(...)`]: crate::store::query_builder::PendingCursorOperation::observe_local
10#[derive(Debug)]
11pub enum LiveQueryEvent {
12 /// The first event that will be delivered and it will only be delivered once.
13 Initial,
14
15 /// This event will be delivered each time the results of the provided query change. It
16 /// contains information about the set of documents that previously matched the query
17 /// before the update, along with information about what documents have been inserted,
18 /// deleted, updated, or moved, as part of the set of matching documents.
19 Update {
20 old_documents: Vec<ffi_sdk::BoxedDocument>,
21 insertions: Box<[usize]>,
22 deletions: Box<[usize]>,
23 updates: Box<[usize]>,
24 moves: Vec<LiveQueryMove>,
25 },
26}
27
28impl LiveQueryEvent {
29 /// Return an hash of a document
30 pub fn hash(&self, docs: &[ffi_sdk::BoxedDocument]) -> Result<u64, DittoError> {
31 ffi_sdk::ditto_documents_hash(docs.into()).ok()
32 }
33
34 /// Return the hash the `Document`s mnemonic.
35 pub fn hash_mnemonic(&self, docs: &[ffi_sdk::BoxedDocument]) -> Result<String, DittoError> {
36 let mnemonic_c_str = { ffi_sdk::ditto_documents_hash_mnemonic(docs.into()).ok()? };
37 Ok(mnemonic_c_str.into_string())
38 }
39}