undelegate<E> method

SetDiffs<E> undelegate<E>(
  1. SetDiffArguments<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

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

  return SetDiffs<E>.ofOperations(
    operations.map<SetDiff<E>>((final op) {
      switch (op.type) {
        case SetDiffType.update:
          final opCast = (op as UpdateDiff<DiffDelegate>);
          return UpdateDiff(
            args,
            reverseMapping[opCast.oldValue.diffKey]!,
            reverseMapping[opCast.newValue.diffKey]!,
          );
        case SetDiffType.remove:
          return SetDiff.remove(
              args,
              op.items
                  .map((delegate) => reverseMapping[delegate.diffKey])
                  .notNullSet());

        case SetDiffType.add:
          return SetDiff.add(
              args,
              op.items.map((delegate) {
                return reverseMapping[delegate.diffKey];
              }).notNullSet());
        default:
          throw "Invalid type";
      }
    }).toList(growable: false),
    replacement
        .map((delegate) => reverseMapping[delegate.diffKey])
        .notNull()
        .toSet(),
    args,
  );
}