updateData method

  1. @override
List<ResEvent> updateData(
  1. dynamic newData
)
override

Implementation

@override
List<ResEvent> updateData(dynamic newData) {
  final evts = <ResEvent>[];

  if (newData is! List<dynamic>) {
    throw InvalidMessageException(
        'tried to update the collection $rid with ${newData.runtimeType}',
        newData);
  }

  final a = _items;
  final b =
      newData.map((e) => _client.parseValue(e, addIndirect: false)).toList();
  _patchDiff(a, b, onAdd: (value, idx) {
    _items.insert(idx, value);
    if (value is ResEntry) {
      final cacheItem = _client.get(value.rid);
      if (cacheItem == null) {
        throw CacheInconsistenyException(
            'cacheItem ${value.rid} not available while resolving the update for $rid',
            newData);
      }
      cacheItem.addIndirectReference();
    }
    evts.add(CollectionAddEvent(rid, idx, value));
  }, onRemove: (idx) {
    final value = _items.removeAt(idx);
    if (value is ResEntry) {
      final cacheItem = _client.get(value.rid);
      if (cacheItem == null) {
        throw CacheInconsistenyException(
            'cacheItem ${value.rid} not available while resolving the update for $rid',
            newData);
      }
      cacheItem.addIndirectReference(value: -1);
      // TODO: try delete
    }
  });

  return evts;
}