Ver Fonte

chore: set notifier to null after dispose

appflowy há 3 anos atrás
pai
commit
d71e0de8c3

+ 5 - 4
frontend/app_flowy/lib/workspace/application/grid/cell_bloc/cell_listener.dart

@@ -12,7 +12,7 @@ typedef UpdateFieldNotifiedValue = Either<CellNotificationData, FlowyError>;
 class CellListener {
   final String rowId;
   final String fieldId;
-  PublishNotifier<UpdateFieldNotifiedValue> updateCellNotifier = PublishNotifier();
+  PublishNotifier<UpdateFieldNotifiedValue>? updateCellNotifier = PublishNotifier();
   GridNotificationListener? _listener;
   CellListener({required this.rowId, required this.fieldId});
 
@@ -24,8 +24,8 @@ class CellListener {
     switch (ty) {
       case GridNotification.DidUpdateCell:
         result.fold(
-          (payload) => updateCellNotifier.value = left(CellNotificationData.fromBuffer(payload)),
-          (error) => updateCellNotifier.value = right(error),
+          (payload) => updateCellNotifier?.value = left(CellNotificationData.fromBuffer(payload)),
+          (error) => updateCellNotifier?.value = right(error),
         );
         break;
       default:
@@ -35,6 +35,7 @@ class CellListener {
 
   Future<void> stop() async {
     await _listener?.stop();
-    updateCellNotifier.dispose();
+    updateCellNotifier?.dispose();
+    updateCellNotifier = null;
   }
 }

+ 1 - 1
frontend/app_flowy/lib/workspace/application/grid/cell_bloc/checkbox_cell_bloc.dart

@@ -43,7 +43,7 @@ class CheckboxCellBloc extends Bloc<CheckboxCellEvent, CheckboxCellState> {
   }
 
   void _startListening() {
-    _listener.updateCellNotifier.addPublishListener((result) {
+    _listener.updateCellNotifier?.addPublishListener((result) {
       result.fold(
         (notificationData) async => await _loadCellData(),
         (err) => Log.error(err),

+ 2 - 2
frontend/app_flowy/lib/workspace/application/grid/cell_bloc/date_cell_bloc.dart

@@ -52,7 +52,7 @@ class DateCellBloc extends Bloc<DateCellEvent, DateCellState> {
   }
 
   void _startListening() {
-    _cellListener.updateCellNotifier.addPublishListener((result) {
+    _cellListener.updateCellNotifier?.addPublishListener((result) {
       result.fold(
         (notificationData) => _loadCellData(),
         (err) => Log.error(err),
@@ -60,7 +60,7 @@ class DateCellBloc extends Bloc<DateCellEvent, DateCellState> {
     });
     _cellListener.start();
 
-    _fieldListener.updateFieldNotifier.addPublishListener((result) {
+    _fieldListener.updateFieldNotifier?.addPublishListener((result) {
       result.fold(
         (field) {
           if (!isClosed) {

+ 2 - 2
frontend/app_flowy/lib/workspace/application/grid/cell_bloc/number_cell_bloc.dart

@@ -59,7 +59,7 @@ class NumberCellBloc extends Bloc<NumberCellEvent, NumberCellState> {
   }
 
   void _startListening() {
-    _cellListener.updateCellNotifier.addPublishListener((result) {
+    _cellListener.updateCellNotifier?.addPublishListener((result) {
       result.fold(
         (notificationData) async {
           await _getCellData();
@@ -69,7 +69,7 @@ class NumberCellBloc extends Bloc<NumberCellEvent, NumberCellState> {
     });
     _cellListener.start();
 
-    _fieldListener.updateFieldNotifier.addPublishListener((result) {
+    _fieldListener.updateFieldNotifier?.addPublishListener((result) {
       result.fold(
         (field) => _getCellData(),
         (err) => Log.error(err),

+ 2 - 2
frontend/app_flowy/lib/workspace/application/grid/cell_bloc/selection_cell_bloc.dart

@@ -64,7 +64,7 @@ class SelectionCellBloc extends Bloc<SelectionCellEvent, SelectionCellState> {
   }
 
   void _startListening() {
-    _cellListener.updateCellNotifier.addPublishListener((result) {
+    _cellListener.updateCellNotifier?.addPublishListener((result) {
       result.fold(
         (notificationData) => _loadOptions(),
         (err) => Log.error(err),
@@ -72,7 +72,7 @@ class SelectionCellBloc extends Bloc<SelectionCellEvent, SelectionCellState> {
     });
     _cellListener.start();
 
-    _fieldListener.updateFieldNotifier.addPublishListener((result) {
+    _fieldListener.updateFieldNotifier?.addPublishListener((result) {
       result.fold(
         (field) => _loadOptions(),
         (err) => Log.error(err),

+ 2 - 2
frontend/app_flowy/lib/workspace/application/grid/cell_bloc/selection_editor_bloc.dart

@@ -134,7 +134,7 @@ class SelectOptionEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOptionE
   }
 
   void _startListening() {
-    _cellListener.updateCellNotifier.addPublishListener((result) {
+    _cellListener.updateCellNotifier?.addPublishListener((result) {
       result.fold(
         (notificationData) => _loadOptions(),
         (err) => Log.error(err),
@@ -142,7 +142,7 @@ class SelectOptionEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOptionE
     });
     _cellListener.start();
 
-    _fieldListener.updateFieldNotifier.addPublishListener((result) {
+    _fieldListener.updateFieldNotifier?.addPublishListener((result) {
       result.fold(
         (field) {
           if (!isClosed) {

+ 1 - 1
frontend/app_flowy/lib/workspace/application/grid/field/field_cell_bloc.dart

@@ -45,7 +45,7 @@ class FieldCellBloc extends Bloc<FieldCellEvent, FieldCellState> {
   }
 
   void _startListening() {
-    _fieldListener.updateFieldNotifier.addPublishListener((result) {
+    _fieldListener.updateFieldNotifier?.addPublishListener((result) {
       result.fold(
         (field) {
           if (!isClosed) {

+ 5 - 4
frontend/app_flowy/lib/workspace/application/grid/field/field_listener.dart

@@ -11,7 +11,7 @@ typedef UpdateFieldNotifiedValue = Either<Field, FlowyError>;
 
 class SingleFieldListener {
   final String fieldId;
-  PublishNotifier<UpdateFieldNotifiedValue> updateFieldNotifier = PublishNotifier();
+  PublishNotifier<UpdateFieldNotifiedValue>? updateFieldNotifier = PublishNotifier();
   GridNotificationListener? _listener;
 
   SingleFieldListener({required this.fieldId});
@@ -30,8 +30,8 @@ class SingleFieldListener {
     switch (ty) {
       case GridNotification.DidUpdateField:
         result.fold(
-          (payload) => updateFieldNotifier.value = left(Field.fromBuffer(payload)),
-          (error) => updateFieldNotifier.value = right(error),
+          (payload) => updateFieldNotifier?.value = left(Field.fromBuffer(payload)),
+          (error) => updateFieldNotifier?.value = right(error),
         );
         break;
       default:
@@ -41,6 +41,7 @@ class SingleFieldListener {
 
   Future<void> stop() async {
     await _listener?.stop();
-    updateFieldNotifier.dispose();
+    updateFieldNotifier?.dispose();
+    updateFieldNotifier = null;
   }
 }

+ 5 - 4
frontend/app_flowy/lib/workspace/application/grid/field/grid_listenr.dart

@@ -11,7 +11,7 @@ typedef UpdateFieldNotifiedValue = Either<GridFieldChangeset, FlowyError>;
 
 class GridFieldsListener {
   final String gridId;
-  PublishNotifier<UpdateFieldNotifiedValue> updateFieldsNotifier = PublishNotifier();
+  PublishNotifier<UpdateFieldNotifiedValue>? updateFieldsNotifier = PublishNotifier();
   GridNotificationListener? _listener;
   GridFieldsListener({required this.gridId});
 
@@ -26,8 +26,8 @@ class GridFieldsListener {
     switch (ty) {
       case GridNotification.DidUpdateGridField:
         result.fold(
-          (payload) => updateFieldsNotifier.value = left(GridFieldChangeset.fromBuffer(payload)),
-          (error) => updateFieldsNotifier.value = right(error),
+          (payload) => updateFieldsNotifier?.value = left(GridFieldChangeset.fromBuffer(payload)),
+          (error) => updateFieldsNotifier?.value = right(error),
         );
         break;
       default:
@@ -37,6 +37,7 @@ class GridFieldsListener {
 
   Future<void> stop() async {
     await _listener?.stop();
-    updateFieldsNotifier.dispose();
+    updateFieldsNotifier?.dispose();
+    updateFieldsNotifier = null;
   }
 }

+ 1 - 1
frontend/app_flowy/lib/workspace/application/grid/grid_bloc.dart

@@ -58,7 +58,7 @@ class GridBloc extends Bloc<GridEvent, GridState> {
   }
 
   void _startListening() {
-    _fieldListener.updateFieldsNotifier.addPublishListener((result) {
+    _fieldListener.updateFieldsNotifier?.addPublishListener((result) {
       result.fold(
         (changeset) {
           fieldCache.applyChangeset(changeset);

+ 1 - 1
frontend/app_flowy/lib/workspace/application/grid/row/row_bloc.dart

@@ -73,7 +73,7 @@ class RowBloc extends Bloc<RowEvent, RowState> {
   }
 
   Future<void> _startListening() async {
-    _rowlistener.updateRowNotifier.addPublishListener((result) {
+    _rowlistener.updateRowNotifier?.addPublishListener((result) {
       result.fold(
         (row) => add(RowEvent.didUpdateRow(row)),
         (err) => Log.error(err),

+ 5 - 4
frontend/app_flowy/lib/workspace/application/grid/row/row_listener.dart

@@ -12,7 +12,7 @@ typedef UpdateFieldNotifiedValue = Either<List<Field>, FlowyError>;
 
 class RowListener {
   final String rowId;
-  PublishNotifier<UpdateRowNotifiedValue> updateRowNotifier = PublishNotifier();
+  PublishNotifier<UpdateRowNotifiedValue>? updateRowNotifier = PublishNotifier();
   GridNotificationListener? _listener;
 
   RowListener({required this.rowId});
@@ -25,8 +25,8 @@ class RowListener {
     switch (ty) {
       case GridNotification.DidUpdateRow:
         result.fold(
-          (payload) => updateRowNotifier.value = left(Row.fromBuffer(payload)),
-          (error) => updateRowNotifier.value = right(error),
+          (payload) => updateRowNotifier?.value = left(Row.fromBuffer(payload)),
+          (error) => updateRowNotifier?.value = right(error),
         );
         break;
       default:
@@ -36,6 +36,7 @@ class RowListener {
 
   Future<void> stop() async {
     await _listener?.stop();
-    updateRowNotifier.dispose();
+    updateRowNotifier?.dispose();
+    updateRowNotifier = null;
   }
 }

+ 1 - 1
frontend/app_flowy/lib/workspace/application/grid/setting/property_bloc.dart

@@ -51,7 +51,7 @@ class GridPropertyBloc extends Bloc<GridPropertyEvent, GridPropertyState> {
   }
 
   void _startListening() {
-    _fieldListener.updateFieldsNotifier.addPublishListener((result) {
+    _fieldListener.updateFieldsNotifier?.addPublishListener((result) {
       result.fold(
         (changeset) {
           _fieldCache.applyChangeset(changeset);

+ 2 - 1
frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/header/grid_header.dart

@@ -25,6 +25,7 @@ class GridHeaderSliverAdaptor extends StatelessWidget {
       create: (context) =>
           getIt<GridHeaderBloc>(param1: gridId, param2: fieldCache)..add(const GridHeaderEvent.initial()),
       child: BlocBuilder<GridHeaderBloc, GridHeaderState>(
+        buildWhen: (previous, current) => previous.fields.length != current.fields.length,
         builder: (context, state) {
           return SliverPersistentHeader(
             delegate: SliverHeaderDelegateImplementation(gridId: gridId, fields: state.fields),
@@ -45,7 +46,7 @@ class SliverHeaderDelegateImplementation extends SliverPersistentHeaderDelegate
 
   @override
   Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
-    return _GridHeader(gridId: gridId, fields: fields, key: ObjectKey(fields));
+    return _GridHeader(gridId: gridId, fields: fields);
   }
 
   @override