2024-03-30 08:28:24 +00:00
|
|
|
use crate::entities::{
|
|
|
|
DocEventPB, DocumentAwarenessStatesPB, DocumentSnapshotStatePB, DocumentSyncStatePB,
|
2023-04-13 10:53:51 +00:00
|
|
|
};
|
2024-03-30 08:28:24 +00:00
|
|
|
use crate::notification::{send_notification, DocumentNotification};
|
2024-08-18 03:16:42 +00:00
|
|
|
use collab::preclude::Collab;
|
|
|
|
use collab_document::document::Document;
|
2024-03-30 08:28:24 +00:00
|
|
|
use futures::StreamExt;
|
2023-10-30 04:35:06 +00:00
|
|
|
use lib_dispatch::prelude::af_spawn;
|
2023-04-13 10:53:51 +00:00
|
|
|
|
2024-08-18 03:16:42 +00:00
|
|
|
pub fn subscribe_document_changed(doc_id: &str, document: &mut Document) {
|
2024-03-28 09:46:31 +00:00
|
|
|
let doc_id_clone_for_block_changed = doc_id.to_owned();
|
2024-08-18 03:16:42 +00:00
|
|
|
document.subscribe_block_changed("key", move |events, is_remote| {
|
|
|
|
#[cfg(feature = "verbose_log")]
|
|
|
|
tracing::trace!("subscribe_document_changed: {:?}", events);
|
2024-04-07 13:36:55 +00:00
|
|
|
|
2024-08-18 03:16:42 +00:00
|
|
|
// send notification to the client.
|
|
|
|
send_notification(
|
|
|
|
&doc_id_clone_for_block_changed,
|
|
|
|
DocumentNotification::DidReceiveUpdate,
|
|
|
|
)
|
|
|
|
.payload::<DocEventPB>((events, is_remote, None).into())
|
|
|
|
.send();
|
|
|
|
});
|
2024-03-28 09:46:31 +00:00
|
|
|
|
|
|
|
let doc_id_clone_for_awareness_state = doc_id.to_owned();
|
2024-08-18 03:16:42 +00:00
|
|
|
document.subscribe_awareness_state("key", move |events| {
|
2024-04-07 13:36:55 +00:00
|
|
|
#[cfg(feature = "verbose_log")]
|
|
|
|
tracing::trace!("subscribe_awareness_state: {:?}", events);
|
2024-03-28 09:46:31 +00:00
|
|
|
send_notification(
|
|
|
|
&doc_id_clone_for_awareness_state,
|
|
|
|
DocumentNotification::DidUpdateDocumentAwarenessState,
|
|
|
|
)
|
|
|
|
.payload::<DocumentAwarenessStatesPB>(events.into())
|
|
|
|
.send();
|
|
|
|
});
|
2023-07-05 12:57:09 +00:00
|
|
|
}
|
|
|
|
|
2024-08-18 03:16:42 +00:00
|
|
|
pub fn subscribe_document_snapshot_state(collab: &Collab) {
|
|
|
|
let document_id = collab.object_id().to_string();
|
|
|
|
let mut snapshot_state = collab.subscribe_snapshot_state();
|
2023-10-30 04:35:06 +00:00
|
|
|
af_spawn(async move {
|
2023-07-05 12:57:09 +00:00
|
|
|
while let Some(snapshot_state) = snapshot_state.next().await {
|
|
|
|
if let Some(new_snapshot_id) = snapshot_state.snapshot_id() {
|
2023-07-14 05:37:13 +00:00
|
|
|
tracing::debug!("Did create document remote snapshot: {}", new_snapshot_id);
|
2023-07-05 12:57:09 +00:00
|
|
|
send_notification(
|
|
|
|
&document_id,
|
|
|
|
DocumentNotification::DidUpdateDocumentSnapshotState,
|
|
|
|
)
|
|
|
|
.payload(DocumentSnapshotStatePB { new_snapshot_id })
|
|
|
|
.send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-18 03:16:42 +00:00
|
|
|
pub fn subscribe_document_sync_state(collab: &Collab) {
|
|
|
|
let document_id = collab.object_id().to_string();
|
|
|
|
let mut sync_state_stream = collab.subscribe_sync_state();
|
2023-10-30 04:35:06 +00:00
|
|
|
af_spawn(async move {
|
2023-07-05 12:57:09 +00:00
|
|
|
while let Some(sync_state) = sync_state_stream.next().await {
|
|
|
|
send_notification(
|
|
|
|
&document_id,
|
|
|
|
DocumentNotification::DidUpdateDocumentSyncState,
|
|
|
|
)
|
|
|
|
.payload(DocumentSyncStatePB::from(sync_state))
|
|
|
|
.send();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|