AppFlowy/frontend/rust-lib/flowy-document/src/document.rs
Bartosz Sypytkowski fd5299a13d
move to latest appflowy collab version (#5894)
* chore: move to latest appflowy collab version

* chore: filter mapping

* chore: remove mutex folder

* chore: cleanup borrow checker issues

* chore: fixed flowy user crate compilation errors

* chore: removed parking lot crate

* chore: adjusting non locking approach

* chore: remove with folder method

* chore: fix folder manager

* chore: fixed workspace database compilation errors

* chore: initialize database plugins

* chore: fix locks in flowy core

* chore: remove supabase

* chore: async traits

* chore: add mutexes in dart ffi

* chore: post rebase fixes

* chore: remove supabase dart code

* chore: fix deadlock

* chore: fix page_id is empty

* chore: use data source to init collab

* chore: fix user awareness test

* chore: fix database deadlock

* fix: initialize user awareness

* chore: fix open workspace test

* chore: fix import csv

* chore: fix update row meta deadlock

* chore: fix document size test

* fix: timestamp set/get type convert

* fix: calculation

* chore: revert Arc to Rc

* chore: attach plugin to database and database row

* chore: async get row

* chore: clippy

* chore: fix tauri build

* chore: clippy

* fix: duplicate view deadlock

* chore: fmt

* chore: tauri build

---------

Co-authored-by: nathan <nathan@appflowy.io>
2024-08-18 11:16:42 +08:00

70 lines
2.3 KiB
Rust

use crate::entities::{
DocEventPB, DocumentAwarenessStatesPB, DocumentSnapshotStatePB, DocumentSyncStatePB,
};
use crate::notification::{send_notification, DocumentNotification};
use collab::preclude::Collab;
use collab_document::document::Document;
use futures::StreamExt;
use lib_dispatch::prelude::af_spawn;
pub fn subscribe_document_changed(doc_id: &str, document: &mut Document) {
let doc_id_clone_for_block_changed = doc_id.to_owned();
document.subscribe_block_changed("key", move |events, is_remote| {
#[cfg(feature = "verbose_log")]
tracing::trace!("subscribe_document_changed: {:?}", events);
// send notification to the client.
send_notification(
&doc_id_clone_for_block_changed,
DocumentNotification::DidReceiveUpdate,
)
.payload::<DocEventPB>((events, is_remote, None).into())
.send();
});
let doc_id_clone_for_awareness_state = doc_id.to_owned();
document.subscribe_awareness_state("key", move |events| {
#[cfg(feature = "verbose_log")]
tracing::trace!("subscribe_awareness_state: {:?}", events);
send_notification(
&doc_id_clone_for_awareness_state,
DocumentNotification::DidUpdateDocumentAwarenessState,
)
.payload::<DocumentAwarenessStatesPB>(events.into())
.send();
});
}
pub fn subscribe_document_snapshot_state(collab: &Collab) {
let document_id = collab.object_id().to_string();
let mut snapshot_state = collab.subscribe_snapshot_state();
af_spawn(async move {
while let Some(snapshot_state) = snapshot_state.next().await {
if let Some(new_snapshot_id) = snapshot_state.snapshot_id() {
tracing::debug!("Did create document remote snapshot: {}", new_snapshot_id);
send_notification(
&document_id,
DocumentNotification::DidUpdateDocumentSnapshotState,
)
.payload(DocumentSnapshotStatePB { new_snapshot_id })
.send();
}
}
});
}
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();
af_spawn(async move {
while let Some(sync_state) = sync_state_stream.next().await {
send_notification(
&document_id,
DocumentNotification::DidUpdateDocumentSyncState,
)
.payload(DocumentSyncStatePB::from(sync_state))
.send();
}
});
}