applyUpdates method

  1. @override
Future<FdcDataApplyResult> applyUpdates(
  1. FdcChangeSet changes
)
override

Applies a dataset change set to the backend.

All adapters use backend-confirmed immediate semantics. In immediate update mode, the dataset may complete the local post/delete transition before this asynchronous backend call finishes, but it keeps the posted edit/insert/delete state recoverable until the apply result is confirmed. If the adapter returns errors or throws, the dataset normalizes the failure, restores the rejected dirty state when needed, and emits the canonical dataset error event.

Implementation

@override
Future<FdcDataApplyResult> applyUpdates(FdcChangeSet changes) async {
  if (changes.isEmpty) {
    return const FdcDataApplyResult.success();
  }

  final nextRows = _rows.map((row) => row.copy()).toList();
  final keyFields = changes.fields
      .where((field) => field.isKey)
      .toList(growable: false);

  for (final delete in changes.deletes) {
    if (!_removeEntry(nextRows, delete, keyFields)) {
      return _missingEntryApplyResult(delete, operation: 'Delete');
    }
  }

  for (final update in changes.updates) {
    final row = _findEntryRow(nextRows, update, keyFields);
    if (row == null) {
      return _missingEntryApplyResult(update, operation: 'Update');
    }
    _mergeCaseInsensitive(row.values, update.values);
  }

  for (final insert in changes.inserts) {
    nextRows.add(
      _FdcMemoryRow(
        identity: insert.recordId,
        values: Map<String, Object?>.of(insert.values),
      ),
    );
    if (insert.recordId >= _nextRowIdentity) {
      _nextRowIdentity = insert.recordId + 1;
    }
  }

  _rows
    ..clear()
    ..addAll(nextRows);

  return const FdcDataApplyResult.success();
}