feat: using cached view (#2877)

This commit is contained in:
Nathan.fooo 2023-06-23 23:19:34 +08:00 committed by GitHub
parent f3d6c66f5c
commit 88faa0f02a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 48 additions and 59 deletions

View File

@ -34,12 +34,12 @@ default = ["custom-protocol"]
custom-protocol = ["tauri/custom-protocol"]
[patch.crates-io]
collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "06e942" }
collab-folder = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "06e942" }
collab-persistence = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "06e942" }
collab-document = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "06e942" }
collab-database = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "06e942" }
appflowy-integrate = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "06e942" }
collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "d1882d" }
collab-folder = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "d1882d" }
collab-persistence = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "d1882d" }
collab-document = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "d1882d" }
collab-database = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "d1882d" }
appflowy-integrate = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "d1882d" }
#collab = { path = "../../AppFlowy-Collab/collab" }
#collab-folder = { path = "../../AppFlowy-Collab/collab-folder" }

View File

@ -85,7 +85,6 @@ checksum = "7de8ce5e0f9f8d88245311066a578d72b7af3e7088f32783804676302df237e4"
[[package]]
name = "appflowy-integrate"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=06e942#06e942cb6433c94b5ecfe1d431b64bba625fc09c"
dependencies = [
"anyhow",
"collab",
@ -887,7 +886,6 @@ dependencies = [
[[package]]
name = "collab"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=06e942#06e942cb6433c94b5ecfe1d431b64bba625fc09c"
dependencies = [
"anyhow",
"bytes",
@ -905,7 +903,6 @@ dependencies = [
[[package]]
name = "collab-client-ws"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=06e942#06e942cb6433c94b5ecfe1d431b64bba625fc09c"
dependencies = [
"bytes",
"collab-sync",
@ -923,7 +920,6 @@ dependencies = [
[[package]]
name = "collab-database"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=06e942#06e942cb6433c94b5ecfe1d431b64bba625fc09c"
dependencies = [
"anyhow",
"async-trait",
@ -949,7 +945,6 @@ dependencies = [
[[package]]
name = "collab-derive"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=06e942#06e942cb6433c94b5ecfe1d431b64bba625fc09c"
dependencies = [
"proc-macro2",
"quote",
@ -961,7 +956,6 @@ dependencies = [
[[package]]
name = "collab-document"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=06e942#06e942cb6433c94b5ecfe1d431b64bba625fc09c"
dependencies = [
"anyhow",
"collab",
@ -979,7 +973,6 @@ dependencies = [
[[package]]
name = "collab-folder"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=06e942#06e942cb6433c94b5ecfe1d431b64bba625fc09c"
dependencies = [
"anyhow",
"chrono",
@ -999,7 +992,6 @@ dependencies = [
[[package]]
name = "collab-persistence"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=06e942#06e942cb6433c94b5ecfe1d431b64bba625fc09c"
dependencies = [
"bincode",
"chrono",
@ -1019,7 +1011,6 @@ dependencies = [
[[package]]
name = "collab-plugins"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=06e942#06e942cb6433c94b5ecfe1d431b64bba625fc09c"
dependencies = [
"anyhow",
"async-trait",
@ -1050,7 +1041,6 @@ dependencies = [
[[package]]
name = "collab-sync"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=06e942#06e942cb6433c94b5ecfe1d431b64bba625fc09c"
dependencies = [
"bytes",
"collab",

View File

@ -33,11 +33,11 @@ opt-level = 3
incremental = false
[patch.crates-io]
collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "06e942" }
collab-folder = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "06e942" }
collab-document = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "06e942" }
collab-database = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "06e942" }
appflowy-integrate = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "06e942" }
collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "d1882d" }
collab-folder = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "d1882d" }
collab-document = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "d1882d" }
collab-database = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "d1882d" }
appflowy-integrate = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "d1882d" }
#collab = { path = "../AppFlowy-Collab/collab" }
#collab-folder = { path = "../AppFlowy-Collab/collab-folder" }

View File

@ -63,7 +63,7 @@ impl DocumentManager {
}
tracing::debug!("open_document: {:?}", doc_id);
// read the existing document from the disk.
let document = self.get_document_from_disk(&doc_id)?;
let document = self.get_document_from_disk(doc_id)?;
// save the document to the memory and read it from the memory if we open the same document again.
// and we don't want to subscribe to the document changes if we open the same document again.
self

View File

@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::convert::TryInto;
use std::ops::{Deref, DerefMut};
use std::sync::Arc;
use collab_folder::core::{View, ViewLayout};
@ -59,33 +60,33 @@ pub struct ViewPB {
pub cover_url: Option<String>,
}
pub fn view_pb_without_child_views(view: View) -> ViewPB {
pub fn view_pb_without_child_views(view: Arc<View>) -> ViewPB {
ViewPB {
id: view.id,
parent_view_id: view.parent_view_id,
name: view.name,
id: view.id.clone(),
parent_view_id: view.parent_view_id.clone(),
name: view.name.clone(),
create_time: view.created_at,
child_views: Default::default(),
layout: view.layout.into(),
icon_url: view.icon_url,
cover_url: view.cover_url,
layout: view.layout.clone().into(),
icon_url: view.icon_url.clone(),
cover_url: view.cover_url.clone(),
}
}
/// Returns a ViewPB with child views. Only the first level of child views are included.
pub fn view_pb_with_child_views(view: View, child_views: Vec<View>) -> ViewPB {
pub fn view_pb_with_child_views(view: Arc<View>, child_views: Vec<Arc<View>>) -> ViewPB {
ViewPB {
id: view.id,
parent_view_id: view.parent_view_id,
name: view.name,
id: view.id.clone(),
parent_view_id: view.parent_view_id.clone(),
name: view.name.clone(),
create_time: view.created_at,
child_views: child_views
.into_iter()
.map(view_pb_without_child_views)
.collect(),
layout: view.layout.into(),
icon_url: view.icon_url,
cover_url: view.cover_url,
layout: view.layout.clone().into(),
icon_url: view.icon_url.clone(),
cover_url: view.cover_url.clone(),
}
}

View File

@ -87,7 +87,7 @@ pub(crate) async fn create_view_handler(
if set_as_current {
let _ = folder.set_current_view(&view.id).await;
}
data_result_ok(view_pb_without_child_views(view))
data_result_ok(view_pb_without_child_views(Arc::new(view)))
}
pub(crate) async fn create_orphan_view_handler(
@ -100,7 +100,7 @@ pub(crate) async fn create_orphan_view_handler(
if set_as_current {
let _ = folder.set_current_view(&view.id).await;
}
data_result_ok(view_pb_without_child_views(view))
data_result_ok(view_pb_without_child_views(Arc::new(view)))
}
pub(crate) async fn read_view_handler(

View File

@ -313,14 +313,13 @@ impl Folder2Manager {
match folder.views.get_view(&view_id) {
None => Err(FlowyError::record_not_found()),
Some(mut view) => {
view.children.retain(|b| !trash_ids.contains(&b.id));
Some(view) => {
let child_views = folder
.views
.get_views_belong_to(&view.id)
.into_iter()
.filter(|view| !trash_ids.contains(&view.id))
.collect::<Vec<View>>();
.collect::<Vec<_>>();
let view_pb = view_pb_with_child_views(view, child_views);
Ok(view_pb)
},
@ -402,7 +401,7 @@ impl Folder2Manager {
/// Return a list of views that belong to the given parent view id.
#[tracing::instrument(level = "debug", skip(self, parent_view_id), err)]
pub async fn get_views_belong_to(&self, parent_view_id: &str) -> FlowyResult<Vec<View>> {
pub async fn get_views_belong_to(&self, parent_view_id: &str) -> FlowyResult<Vec<Arc<View>>> {
let views = self.with_folder(vec![], |folder| {
folder.views.get_views_belong_to(parent_view_id)
});
@ -434,10 +433,6 @@ impl Folder2Manager {
}
if let Ok(view_pb) = self.get_view(&params.view_id).await {
notify_parent_view_did_change(
self.mutex_folder.clone(),
vec![view_pb.parent_view_id.clone()],
);
send_notification(&view_pb.id, FolderNotification::DidUpdateView)
.payload(view_pb)
.send();
@ -457,8 +452,8 @@ impl Folder2Manager {
let duplicate_params = CreateViewParams {
parent_view_id: view.parent_view_id.clone(),
name: format!("{} (copy)", &view.name),
desc: view.desc,
layout: view.layout.into(),
desc: view.desc.clone(),
layout: view.layout.clone().into(),
initial_data: view_data.to_vec(),
view_id: gen_view_id(),
meta: Default::default(),
@ -619,10 +614,11 @@ impl Folder2Manager {
}),
Some(parent_view) => Some((
false,
parent_view.id,
parent_view.id.clone(),
parent_view
.children
.items
.clone()
.into_iter()
.map(|view| view.id)
.collect::<Vec<String>>(),
@ -642,7 +638,7 @@ fn listen_on_view_change(mut rx: ViewChangeReceiver, weak_mutex_folder: &Weak<Mu
match value {
ViewChange::DidCreateView { view } => {
notify_child_views_changed(
view_pb_without_child_views(view.clone()),
view_pb_without_child_views(Arc::new(view.clone())),
ChildViewChangeReason::DidCreateView,
);
notify_parent_view_did_change(folder.clone(), vec![view.parent_view_id]);
@ -657,7 +653,7 @@ fn listen_on_view_change(mut rx: ViewChangeReceiver, weak_mutex_folder: &Weak<Mu
},
ViewChange::DidUpdate { view } => {
notify_child_views_changed(
view_pb_without_child_views(view.clone()),
view_pb_without_child_views(Arc::new(view.clone())),
ChildViewChangeReason::DidUpdateView,
);
notify_parent_view_did_change(folder.clone(), vec![view.parent_view_id]);
@ -707,7 +703,7 @@ fn listen_on_trash_change(mut rx: TrashChangeReceiver, weak_mutex_folder: &Weak<
if let Some(folder) = folder.lock().as_ref() {
let views = folder.views.get_views(&ids);
for view in views {
unique_ids.insert(view.parent_view_id);
unique_ids.insert(view.parent_view_id.clone());
}
let repeated_trash: RepeatedTrashPB = folder.get_all_trash().into();

View File

@ -1,3 +1,5 @@
use std::sync::Arc;
use collab_folder::core::{View, Workspace};
use flowy_derive::ProtoBuf_Enum;
@ -54,7 +56,7 @@ pub(crate) fn send_workspace_notification<T: ToBytes>(ty: FolderNotification, pa
pub(crate) fn send_workspace_setting_notification(
current_workspace: Option<Workspace>,
current_view: Option<View>,
current_view: Option<Arc<View>>,
) -> Option<()> {
let workspace: WorkspacePB = current_workspace?.into();
let latest_view = current_view.map(view_pb_without_child_views);

View File

@ -1,6 +1,6 @@
use std::sync::Arc;
use collab_folder::core::{FolderData, RepeatedView, ViewIdentifier, Workspace};
use collab_folder::core::{FolderData, RepeatedViewIdentifier, ViewIdentifier, Workspace};
use tokio::sync::RwLock;
use lib_infra::util::timestamp;
@ -46,7 +46,7 @@ impl DefaultFolderBuilder {
let workspace = Workspace {
id: workspace_id,
name: "Workspace".to_string(),
child_views: RepeatedView::new(first_level_views),
child_views: RepeatedViewIdentifier::new(first_level_views),
created_at: timestamp(),
};
@ -78,11 +78,11 @@ pub fn gen_workspace_id() -> String {
impl From<&ParentChildViews> for ViewPB {
fn from(value: &ParentChildViews) -> Self {
view_pb_with_child_views(
value.parent_view.clone(),
Arc::new(value.parent_view.clone()),
value
.child_views
.iter()
.map(|v| v.parent_view.clone())
.map(|v| Arc::new(v.parent_view.clone()))
.collect(),
)
}

View File

@ -4,7 +4,7 @@ use std::sync::Arc;
use bytes::Bytes;
pub use collab_folder::core::View;
use collab_folder::core::{RepeatedView, ViewIdentifier, ViewLayout};
use collab_folder::core::{RepeatedViewIdentifier, ViewIdentifier, ViewLayout};
use tokio::sync::RwLock;
use flowy_error::FlowyError;
@ -113,7 +113,7 @@ impl ViewBuilder {
layout: self.layout,
icon_url: self.icon_url,
cover_url: self.cover_url,
children: RepeatedView::new(
children: RepeatedViewIdentifier::new(
self
.child_views
.iter()