1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 14:27:35 +00:00

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
parent 5a0ad6812c
commit 0c5c75e8a4
9 changed files with 150 additions and 38 deletions

View file

@ -61,7 +61,7 @@ private:
, m_on_complete(move(on_complete))
{
if (m_on_complete) {
m_promise->on_resolved = [](NonnullRefPtr<Core::Object>& object) -> ErrorOr<void> {
m_promise->on_resolution = [](NonnullRefPtr<Core::Object>& object) -> ErrorOr<void> {
auto self = static_ptr_cast<BackgroundAction<Result>>(object);
VERIFY(self->m_result.has_value());
if (auto maybe_error = self->m_on_complete(self->m_result.value()); maybe_error.is_error())
@ -78,7 +78,7 @@ private:
enqueue_work([this, origin_event_loop = &Core::EventLoop::current()] {
auto result = m_action(*this);
// The event loop cancels the promise when it exits.
m_canceled |= m_promise->is_canceled();
m_canceled |= m_promise->is_rejected();
auto callback_scheduled = false;
// All of our work was successful and we weren't cancelled; resolve the event loop's promise.
if (!m_canceled && !result.is_error()) {
@ -99,7 +99,7 @@ private:
if (result.is_error())
error = result.release_error();
m_promise->cancel(Error::from_errno(ECANCELED));
m_promise->reject(Error::from_errno(ECANCELED));
if (!m_canceled && m_on_error) {
callback_scheduled = true;
origin_event_loop->deferred_invoke([this, error = move(error)]() mutable {