calculateDiffInSeparateIsolate<Item> function

Future<List<Operation<Item>>> calculateDiffInSeparateIsolate<Item>(
  1. List<Item> oldList,
  2. List<Item> newList,
  3. bool areEqual(
    1. Item a,
    2. Item b
    ),
  4. int getHashCode(
    1. Item item
    ),
)

Implementation

Future<List<Operation<Item>>> calculateDiffInSeparateIsolate<Item>(
  List<Item> oldList,
  List<Item> newList,
  bool Function(Item a, Item b) areEqual,
  int Function(Item item) getHashCode,
) async {
  final receivePort = ReceivePort();
  await Isolate.spawn(_calculationInIsolate, receivePort.sendPort);
  final port = StreamQueue(receivePort);
  final SendPort sendPort = await port.next;

  _sendItemList(sendPort, oldList, getHashCode);
  _sendItemList(sendPort, newList, getHashCode);

  while (!await port.next) {
    // Two items' hashCodes match. Let's find out if they're really the same.
    final first = oldList[await port.next];
    final second = newList[await port.next];
    sendPort.send(areEqual(first, second));
  }

  final operations = await _receiveOperationsList(port, oldList, newList);
  receivePort.close();
  return operations;
}