LibCore: Slightly rework the Core::Promise API

The previous iteration of this API was somewhat odd and rough in random
places, which degraded usability and made less than perfect sense.
This commit reworks the API to be a little closer to more
conventional promise APIs (a la javascript promises).

Also adds a test to ensure the class even works.
This commit is contained in:
Ali Mohammad Pur 2023-07-08 01:30:27 +03:30 committed by Linus Groh
commit 0c5c75e8a4
Notes: sideshowbarker 2024-07-17 10:05:47 +09:00
9 changed files with 150 additions and 38 deletions

View file

@ -64,7 +64,7 @@ ErrorOr<void> Client::on_ready_to_receive()
// Once we get server hello we can start sending.
if (m_connect_pending) {
TRY(m_connect_pending->resolve({}));
m_connect_pending->resolve({});
m_connect_pending.clear();
m_buffer.clear();
return {};
@ -227,13 +227,13 @@ ErrorOr<void> Client::handle_parsed_response(ParseStatus&& parse_status)
bool should_send_next = false;
if (!parse_status.successful) {
m_expecting_response = false;
TRY(m_pending_promises.first()->resolve({}));
m_pending_promises.first()->resolve({});
m_pending_promises.remove(0);
}
if (parse_status.response.has_value()) {
m_expecting_response = false;
should_send_next = parse_status.response->has<SolidResponse>();
TRY(m_pending_promises.first()->resolve(move(parse_status.response)));
m_pending_promises.first()->resolve(move(parse_status.response));
m_pending_promises.remove(0);
}
@ -386,7 +386,7 @@ RefPtr<Promise<Optional<SolidResponse>>> Client::append(StringView mailbox, Mess
auto response_promise = Promise<Optional<Response>>::construct();
m_pending_promises.append(response_promise);
continue_req->on_resolved = [this, message2 { move(message) }](auto& data) -> ErrorOr<void> {
continue_req->on_resolution = [this, message2 { move(message) }](auto& data) -> ErrorOr<void> {
if (!data.has_value()) {
TRY(handle_parsed_response({ .successful = false, .response = {} }));
} else {