singleCallbackPortWithTimeout<P> function

SendPort singleCallbackPortWithTimeout<P>(
  1. void callback(
    1. P response
    ), {
  2. Duration? timeout,
  3. required P timeoutValue,
})

Same as singleResponseFuture, but with required timeoutValue, this allows us not to require a nullable value in the callback

Implementation

SendPort singleCallbackPortWithTimeout<P>(void Function(P response) callback,
    {Duration? timeout, required P timeoutValue}) {
  var responsePort = RawReceivePort();
  var zone = Zone.current;
  callback = zone.registerUnaryCallback(callback);
  Timer? timer;
  responsePort.handler = (response) {
    responsePort.close();
    timer?.cancel();
    zone.runUnary(callback, response as P);
  };

  if (timeout != null) {
    timer = Timer(timeout, () {
      responsePort.close();
      callback(timeoutValue);
    });
  }

  return responsePort.sendPort;
}