singleCallbackPortWithTimeout<P> function
SendPort
singleCallbackPortWithTimeout<P>(
- void callback(
- P response
- Duration? timeout,
- 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;
}