undelegate<K, E> method

MapDiffs<K, E> undelegate<K, E>(
  1. MapDiffArguments<K, E> args
)

This occurs when the diff was delegated - which means that instead of passing the real instance to the isolate, we passed a safe delegate. When the method returns, we need to rehydrate the results by looking the delegates up in a map.

Implementation

MapDiffs<K, E> undelegate<K, E>(MapDiffArguments<K, E> args) {
  final delegateArgs = args as MapDiffArguments<K, DiffDelegate>;
  // Create a mapping of diffKey to replacement
  final reverseMapping = Map.fromEntries(delegateArgs.replacement.values
      .map((d) => MapEntry(d.diffKey, d as E)));

  return MapDiffs<K, E>.ofOperations(
    this.operations.map((final _) {
      final op = _.recast<K, DiffDelegate>(delegateArgs);
      switch (op.type) {
        case MapDiffType.change:
          return MapDiff<K, E>.change(
              args,
              op.key,
              reverseMapping[op.oldValue!.diffKey],
              reverseMapping[op.value!.diffKey]);
        case MapDiffType.unset:
          return MapDiff<K, E>.unset(
              args, op.key, reverseMapping[op.oldValue!.diffKey]);
        case MapDiffType.set:
          return MapDiff<K, E>.set(
              args, op.key, reverseMapping[op.value!.diffKey]);
        default:
          throw "Invalid type";
      }
    }).toList(growable: false),
    args,
  );
}