applySnapshot method

Set applySnapshot(
  1. BsatnRowList inserts,
  2. EventContext context, {
  3. required int querySetId,
  4. Set? protectedKeys,
  5. bool reconnectInFlight = false,
})

Implementation

Set<dynamic> applySnapshot(
  BsatnRowList inserts,
  EventContext context, {
  required int querySetId,
  Set<dynamic>? protectedKeys,
  bool reconnectInFlight = false,
}) {
  final changes = _applyChanges(
    BsatnRowList.empty(),
    inserts,
    protectedKeys: protectedKeys,
  );

  final snapshotKeys = changes.serverPresentKeys;

  final toEvict = <dynamic>[];
  for (final entry in _rowOwners.entries.toList()) {
    final pk = entry.key;
    final owners = entry.value;
    if (!owners.contains(querySetId)) continue;
    if (snapshotKeys.contains(pk)) continue;
    owners.remove(querySetId);
    if (owners.isEmpty) {
      _rowOwners.remove(pk);
      if (protectedKeys == null || !protectedKeys.contains(pk)) {
        toEvict.add(pk);
      }
    }
  }

  for (final pk in snapshotKeys) {
    _rowOwners.putIfAbsent(pk, () => <int>{}).add(querySetId);
  }

  if (!reconnectInFlight) {
    for (final row in _rowsByPrimaryKey.values.toList()) {
      final pk = decoder.getPrimaryKey(row);
      if (pk == null) continue;
      if (snapshotKeys.contains(pk)) continue;
      if (protectedKeys != null && protectedKeys.contains(pk)) continue;
      if (_rowOwners.containsKey(pk)) continue;
      toEvict.add(pk);
    }
  }

  for (final pk in toEvict) {
    _evictRow(pk);
  }

  _emitChanges(changes, context);
  if (toEvict.isNotEmpty) {
    _refreshRowsNotifier();
    _notifyRowListeners(toEvict);
  }
  return changes.insertedPrimaryKeys;
}