Skip to content

Commit 54fcc4a

Browse files
authored
fix: notify all views of field type option (#4229)
1 parent 2ca9119 commit 54fcc4a

File tree

7 files changed

+40
-53
lines changed

7 files changed

+40
-53
lines changed

frontend/rust-lib/flowy-database2/src/event_handler.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,7 @@ pub(crate) async fn update_field_type_option_handler(
219219
let field_type = FieldType::from(old_field.field_type);
220220
let type_option_data = type_option_data_from_pb(params.type_option_data, &field_type)?;
221221
database_editor
222-
.update_field_type_option(
223-
&params.view_id,
224-
&params.field_id,
225-
type_option_data,
226-
old_field,
227-
)
222+
.update_field_type_option(&params.field_id, type_option_data, old_field)
228223
.await?;
229224
}
230225
Ok(())
@@ -262,12 +257,7 @@ pub(crate) async fn switch_to_field_handler(
262257
match (old_field, new_type_option) {
263258
(Some(old_field), Some(new_type_option)) => {
264259
database_editor
265-
.update_field_type_option(
266-
&params.view_id,
267-
&params.field_id,
268-
new_type_option,
269-
old_field,
270-
)
260+
.update_field_type_option(&params.field_id, new_type_option, old_field)
271261
.await?;
272262
},
273263
_ => {

frontend/rust-lib/flowy-database2/src/services/database/database_editor.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,12 @@ impl DatabaseEditor {
317317
/// Do nothing if the [TypeOptionData] is empty.
318318
pub async fn update_field_type_option(
319319
&self,
320-
view_id: &str,
321320
_field_id: &str,
322321
type_option_data: TypeOptionData,
323322
old_field: Field,
324323
) -> FlowyResult<()> {
325-
let view_editor = self.database_views.get_view_editor(view_id).await?;
326-
update_field_type_option_fn(&self.database, &view_editor, type_option_data, old_field).await?;
324+
let view_editors = self.database_views.editors().await;
325+
update_field_type_option_fn(&self.database, &view_editors, type_option_data, old_field).await?;
327326

328327
Ok(())
329328
}
@@ -808,9 +807,15 @@ impl DatabaseEditor {
808807
.for_each(|option| type_option.insert_option(option.into()));
809808

810809
// Update the field's type option
811-
self
812-
.update_field_type_option(view_id, field_id, type_option.to_type_option_data(), field)
813-
.await?;
810+
let view_editors = self.database_views.editors().await;
811+
update_field_type_option_fn(
812+
&self.database,
813+
&view_editors,
814+
type_option.to_type_option_data(),
815+
field.clone(),
816+
)
817+
.await?;
818+
814819
// Insert the options into the cell
815820
self
816821
.update_cell_with_changeset(view_id, row_id, field_id, cell_changeset)
@@ -842,10 +847,10 @@ impl DatabaseEditor {
842847
type_option.delete_option(&option.id);
843848
}
844849

845-
let view_editor = self.database_views.get_view_editor(view_id).await?;
850+
let view_editors = self.database_views.editors().await;
846851
update_field_type_option_fn(
847852
&self.database,
848-
&view_editor,
853+
&view_editors,
849854
type_option.to_type_option_data(),
850855
field.clone(),
851856
)
@@ -1290,22 +1295,23 @@ impl DatabaseViewOperation for DatabaseViewOperationImpl {
12901295

12911296
fn update_field(
12921297
&self,
1293-
view_id: &str,
12941298
type_option_data: TypeOptionData,
12951299
old_field: Field,
12961300
) -> FutureResult<(), FlowyError> {
1297-
let view_id = view_id.to_string();
12981301
let weak_editor_by_view_id = Arc::downgrade(&self.editor_by_view_id);
12991302
let weak_database = Arc::downgrade(&self.database);
13001303
FutureResult::new(async move {
13011304
if let (Some(database), Some(editor_by_view_id)) =
13021305
(weak_database.upgrade(), weak_editor_by_view_id.upgrade())
13031306
{
1304-
let view_editor = editor_by_view_id.read().await.get(&view_id).cloned();
1305-
if let Some(view_editor) = view_editor {
1306-
let _ =
1307-
update_field_type_option_fn(&database, &view_editor, type_option_data, old_field).await;
1308-
}
1307+
let view_editors = editor_by_view_id
1308+
.read()
1309+
.await
1310+
.values()
1311+
.map(|editor| editor.clone())
1312+
.collect();
1313+
let _ =
1314+
update_field_type_option_fn(&database, &view_editors, type_option_data, old_field).await;
13091315
}
13101316
Ok(())
13111317
})
@@ -1573,7 +1579,7 @@ impl DatabaseViewOperation for DatabaseViewOperationImpl {
15731579
#[tracing::instrument(level = "trace", skip_all, err)]
15741580
pub async fn update_field_type_option_fn(
15751581
database: &Arc<MutexDatabase>,
1576-
view_editor: &Arc<DatabaseViewEditor>,
1582+
view_editors: &Vec<Arc<DatabaseViewEditor>>,
15771583
type_option_data: TypeOptionData,
15781584
old_field: Field,
15791585
) -> FlowyResult<()> {
@@ -1601,9 +1607,12 @@ pub async fn update_field_type_option_fn(
16011607
});
16021608

16031609
let _ = notify_did_update_database_field(database, &old_field.id);
1604-
view_editor
1605-
.v_did_update_field_type_option(&old_field)
1606-
.await?;
1610+
for view_editor in view_editors {
1611+
view_editor
1612+
.v_did_update_field_type_option(&old_field)
1613+
.await?;
1614+
}
1615+
16071616
Ok(())
16081617
}
16091618

frontend/rust-lib/flowy-database2/src/services/database_view/view_editor.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ impl DatabaseViewEditor {
363363
if let (Some(type_option_data), Some(payload)) = result {
364364
self
365365
.delegate
366-
.update_field(&self.view_id, type_option_data, old_field)
366+
.update_field(type_option_data, old_field)
367367
.await?;
368368

369369
let group_changes = GroupChangesPB {
@@ -402,10 +402,7 @@ impl DatabaseViewEditor {
402402
changes.deleted_rows.extend(deleted_rows);
403403

404404
if let Some(type_option) = type_option_data {
405-
self
406-
.delegate
407-
.update_field(&self.view_id, type_option, field)
408-
.await?;
405+
self.delegate.update_field(type_option, field).await?;
409406
}
410407
let notification = GroupChangesPB {
411408
view_id: self.view_id.clone(),
@@ -436,7 +433,7 @@ impl DatabaseViewEditor {
436433
if !type_option_data.is_empty() {
437434
self
438435
.delegate
439-
.update_field(&self.view_id, type_option_data, old_field)
436+
.update_field(type_option_data, old_field)
440437
.await?;
441438
}
442439
let notification = GroupChangesPB {

frontend/rust-lib/flowy-database2/src/services/database_view/view_operation.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ pub trait DatabaseViewOperation: Send + Sync + 'static {
4141

4242
fn update_field(
4343
&self,
44-
view_id: &str,
4544
type_option_data: TypeOptionData,
4645
old_field: Field,
4746
) -> FutureResult<(), FlowyError>;

frontend/rust-lib/flowy-database2/src/services/field/field_operation.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::services::database::DatabaseEditor;
99
use crate::services::field::{MultiSelectTypeOption, SingleSelectTypeOption};
1010

1111
pub async fn edit_field_type_option<T: From<TypeOptionData> + Into<TypeOptionData>>(
12-
view_id: &str,
1312
field_id: &str,
1413
editor: Arc<DatabaseEditor>,
1514
action: impl FnOnce(&mut T),
@@ -25,7 +24,7 @@ pub async fn edit_field_type_option<T: From<TypeOptionData> + Into<TypeOptionDat
2524
action(&mut type_option);
2625
let type_option_data: TypeOptionData = type_option.into();
2726
editor
28-
.update_field_type_option(view_id, field_id, type_option_data, old_field)
27+
.update_field_type_option(field_id, type_option_data, old_field)
2928
.await?;
3029
}
3130
}
@@ -34,19 +33,17 @@ pub async fn edit_field_type_option<T: From<TypeOptionData> + Into<TypeOptionDat
3433
}
3534

3635
pub async fn edit_single_select_type_option(
37-
view_id: &str,
3836
field_id: &str,
3937
editor: Arc<DatabaseEditor>,
4038
action: impl FnOnce(&mut SingleSelectTypeOption),
4139
) -> FlowyResult<()> {
42-
edit_field_type_option(view_id, field_id, editor, action).await
40+
edit_field_type_option(field_id, editor, action).await
4341
}
4442

4543
pub async fn edit_multi_select_type_option(
46-
view_id: &str,
4744
field_id: &str,
4845
editor: Arc<DatabaseEditor>,
4946
action: impl FnOnce(&mut MultiSelectTypeOption),
5047
) -> FlowyResult<()> {
51-
edit_field_type_option(view_id, field_id, editor, action).await
48+
edit_field_type_option(field_id, editor, action).await
5249
}

frontend/rust-lib/flowy-database2/tests/database/field_test/script.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl DatabaseFieldTest {
9999
let old_field = self.editor.get_field(&field_id).unwrap();
100100
self
101101
.editor
102-
.update_field_type_option(&self.view_id, &field_id, type_option, old_field)
102+
.update_field_type_option(&field_id, type_option, old_field)
103103
.await
104104
.unwrap();
105105
},

frontend/rust-lib/flowy-database2/tests/database/group_test/script.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,9 @@ impl DatabaseGroupTest {
306306
action: impl FnOnce(&mut SingleSelectTypeOption),
307307
) {
308308
let single_select = self.get_single_select_field().await;
309-
edit_single_select_type_option(
310-
&self.view_id,
311-
&single_select.id,
312-
self.editor.clone(),
313-
action,
314-
)
315-
.await
316-
.unwrap();
309+
edit_single_select_type_option(&single_select.id, self.editor.clone(), action)
310+
.await
311+
.unwrap();
317312
}
318313

319314
pub async fn get_url_field(&self) -> Field {

0 commit comments

Comments
 (0)