runWithCallback<Request_T, Response_T, Message_T> method

Future<Response_T> runWithCallback<Request_T, Response_T, Message_T>({
  1. required AsyncTaskWithSend<Request_T, Response_T, Message_T> task,
  2. required AsyncCallback<Message_T> callback,
  3. String? callbackId,
  4. Request_T? request,
})

Implementation

Future<Response_T> runWithCallback<Request_T, Response_T, Message_T>({
  required AsyncTaskWithSend<Request_T, Response_T, Message_T> task,
  required AsyncCallback<Message_T> callback,
  String? callbackId,
  Request_T? request,
}) async {
  await _lock.lock();
  try {
    // Port to the isolate that executes the request.
    final asyncIsolatePort = await _asyncIsolatePort();
    // Create the request.
    final asyncRequest =
        _AsyncRequestWithCallback<Request_T, Response_T, Message_T>(
      task,
      request,
    );
    // Completer used to return the future response.
    final completer = Completer<Response_T>();
    _asyncCalls[asyncRequest.id] = _AsyncCall<Response_T, Message_T>(
      completer,
      asyncRequest,
      null,
      callback,
      callbackId,
    );
    asyncIsolatePort.send(asyncRequest);
    return completer.future;
  } finally {
    _lock.unlock();
  }
}