go<P, R> static method

Future<Pointer<Void>> go<P, R>(
  1. void computation(
    1. P params,
    2. int portId
    ),
  2. P params, [
  3. int? customPortId
])

执行 Go 代码并等待结果

Implementation

static Future<Pointer<Void>> go<P, R>(
    void Function(P params, int portId) computation, P params,
    [int? customPortId]) async {
  final (receivePort, completer) = _setupCommunication<Pointer<Void>>();
  final sendPort = receivePort.sendPort;
  final portId = customPortId ?? sendPort.nativePort;
  final success =
      IsolateNameServer.registerPortWithName(sendPort, portId.toString());
  if (!success) {
    receivePort.close();
    throw Exception('Failed to register port id: $portId');
  }

  _listen(receivePort, completer, portId);
  try {
    computation(params, portId);
  } catch (e) {
    receivePort.close();
    IsolateNameServer.removePortNameMapping(portId.toString());
    throw Exception('Error executing computation: ${e.toString()}');
  }

  return await completer.future;
}