chore: optimize cell cache

This commit is contained in:
appflowy 2022-08-29 18:15:17 +08:00
parent af3bfebb64
commit 32a1bbb6b9
4 changed files with 15 additions and 5 deletions

View File

@ -11,13 +11,15 @@ typedef UpdateFieldNotifiedValue = Either<Unit, 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});
void start({required void Function(UpdateFieldNotifiedValue) onCellChanged}) { void start({required void Function(UpdateFieldNotifiedValue) onCellChanged}) {
_updateCellNotifier?.addPublishListener(onCellChanged); _updateCellNotifier?.addPublishListener(onCellChanged);
_listener = GridNotificationListener(objectId: "$rowId:$fieldId", handler: _handler); _listener = GridNotificationListener(
objectId: "$rowId:$fieldId", handler: _handler);
} }
void _handler(GridNotification ty, Either<Uint8List, FlowyError> result) { void _handler(GridNotification ty, Either<Uint8List, FlowyError> result) {

View File

@ -33,10 +33,17 @@ class GridCellCache {
required this.gridId, required this.gridId,
}); });
void remove(String fieldId) { void removeCellWithFieldId(String fieldId) {
_cellDataByFieldId.remove(fieldId); _cellDataByFieldId.remove(fieldId);
} }
void remove(GridCellCacheKey key) {
var map = _cellDataByFieldId[key.fieldId];
if (map != null) {
map.remove(key.rowId);
}
}
void insert<T extends GridCell>(GridCellCacheKey key, T value) { void insert<T extends GridCell>(GridCellCacheKey key, T value) {
var map = _cellDataByFieldId[key.fieldId]; var map = _cellDataByFieldId[key.fieldId];
if (map == null) { if (map == null) {

View File

@ -191,7 +191,7 @@ class IGridCellController<T, D> extends Equatable {
_cellListener?.start(onCellChanged: (result) { _cellListener?.start(onCellChanged: (result) {
result.fold( result.fold(
(_) { (_) {
_cellsCache.remove(fieldId); _cellsCache.remove(_cacheKey);
_loadData(); _loadData();
}, },
(err) => Log.error(err), (err) => Log.error(err),

View File

@ -52,7 +52,8 @@ class GridRowCache {
// //
notifier.onRowFieldsChanged(() => _rowChangeReasonNotifier notifier.onRowFieldsChanged(() => _rowChangeReasonNotifier
.receive(const RowsChangedReason.fieldDidChange())); .receive(const RowsChangedReason.fieldDidChange()));
notifier.onRowFieldChanged((field) => _cellCache.remove(field.id)); notifier.onRowFieldChanged(
(field) => _cellCache.removeCellWithFieldId(field.id));
_rowInfos = block.rows.map((rowPB) => buildGridRow(rowPB)).toList(); _rowInfos = block.rows.map((rowPB) => buildGridRow(rowPB)).toList();
} }