chore: set notifier to null after dispose

This commit is contained in:
appflowy 2022-04-15 14:21:32 +08:00
parent 676dffbf21
commit d71e0de8c3
14 changed files with 35 additions and 30 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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