mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
chore: fix potential issue when call notifier after it was disposed
This commit is contained in:
parent
23900b49f5
commit
e8540b4b78
@ -104,6 +104,8 @@ class GridCellCache {
|
||||
}
|
||||
|
||||
Future<void> dispose() async {
|
||||
_fieldListenerByFieldId.clear();
|
||||
_cellDataByFieldId.clear();
|
||||
fieldDelegate.dispose();
|
||||
}
|
||||
}
|
||||
|
@ -93,7 +93,7 @@ class GridBloc extends Bloc<GridEvent, GridState> {
|
||||
|
||||
emit(state.copyWith(
|
||||
grid: Some(grid),
|
||||
fields: fieldCache.clonedFields,
|
||||
fields: fieldCache.fields,
|
||||
rows: rowCache.clonedRows,
|
||||
loadingState: GridLoadingState.finish(left(unit)),
|
||||
));
|
||||
|
@ -15,7 +15,7 @@ class GridHeaderBloc extends Bloc<GridHeaderEvent, GridHeaderState> {
|
||||
GridHeaderBloc({
|
||||
required this.gridId,
|
||||
required this.fieldCache,
|
||||
}) : super(GridHeaderState.initial(fieldCache.clonedFields)) {
|
||||
}) : super(GridHeaderState.initial(fieldCache.fields)) {
|
||||
on<GridHeaderEvent>(
|
||||
(event, emit) async {
|
||||
await event.map(
|
||||
|
@ -59,7 +59,7 @@ typedef ChangesetListener = void Function(GridFieldChangeset);
|
||||
class GridFieldCache {
|
||||
final String gridId;
|
||||
late final GridFieldsListener _fieldListener;
|
||||
final FieldsNotifier _fieldNotifier = FieldsNotifier();
|
||||
FieldsNotifier? _fieldNotifier = FieldsNotifier();
|
||||
final List<ChangesetListener> _changesetListener = [];
|
||||
|
||||
GridFieldCache({required this.gridId}) {
|
||||
@ -81,15 +81,16 @@ class GridFieldCache {
|
||||
|
||||
Future<void> dispose() async {
|
||||
await _fieldListener.stop();
|
||||
_fieldNotifier.dispose();
|
||||
_fieldNotifier?.dispose();
|
||||
_fieldNotifier = null;
|
||||
}
|
||||
|
||||
UnmodifiableListView<Field> get unmodifiableFields => UnmodifiableListView(_fieldNotifier.fields);
|
||||
UnmodifiableListView<Field> get unmodifiableFields => UnmodifiableListView(_fieldNotifier?.fields ?? []);
|
||||
|
||||
List<Field> get clonedFields => [..._fieldNotifier.fields];
|
||||
List<Field> get fields => [..._fieldNotifier?.fields ?? []];
|
||||
|
||||
set fields(List<Field> fields) {
|
||||
_fieldNotifier.fields = [...fields];
|
||||
_fieldNotifier?.fields = [...fields];
|
||||
}
|
||||
|
||||
VoidCallback addListener(
|
||||
@ -100,7 +101,7 @@ class GridFieldCache {
|
||||
}
|
||||
|
||||
if (onChanged != null) {
|
||||
onChanged(clonedFields);
|
||||
onChanged(fields);
|
||||
}
|
||||
|
||||
if (listener != null) {
|
||||
@ -108,12 +109,12 @@ class GridFieldCache {
|
||||
}
|
||||
}
|
||||
|
||||
_fieldNotifier.addListener(f);
|
||||
_fieldNotifier?.addListener(f);
|
||||
return f;
|
||||
}
|
||||
|
||||
void removeListener(VoidCallback f) {
|
||||
_fieldNotifier.removeListener(f);
|
||||
_fieldNotifier?.removeListener(f);
|
||||
}
|
||||
|
||||
void addChangesetListener(ChangesetListener listener) {
|
||||
@ -131,43 +132,43 @@ class GridFieldCache {
|
||||
if (deletedFields.isEmpty) {
|
||||
return;
|
||||
}
|
||||
final List<Field> fields = _fieldNotifier.fields;
|
||||
final List<Field> newFields = fields;
|
||||
final Map<String, FieldOrder> deletedFieldMap = {
|
||||
for (var fieldOrder in deletedFields) fieldOrder.fieldId: fieldOrder
|
||||
};
|
||||
|
||||
fields.retainWhere((field) => (deletedFieldMap[field.id] == null));
|
||||
_fieldNotifier.fields = fields;
|
||||
newFields.retainWhere((field) => (deletedFieldMap[field.id] == null));
|
||||
_fieldNotifier?.fields = newFields;
|
||||
}
|
||||
|
||||
void _insertFields(List<IndexField> insertedFields) {
|
||||
if (insertedFields.isEmpty) {
|
||||
return;
|
||||
}
|
||||
final List<Field> fields = _fieldNotifier.fields;
|
||||
final List<Field> newFields = fields;
|
||||
for (final indexField in insertedFields) {
|
||||
if (fields.length > indexField.index) {
|
||||
fields.insert(indexField.index, indexField.field_1);
|
||||
if (newFields.length > indexField.index) {
|
||||
newFields.insert(indexField.index, indexField.field_1);
|
||||
} else {
|
||||
fields.add(indexField.field_1);
|
||||
newFields.add(indexField.field_1);
|
||||
}
|
||||
}
|
||||
_fieldNotifier.fields = fields;
|
||||
_fieldNotifier?.fields = newFields;
|
||||
}
|
||||
|
||||
void _updateFields(List<Field> updatedFields) {
|
||||
if (updatedFields.isEmpty) {
|
||||
return;
|
||||
}
|
||||
final List<Field> fields = _fieldNotifier.fields;
|
||||
final List<Field> newFields = fields;
|
||||
for (final updatedField in updatedFields) {
|
||||
final index = fields.indexWhere((field) => field.id == updatedField.id);
|
||||
final index = newFields.indexWhere((field) => field.id == updatedField.id);
|
||||
if (index != -1) {
|
||||
fields.removeAt(index);
|
||||
fields.insert(index, updatedField);
|
||||
newFields.removeAt(index);
|
||||
newFields.insert(index, updatedField);
|
||||
}
|
||||
}
|
||||
_fieldNotifier.fields = fields;
|
||||
_fieldNotifier?.fields = newFields;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@ class GridPropertyBloc extends Bloc<GridPropertyEvent, GridPropertyState> {
|
||||
|
||||
GridPropertyBloc({required String gridId, required GridFieldCache fieldCache})
|
||||
: _fieldCache = fieldCache,
|
||||
super(GridPropertyState.initial(gridId, fieldCache.clonedFields)) {
|
||||
super(GridPropertyState.initial(gridId, fieldCache.fields)) {
|
||||
on<GridPropertyEvent>(
|
||||
(event, emit) async {
|
||||
await event.map(
|
||||
|
Loading…
Reference in New Issue
Block a user