fix: notify all views of field type option (#4229)

This commit is contained in:
Richard Shiue 2023-12-29 08:11:26 +08:00 committed by GitHub
parent 2ca911950c
commit 54fcc4ac5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 40 additions and 53 deletions

View File

@ -219,12 +219,7 @@ pub(crate) async fn update_field_type_option_handler(
let field_type = FieldType::from(old_field.field_type); let field_type = FieldType::from(old_field.field_type);
let type_option_data = type_option_data_from_pb(params.type_option_data, &field_type)?; let type_option_data = type_option_data_from_pb(params.type_option_data, &field_type)?;
database_editor database_editor
.update_field_type_option( .update_field_type_option(&params.field_id, type_option_data, old_field)
&params.view_id,
&params.field_id,
type_option_data,
old_field,
)
.await?; .await?;
} }
Ok(()) Ok(())
@ -262,12 +257,7 @@ pub(crate) async fn switch_to_field_handler(
match (old_field, new_type_option) { match (old_field, new_type_option) {
(Some(old_field), Some(new_type_option)) => { (Some(old_field), Some(new_type_option)) => {
database_editor database_editor
.update_field_type_option( .update_field_type_option(&params.field_id, new_type_option, old_field)
&params.view_id,
&params.field_id,
new_type_option,
old_field,
)
.await?; .await?;
}, },
_ => { _ => {

View File

@ -317,13 +317,12 @@ impl DatabaseEditor {
/// Do nothing if the [TypeOptionData] is empty. /// Do nothing if the [TypeOptionData] is empty.
pub async fn update_field_type_option( pub async fn update_field_type_option(
&self, &self,
view_id: &str,
_field_id: &str, _field_id: &str,
type_option_data: TypeOptionData, type_option_data: TypeOptionData,
old_field: Field, old_field: Field,
) -> FlowyResult<()> { ) -> FlowyResult<()> {
let view_editor = self.database_views.get_view_editor(view_id).await?; let view_editors = self.database_views.editors().await;
update_field_type_option_fn(&self.database, &view_editor, type_option_data, old_field).await?; update_field_type_option_fn(&self.database, &view_editors, type_option_data, old_field).await?;
Ok(()) Ok(())
} }
@ -808,9 +807,15 @@ impl DatabaseEditor {
.for_each(|option| type_option.insert_option(option.into())); .for_each(|option| type_option.insert_option(option.into()));
// Update the field's type option // Update the field's type option
self let view_editors = self.database_views.editors().await;
.update_field_type_option(view_id, field_id, type_option.to_type_option_data(), field) update_field_type_option_fn(
.await?; &self.database,
&view_editors,
type_option.to_type_option_data(),
field.clone(),
)
.await?;
// Insert the options into the cell // Insert the options into the cell
self self
.update_cell_with_changeset(view_id, row_id, field_id, cell_changeset) .update_cell_with_changeset(view_id, row_id, field_id, cell_changeset)
@ -842,10 +847,10 @@ impl DatabaseEditor {
type_option.delete_option(&option.id); type_option.delete_option(&option.id);
} }
let view_editor = self.database_views.get_view_editor(view_id).await?; let view_editors = self.database_views.editors().await;
update_field_type_option_fn( update_field_type_option_fn(
&self.database, &self.database,
&view_editor, &view_editors,
type_option.to_type_option_data(), type_option.to_type_option_data(),
field.clone(), field.clone(),
) )
@ -1290,22 +1295,23 @@ impl DatabaseViewOperation for DatabaseViewOperationImpl {
fn update_field( fn update_field(
&self, &self,
view_id: &str,
type_option_data: TypeOptionData, type_option_data: TypeOptionData,
old_field: Field, old_field: Field,
) -> FutureResult<(), FlowyError> { ) -> FutureResult<(), FlowyError> {
let view_id = view_id.to_string();
let weak_editor_by_view_id = Arc::downgrade(&self.editor_by_view_id); let weak_editor_by_view_id = Arc::downgrade(&self.editor_by_view_id);
let weak_database = Arc::downgrade(&self.database); let weak_database = Arc::downgrade(&self.database);
FutureResult::new(async move { FutureResult::new(async move {
if let (Some(database), Some(editor_by_view_id)) = if let (Some(database), Some(editor_by_view_id)) =
(weak_database.upgrade(), weak_editor_by_view_id.upgrade()) (weak_database.upgrade(), weak_editor_by_view_id.upgrade())
{ {
let view_editor = editor_by_view_id.read().await.get(&view_id).cloned(); let view_editors = editor_by_view_id
if let Some(view_editor) = view_editor { .read()
let _ = .await
update_field_type_option_fn(&database, &view_editor, type_option_data, old_field).await; .values()
} .map(|editor| editor.clone())
.collect();
let _ =
update_field_type_option_fn(&database, &view_editors, type_option_data, old_field).await;
} }
Ok(()) Ok(())
}) })
@ -1573,7 +1579,7 @@ impl DatabaseViewOperation for DatabaseViewOperationImpl {
#[tracing::instrument(level = "trace", skip_all, err)] #[tracing::instrument(level = "trace", skip_all, err)]
pub async fn update_field_type_option_fn( pub async fn update_field_type_option_fn(
database: &Arc<MutexDatabase>, database: &Arc<MutexDatabase>,
view_editor: &Arc<DatabaseViewEditor>, view_editors: &Vec<Arc<DatabaseViewEditor>>,
type_option_data: TypeOptionData, type_option_data: TypeOptionData,
old_field: Field, old_field: Field,
) -> FlowyResult<()> { ) -> FlowyResult<()> {
@ -1601,9 +1607,12 @@ pub async fn update_field_type_option_fn(
}); });
let _ = notify_did_update_database_field(database, &old_field.id); let _ = notify_did_update_database_field(database, &old_field.id);
view_editor for view_editor in view_editors {
.v_did_update_field_type_option(&old_field) view_editor
.await?; .v_did_update_field_type_option(&old_field)
.await?;
}
Ok(()) Ok(())
} }

View File

@ -363,7 +363,7 @@ impl DatabaseViewEditor {
if let (Some(type_option_data), Some(payload)) = result { if let (Some(type_option_data), Some(payload)) = result {
self self
.delegate .delegate
.update_field(&self.view_id, type_option_data, old_field) .update_field(type_option_data, old_field)
.await?; .await?;
let group_changes = GroupChangesPB { let group_changes = GroupChangesPB {
@ -402,10 +402,7 @@ impl DatabaseViewEditor {
changes.deleted_rows.extend(deleted_rows); changes.deleted_rows.extend(deleted_rows);
if let Some(type_option) = type_option_data { if let Some(type_option) = type_option_data {
self self.delegate.update_field(type_option, field).await?;
.delegate
.update_field(&self.view_id, type_option, field)
.await?;
} }
let notification = GroupChangesPB { let notification = GroupChangesPB {
view_id: self.view_id.clone(), view_id: self.view_id.clone(),
@ -436,7 +433,7 @@ impl DatabaseViewEditor {
if !type_option_data.is_empty() { if !type_option_data.is_empty() {
self self
.delegate .delegate
.update_field(&self.view_id, type_option_data, old_field) .update_field(type_option_data, old_field)
.await?; .await?;
} }
let notification = GroupChangesPB { let notification = GroupChangesPB {

View File

@ -41,7 +41,6 @@ pub trait DatabaseViewOperation: Send + Sync + 'static {
fn update_field( fn update_field(
&self, &self,
view_id: &str,
type_option_data: TypeOptionData, type_option_data: TypeOptionData,
old_field: Field, old_field: Field,
) -> FutureResult<(), FlowyError>; ) -> FutureResult<(), FlowyError>;

View File

@ -9,7 +9,6 @@ use crate::services::database::DatabaseEditor;
use crate::services::field::{MultiSelectTypeOption, SingleSelectTypeOption}; use crate::services::field::{MultiSelectTypeOption, SingleSelectTypeOption};
pub async fn edit_field_type_option<T: From<TypeOptionData> + Into<TypeOptionData>>( pub async fn edit_field_type_option<T: From<TypeOptionData> + Into<TypeOptionData>>(
view_id: &str,
field_id: &str, field_id: &str,
editor: Arc<DatabaseEditor>, editor: Arc<DatabaseEditor>,
action: impl FnOnce(&mut T), action: impl FnOnce(&mut T),
@ -25,7 +24,7 @@ pub async fn edit_field_type_option<T: From<TypeOptionData> + Into<TypeOptionDat
action(&mut type_option); action(&mut type_option);
let type_option_data: TypeOptionData = type_option.into(); let type_option_data: TypeOptionData = type_option.into();
editor editor
.update_field_type_option(view_id, field_id, type_option_data, old_field) .update_field_type_option(field_id, type_option_data, old_field)
.await?; .await?;
} }
} }
@ -34,19 +33,17 @@ pub async fn edit_field_type_option<T: From<TypeOptionData> + Into<TypeOptionDat
} }
pub async fn edit_single_select_type_option( pub async fn edit_single_select_type_option(
view_id: &str,
field_id: &str, field_id: &str,
editor: Arc<DatabaseEditor>, editor: Arc<DatabaseEditor>,
action: impl FnOnce(&mut SingleSelectTypeOption), action: impl FnOnce(&mut SingleSelectTypeOption),
) -> FlowyResult<()> { ) -> FlowyResult<()> {
edit_field_type_option(view_id, field_id, editor, action).await edit_field_type_option(field_id, editor, action).await
} }
pub async fn edit_multi_select_type_option( pub async fn edit_multi_select_type_option(
view_id: &str,
field_id: &str, field_id: &str,
editor: Arc<DatabaseEditor>, editor: Arc<DatabaseEditor>,
action: impl FnOnce(&mut MultiSelectTypeOption), action: impl FnOnce(&mut MultiSelectTypeOption),
) -> FlowyResult<()> { ) -> FlowyResult<()> {
edit_field_type_option(view_id, field_id, editor, action).await edit_field_type_option(field_id, editor, action).await
} }

View File

@ -99,7 +99,7 @@ impl DatabaseFieldTest {
let old_field = self.editor.get_field(&field_id).unwrap(); let old_field = self.editor.get_field(&field_id).unwrap();
self self
.editor .editor
.update_field_type_option(&self.view_id, &field_id, type_option, old_field) .update_field_type_option(&field_id, type_option, old_field)
.await .await
.unwrap(); .unwrap();
}, },

View File

@ -306,14 +306,9 @@ impl DatabaseGroupTest {
action: impl FnOnce(&mut SingleSelectTypeOption), action: impl FnOnce(&mut SingleSelectTypeOption),
) { ) {
let single_select = self.get_single_select_field().await; let single_select = self.get_single_select_field().await;
edit_single_select_type_option( edit_single_select_type_option(&single_select.id, self.editor.clone(), action)
&self.view_id, .await
&single_select.id, .unwrap();
self.editor.clone(),
action,
)
.await
.unwrap();
} }
pub async fn get_url_field(&self) -> Field { pub async fn get_url_field(&self) -> Field {