SingleResponseChannel<R> constructor
SingleResponseChannel<R> ({})
Creates a response channel.
The result is completed with the first value sent to port.
If callback
is provided, the value sent to port is first passed
to callback
, and the result of that is used to complete result
.
If timeout
is provided, the future is completed after that
duration if it hasn't received a value from the port earlier.
If throwOnTimeout
is true, the the future is completed with a
TimeoutException as an error if it times out.
Otherwise, if onTimeout
is provided,
the future is completed with the result of running onTimeout()
.
If onTimeout
is not provided either,
the future is completed with timeoutValue
, which defaults to null
.
Implementation
SingleResponseChannel(
{FutureOr<R> Function(dynamic value)? callback,
Duration? timeout,
bool throwOnTimeout = false,
FutureOr<R> Function()? onTimeout,
R? timeoutValue})
: _receivePort = RawReceivePort(),
_completer = Completer<R?>.sync(),
_callback = callback,
_zone = Zone.current {
_receivePort.handler = _handleResponse;
if (timeout != null) {
_timer = Timer(timeout, () {
// Executed as a timer event.
_receivePort.close();
if (!_completer.isCompleted) {
if (throwOnTimeout) {
_completer.completeError(
TimeoutException('Timeout waiting for response', timeout));
} else if (onTimeout != null) {
_completer.complete(Future.sync(onTimeout));
} else {
_completer.complete(timeoutValue);
}
}
});
}
}