Implement setRequestHandler with async/await

This commit is contained in:
Simon Binder 2022-12-20 23:42:42 +01:00
parent de67278f73
commit 8a4dea57e7
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
1 changed files with 10 additions and 5 deletions

View File

@ -147,11 +147,16 @@ class DriftCommunication {
/// [handler] throws, the error will be re-directed to the client. If
/// [handler] returns a [Future], it will be awaited.
void setRequestHandler(dynamic Function(Request) handler) {
incomingRequests.listen((request) {
Future.sync(() => handler(request)).then(
(result) => respond(request, result),
onError: (Object e, StackTrace s) => respondError(request, e, s),
);
incomingRequests.listen((request) async {
Object? response;
try {
response = await handler(request);
} catch (e, s) {
return respondError(request, e, s);
}
respond(request, response);
});
}
}