refactor: remove UpdateRowPB, refactor RowInfo class

This commit is contained in:
appflowy
2022-08-13 14:59:50 +08:00
parent 6b0becd9ca
commit 57ede798d8
19 changed files with 118 additions and 159 deletions

View File

@ -106,48 +106,15 @@ impl std::convert::From<Vec<BlockPB>> for RepeatedBlockPB {
#[derive(Debug, Clone, Default, ProtoBuf)]
pub struct InsertedRowPB {
#[pb(index = 1)]
pub block_id: String,
pub row: RowPB,
#[pb(index = 2)]
pub row_id: String,
#[pb(index = 3)]
pub height: i32,
#[pb(index = 4, one_of)]
#[pb(index = 2, one_of)]
pub index: Option<i32>,
}
#[derive(Debug, Default, ProtoBuf)]
pub struct UpdatedRowPB {
#[pb(index = 1)]
pub block_id: String,
#[pb(index = 2)]
pub row_id: String,
#[pb(index = 3)]
pub row: RowPB,
}
impl UpdatedRowPB {
pub fn new(row_rev: &RowRevision, row: RowPB) -> Self {
Self {
row_id: row_rev.id.clone(),
block_id: row_rev.block_id.clone(),
row,
}
}
}
impl std::convert::From<RowPB> for InsertedRowPB {
fn from(row_info: RowPB) -> Self {
Self {
row_id: row_info.id,
block_id: row_info.block_id,
height: row_info.height,
index: None,
}
fn from(row: RowPB) -> Self {
Self { row, index: None }
}
}
@ -170,7 +137,7 @@ pub struct GridBlockChangesetPB {
pub deleted_rows: Vec<String>,
#[pb(index = 4)]
pub updated_rows: Vec<UpdatedRowPB>,
pub updated_rows: Vec<RowPB>,
#[pb(index = 5)]
pub visible_rows: Vec<String>,
@ -195,7 +162,7 @@ impl GridBlockChangesetPB {
}
}
pub fn update(block_id: &str, updated_rows: Vec<UpdatedRowPB>) -> Self {
pub fn update(block_id: &str, updated_rows: Vec<RowPB>) -> Self {
Self {
block_id: block_id.to_owned(),
updated_rows,

View File

@ -235,7 +235,7 @@ pub(crate) async fn get_row_handler(
let row = editor
.get_row_rev(&params.row_id)
.await?
.and_then(make_row_from_row_rev);
.and_then(|row_rev| Some(make_row_from_row_rev(row_rev)));
data_result(OptionalRowPB { row })
}

View File

@ -1,9 +1,9 @@
use crate::dart_notification::{send_dart_notification, GridNotification};
use crate::entities::{CellChangesetPB, GridBlockChangesetPB, InsertedRowPB, RowPB, UpdatedRowPB};
use crate::entities::{CellChangesetPB, GridBlockChangesetPB, InsertedRowPB, RowPB};
use crate::manager::GridUser;
use crate::services::block_revision_editor::{GridBlockRevisionCompactor, GridBlockRevisionEditor};
use crate::services::persistence::block_index::BlockIndexCache;
use crate::services::row::{block_from_row_orders, GridBlockSnapshot};
use crate::services::row::{block_from_row_orders, make_row_from_row_rev, GridBlockSnapshot};
use dashmap::DashMap;
use flowy_error::FlowyResult;
use flowy_grid_data_model::revision::{
@ -110,20 +110,18 @@ impl GridBlockManager {
pub async fn update_row<F>(&self, changeset: RowMetaChangeset, row_builder: F) -> FlowyResult<()>
where
F: FnOnce(Arc<RowRevision>) -> Option<RowPB>,
F: FnOnce(Arc<RowRevision>) -> RowPB,
{
let editor = self.get_editor_from_row_id(&changeset.row_id).await?;
let _ = editor.update_row(changeset.clone()).await?;
match editor.get_row_rev(&changeset.row_id).await? {
None => tracing::error!("Internal error: can't find the row with id: {}", changeset.row_id),
Some(row_rev) => {
if let Some(row) = row_builder(row_rev.clone()) {
let row_order = UpdatedRowPB::new(&row_rev, row);
let block_order_changeset = GridBlockChangesetPB::update(&editor.block_id, vec![row_order]);
let _ = self
.notify_did_update_block(&editor.block_id, block_order_changeset)
.await?;
}
let block_order_changeset =
GridBlockChangesetPB::update(&editor.block_id, vec![row_builder(row_rev.clone())]);
let _ = self
.notify_did_update_block(&editor.block_id, block_order_changeset)
.await?;
}
}
Ok(())
@ -170,17 +168,16 @@ impl GridBlockManager {
match editor.get_row_revs(Some(vec![Cow::Borrowed(row_id)])).await?.pop() {
None => {}
Some(row_rev) => {
let delete_row_id = row_rev.id.clone();
let insert_row = InsertedRowPB {
block_id: row_rev.block_id.clone(),
row_id: row_rev.id.clone(),
index: Some(to as i32),
height: row_rev.height,
row: make_row_from_row_rev(row_rev),
};
let notified_changeset = GridBlockChangesetPB {
block_id: editor.block_id.clone(),
inserted_rows: vec![insert_row],
deleted_rows: vec![row_rev.id.clone()],
deleted_rows: vec![delete_row_id],
..Default::default()
};
@ -195,7 +192,7 @@ impl GridBlockManager {
pub async fn update_cell<F>(&self, changeset: CellChangesetPB, row_builder: F) -> FlowyResult<()>
where
F: FnOnce(Arc<RowRevision>) -> Option<RowPB>,
F: FnOnce(Arc<RowRevision>) -> RowPB,
{
let row_changeset: RowMetaChangeset = changeset.clone().into();
let _ = self.update_row(row_changeset, row_builder).await?;

View File

@ -39,8 +39,8 @@ pub(crate) fn make_row_orders_from_row_revs(row_revs: &[Arc<RowRevision>]) -> Ve
row_revs.iter().map(RowPB::from).collect::<Vec<_>>()
}
pub(crate) fn make_row_from_row_rev(row_rev: Arc<RowRevision>) -> Option<RowPB> {
make_rows_from_row_revs(&[row_rev]).pop()
pub(crate) fn make_row_from_row_rev(row_rev: Arc<RowRevision>) -> RowPB {
make_rows_from_row_revs(&[row_rev]).pop().unwrap()
}
pub(crate) fn make_rows_from_row_revs(row_revs: &[Arc<RowRevision>]) -> Vec<RowPB> {