undelegate<E> method

ListDiffs<E> undelegate<E>(
  1. ListDiffArguments<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

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

  return ListDiffs<E>.ofOperations(
      this.operations.map((final op) {
        if (op is DeleteDiff) {
          return DeleteDiff<E>(args, op.index, op.size);
        } else if (op is InsertDiff) {
          return InsertDiff<E>(
              args,
              op.index!,
              op.size,
              op.items
                  .map((delegate) => reverseMapping[delegate.diffKey])
                  .notNullList());
        } else if (op is ReplaceDiff) {
          return ReplaceDiff<E>(
              args,
              op.index!,
              op.size,
              op.items
                  .map((delegate) => reverseMapping[delegate.diffKey])
                  .notNull()
                  .toList());
        } else {
          throw Exception("Unknown type");
        }
      }).toList(growable: false),
      args.withId(this.args.id));
}