resolve method

  1. @override
Future<DatumConflictResolution<T>> resolve({
  1. T? local,
  2. T? remote,
  3. required DatumConflictContext context,
})
override

Resolves a conflict between a local and remote version of an entity.

Implementation

@override
Future<DatumConflictResolution<T>> resolve({
  T? local,
  T? remote,
  required DatumConflictContext context,
}) async {
  final choice = await onPrompt(context, local, remote);

  switch (choice) {
    case DatumResolutionStrategy.takeLocal:
      if (local == null) {
        return DatumConflictResolution.abort(
          'Local data unavailable for chosen strategy.',
        );
      }
      return DatumConflictResolution.useLocal(local);
    case DatumResolutionStrategy.takeRemote:
      if (remote == null) {
        return DatumConflictResolution.abort(
          'Remote data unavailable for chosen strategy.',
        );
      }
      return DatumConflictResolution.useRemote(remote);
    case DatumResolutionStrategy.merge:
      if (onMerge == null) {
        return DatumConflictResolution.abort(
          'Merge strategy chosen, but no `onMerge` function was provided.',
        );
      }
      if (local == null || remote == null) {
        return DatumConflictResolution.abort(
          'Merge requires both local and remote data to be available.',
        );
      }
      final merged = await onMerge!(local, remote, context);
      if (merged == null) {
        return DatumConflictResolution.abort(
          'User cancelled merge operation.',
        );
      }
      return DatumConflictResolution.merge(merged);
    case DatumResolutionStrategy.askUser:
      // This case indicates the UI wants to handle it, but the resolver needs a concrete action.
      return DatumConflictResolution.requireUserInput(
        'Additional user input required.',
      );
    case DatumResolutionStrategy.abort:
      return DatumConflictResolution.abort('User cancelled resolution.');
  }
}