sendWaitWhere method
Sends a message to another side of channel and waits for the response.
Recognition of the response message is performed by the given filter
function.
See Filters for some useful filter implementations.
If timeout
is given and there's no suitable response message within the timeout,
method fails with TimeoutException.
Implementation
Future<Message> sendWaitWhere(Message message, Filter filter,
{Duration? timeout}) {
var comp = Completer<Message>();
var lsid = listener.prependListener((e) {
try {
var msg = Message.fromJson(assertTyped(e));
if (filter(msg)) {
comp.complete(msg);
return false;
}
} on TypeAssertionError {
return true;
}
return true;
});
var future = comp.future.then((value) {
listener.removeListener(lsid);
return value;
});
sendSignal(message);
return (timeout != null) ? future.timeout(timeout) : future;
}