restoreRemovedEntity method

void restoreRemovedEntity(
  1. EntityRemovalSnapshot snapshot
)

Restore a failed optimistic removal from its graph-owned receipt.

Implementation

void restoreRemovedEntity(EntityRemovalSnapshot snapshot) {
  _write(() {
    final entity = snapshot.entity;
    if (entity != null) {
      _entities.putIfAbsent(snapshot.type, () => {});
      _entities[snapshot.type]![snapshot.id] = Map.of(entity);
    }
    final patch = snapshot.patch;
    if (patch != null) {
      _patches.putIfAbsent(snapshot.type, () => {});
      _patches[snapshot.type]![snapshot.id] = Map.of(patch);
    }
    final key = _ek(snapshot.type, snapshot.id);
    if (snapshot.state != null) _entityStates[key] = snapshot.state!;
    if (snapshot.syncMetadata != null) {
      _syncMetadata[key] = snapshot.syncMetadata!;
    }
    for (final entry in snapshot.listIndexes.entries) {
      final previous = _lists[entry.key] ?? ListState.empty;
      final ids = previous.ids.where((item) => item != snapshot.id).toList();
      ids.insert(entry.value.clamp(0, ids.length), snapshot.id);
      _lists[entry.key] = previous.copyWith(
        ids: List.unmodifiable(ids),
        total: previous.total == null ? null : previous.total! + 1,
      );
      _notifyList(entry.key);
    }
    _notifyEntity(snapshot.type, snapshot.id);
  });
}