Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

chore: try to fix when load data from cloud cause windows delay rendering (#6708)

Nathan.fooo3fbdcab7b015
diff --git a/frontend/rust-lib/collab-integrate/src/collab_builder.rs b/frontend/rust-lib/collab-integrate/src/collab_builder.rs
index acf6123076..b6e89a5a2d 100644
--- a/frontend/rust-lib/collab-integrate/src/collab_builder.rs
+++ b/frontend/rust-lib/collab-integrate/src/collab_builder.rs
@@ -150,7 +150,7 @@ impl AppFlowyCollabBuilder {
level = "trace",
skip(self, data_source, collab_db, builder_config, data)
)]
- pub fn create_document(
+ pub async fn create_document(
&self,
object: CollabObject,
data_source: DataSource,
@@ -160,7 +160,7 @@ impl AppFlowyCollabBuilder {
) -> Result<Arc<RwLock<Document>>, Error> {
let expected_collab_type = CollabType::Document;
assert_eq!(object.collab_type, expected_collab_type);
- let mut collab = self.build_collab(&object, &collab_db, data_source)?;
+ let mut collab = self.build_collab(&object, &collab_db, data_source).await?;
collab.enable_undo_redo();
let document = match data {
@@ -192,7 +192,7 @@ impl AppFlowyCollabBuilder {
level = "trace",
skip(self, object, doc_state, collab_db, builder_config, folder_notifier)
)]
- pub fn create_folder(
+ pub async fn create_folder(
&self,
object: CollabObject,
doc_state: DataSource,
@@ -205,11 +205,11 @@ impl AppFlowyCollabBuilder {
assert_eq!(object.collab_type, expected_collab_type);
let folder = match folder_data {
None => {
- let collab = self.build_collab(&object, &collab_db, doc_state)?;
+ let collab = self.build_collab(&object, &collab_db, doc_state).await?;
Folder::open(object.uid, collab, folder_notifier)?
},
Some(data) => {
- let collab = self.build_collab(&object, &collab_db, doc_state)?;
+ let collab = self.build_collab(&object, &collab_db, doc_state).await?;
let folder = Folder::create(object.uid, collab, folder_notifier, data);
if let Err(err) = self.write_collab_to_disk(
object.uid,
@@ -233,7 +233,7 @@ impl AppFlowyCollabBuilder {
level = "trace",
skip(self, object, doc_state, collab_db, builder_config, notifier)
)]
- pub fn create_user_awareness(
+ pub async fn create_user_awareness(
&self,
object: CollabObject,
doc_state: DataSource,
@@ -243,7 +243,7 @@ impl AppFlowyCollabBuilder {
) -> Result<Arc<RwLock<UserAwareness>>, Error> {
let expected_collab_type = CollabType::UserAwareness;
assert_eq!(object.collab_type, expected_collab_type);
- let collab = self.build_collab(&object, &collab_db, doc_state)?;
+ let collab = self.build_collab(&object, &collab_db, doc_state).await?;
let user_awareness = UserAwareness::create(collab, notifier)?;
let user_awareness = Arc::new(RwLock::new(user_awareness));
self.finalize(object, builder_config, user_awareness)
@@ -266,27 +266,34 @@ impl AppFlowyCollabBuilder {
self.finalize(object, builder_config, workspace)
}
- pub fn build_collab(
+ pub async fn build_collab(
&self,
object: &CollabObject,
collab_db: &Weak<CollabKVDB>,
data_source: DataSource,
) -> Result<Collab, Error> {
- let mut collab = CollabBuilder::new(object.uid, &object.object_id, data_source)
- .with_device_id(self.workspace_integrate.device_id()?)
- .build()?;
-
- let persistence_config = CollabPersistenceConfig::default();
- let db_plugin = RocksdbDiskPlugin::new_with_config(
- object.uid,
- object.workspace_id.clone(),
- object.object_id.to_string(),
- object.collab_type.clone(),
- collab_db.clone(),
- persistence_config.clone(),
- );
- collab.add_plugin(Box::new(db_plugin));
- collab.initialize();
+ let object = object.clone();
+ let collab_db = collab_db.clone();
+ let device_id = self.workspace_integrate.device_id()?;
+ let collab = tokio::task::spawn_blocking(move || {
+ let mut collab = CollabBuilder::new(object.uid, &object.object_id, data_source)
+ .with_device_id(device_id)
+ .build()?;
+ let persistence_config = CollabPersistenceConfig::default();
+ let db_plugin = RocksdbDiskPlugin::new_with_config(
+ object.uid,
+ object.workspace_id.clone(),
+ object.object_id.to_string(),
+ object.collab_type.clone(),
+ collab_db,
+ persistence_config,
+ );
+ collab.add_plugin(Box::new(db_plugin));
+ collab.initialize();
+ Ok::<_, Error>(collab)
+ })
+ .await??;
+
Ok(collab)
}
diff --git a/frontend/rust-lib/flowy-database2/src/manager.rs b/frontend/rust-lib/flowy-database2/src/manager.rs
index 68c13b8e84..074d9f2f69 100644
--- a/frontend/rust-lib/flowy-database2/src/manager.rs
+++ b/frontend/rust-lib/flowy-database2/src/manager.rs
@@ -870,7 +870,8 @@ impl DatabaseCollabService for WorkspaceDatabaseCollabServiceImpl {
let collab_db = self.collab_db()?;
let collab = self
.collab_builder
- .build_collab(&object, &collab_db, data_source)?;
+ .build_collab(&object, &collab_db, data_source)
+ .await?;
Ok(collab)
}
diff --git a/frontend/rust-lib/flowy-document/src/event_handler.rs b/frontend/rust-lib/flowy-document/src/event_handler.rs
index 2efde86a89..e63d15b7ab 100644
--- a/frontend/rust-lib/flowy-document/src/event_handler.rs
+++ b/frontend/rust-lib/flowy-document/src/event_handler.rs
@@ -42,7 +42,7 @@ pub(crate) async fn get_encode_collab_handler(
let manager = upgrade_document(manager)?;
let params: OpenDocumentParams = data.into_inner().try_into()?;
let doc_id = params.document_id;
- let state = manager.get_encoded_collab_with_view_id(&doc_id)?;
+ let state = manager.get_encoded_collab_with_view_id(&doc_id).await?;
data_result_ok(EncodedCollabPB {
state_vector: Vec::from(state.state_vector),
doc_state: Vec::from(state.doc_state),
diff --git a/frontend/rust-lib/flowy-document/src/manager.rs b/frontend/rust-lib/flowy-document/src/manager.rs
index 6ef6c7aac2..e22c0827da 100644
--- a/frontend/rust-lib/flowy-document/src/manager.rs
+++ b/frontend/rust-lib/flowy-document/src/manager.rs
@@ -82,13 +82,15 @@ impl DocumentManager {
}
/// Get the encoded collab of the document.
- pub fn get_encoded_collab_with_view_id(&self, doc_id: &str) -> FlowyResult<EncodedCollab> {
+ pub async fn get_encoded_collab_with_view_id(&self, doc_id: &str) -> FlowyResult<EncodedCollab> {
let uid = self.user_service.user_id()?;
let workspace_id = self.user_service.workspace_id()?;
let doc_state =
CollabPersistenceImpl::new(self.user_service.collab_db(uid)?, uid, workspace_id)
.into_data_source();
- let collab = self.collab_for_document(uid, doc_id, doc_state, false)?;
+ let collab = self
+ .collab_for_document(uid, doc_id, doc_state, false)
+ .await?;
let encoded_collab = collab
.try_read()
.unwrap()
@@ -167,7 +169,7 @@ impl DocumentManager {
}
}
- fn collab_for_document(
+ async fn collab_for_document(
&self,
uid: i64,
doc_id: &str,
@@ -180,13 +182,16 @@ impl DocumentManager {
self
.collab_builder
.collab_object(&workspace_id, uid, doc_id, CollabType::Document)?;
- let document = self.collab_builder.create_document(
- collab_object,
- data_source,
- db,
- CollabBuilderConfig::default().sync_enable(sync_enable),
- None,
- )?;
+ let document = self
+ .collab_builder
+ .create_document(
+ collab_object,
+ data_source,
+ db,
+ CollabBuilderConfig::default().sync_enable(sync_enable),
+ None,
+ )
+ .await?;
Ok(document)
}
@@ -243,7 +248,9 @@ impl DocumentManager {
doc_id,
self.user_service.workspace_id()
);
- let result = self.collab_for_document(uid, doc_id, doc_state, enable_sync);
+ let result = self
+ .collab_for_document(uid, doc_id, doc_state, enable_sync)
+ .await;
match result {
Ok(document) => {
// Only push the document to the cache if the sync is enabled.
diff --git a/frontend/rust-lib/flowy-folder/src/manager.rs b/frontend/rust-lib/flowy-folder/src/manager.rs
index 02028a2eaa..b80ce0047e 100644
--- a/frontend/rust-lib/flowy-folder/src/manager.rs
+++ b/frontend/rust-lib/flowy-folder/src/manager.rs
@@ -170,14 +170,17 @@ impl FolderManager {
self
.collab_builder
.collab_object(workspace_id, uid, object_id, CollabType::Folder)?;
- let result = self.collab_builder.create_folder(
- collab_object,
- data_source,
- collab_db,
- config,
- folder_notifier,
- None,
- );
+ let result = self
+ .collab_builder
+ .create_folder(
+ collab_object,
+ data_source,
+ collab_db,
+ config,
+ folder_notifier,
+ None,
+ )
+ .await;
// If opening the folder fails due to missing required data (indicated by a `FolderError::NoRequiredData`),
// the function logs an informational message and attempts to clear the folder data by deleting its
@@ -213,14 +216,17 @@ impl FolderManager {
let doc_state = CollabPersistenceImpl::new(collab_db.clone(), uid, workspace_id.to_string())
.into_data_source();
- let folder = self.collab_builder.create_folder(
- collab_object,
- doc_state,
- collab_db,
- CollabBuilderConfig::default().sync_enable(true),
- notifier,
- folder_data,
- )?;
+ let folder = self
+ .collab_builder
+ .create_folder(
+ collab_object,
+ doc_state,
+ collab_db,
+ CollabBuilderConfig::default().sync_enable(true),
+ notifier,
+ folder_data,
+ )
+ .await?;
Ok(folder)
}
diff --git a/frontend/rust-lib/flowy-user/src/user_manager/manager_user_awareness.rs b/frontend/rust-lib/flowy-user/src/user_manager/manager_user_awareness.rs
index 0cfee15a16..5f120c6e72 100644
--- a/frontend/rust-lib/flowy-user/src/user_manager/manager_user_awareness.rs
+++ b/frontend/rust-lib/flowy-user/src/user_manager/manager_user_awareness.rs
@@ -170,7 +170,8 @@ impl UserManager {
collab_db,
doc_state,
None,
- )?;
+ )
+ .await?;
info!("User awareness initialized successfully");
self.user_awareness.store(Some(awareness));
if let Some(mut is_loading) = self.is_loading_awareness.get_mut(&object_id) {
@@ -238,6 +239,7 @@ impl UserManager {
doc_state,
None,
)
+ .await
} else {
let result = cloud_services
.get_user_service()?
@@ -256,6 +258,7 @@ impl UserManager {
DataSource::DocStateV1(data),
None,
)
+ .await
},
Err(err) => {
if err.is_record_not_found() {
@@ -275,6 +278,7 @@ impl UserManager {
doc_state,
None,
)
+ .await
} else {
Err(err)
}
@@ -315,7 +319,7 @@ impl UserManager {
/// This function constructs a collaboration instance based on the given session and raw data,
/// using a collaboration builder. This instance is specifically geared towards handling
/// user awareness.
- fn collab_for_user_awareness(
+ async fn collab_for_user_awareness(
collab_builder: &Weak<AppFlowyCollabBuilder>,
workspace_id: &str,
uid: i64,
@@ -338,6 +342,7 @@ impl UserManager {
CollabBuilderConfig::default().sync_enable(true),
notifier,
)
+ .await
.context("Build collab for user awareness failed")?;
Ok(collab)
}