singleCallbackPortWithTimeout<P> function
SendPort
singleCallbackPortWithTimeout<P>(
- void callback(
- P response
- Duration timeout,
- P timeoutValue
Create a SendPort that accepts only one message.
The callback function is called once, with the first message
received by the receive port.
All further messages are ignored and the port is closed.
If a message has not been received within the timeout duration,
the callback is called with the timeoutValue instead, and
the port is closed.
If the received value is not a P, it will cause an uncaught
asynchronous error in the current zone.
Returns the SendPort expecting the single message.
Equivalent to:
(new ReceivePort()
..first.timeout(duration, () => timeoutValue).then(callback))
.sendPort
Implementation
SendPort singleCallbackPortWithTimeout<P>(
void Function(P response) callback, Duration timeout, 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);
};
timer = Timer(timeout, () {
responsePort.close();
callback(timeoutValue);
});
return responsePort.sendPort;
}