mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 06:47: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:
parent
5a0ad6812c
commit
0c5c75e8a4
9 changed files with 150 additions and 38 deletions
|
@ -1,76 +1,127 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Kyle Pereira <hey@xylepereira.me>
|
||||
* Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
|
||||
* Copyright (c) 2021-2023, Ali Mohammad Pur <mpfard@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Concepts.h>
|
||||
#include <LibCore/EventLoop.h>
|
||||
#include <LibCore/Object.h>
|
||||
|
||||
namespace Core {
|
||||
|
||||
template<typename Result>
|
||||
template<typename Result, typename TError>
|
||||
class Promise : public Object {
|
||||
C_OBJECT(Promise);
|
||||
|
||||
public:
|
||||
Function<ErrorOr<void>(Result&)> on_resolved;
|
||||
using ErrorType = TError;
|
||||
|
||||
ErrorOr<void> resolve(Result&& result)
|
||||
Function<ErrorOr<void>(Result&)> on_resolution;
|
||||
Function<void(ErrorType&)> on_rejection;
|
||||
|
||||
void resolve(Result&& result)
|
||||
{
|
||||
m_pending_or_error = move(result);
|
||||
m_result_or_rejection = move(result);
|
||||
|
||||
if (on_resolved)
|
||||
return on_resolved(m_pending_or_error->value());
|
||||
return {};
|
||||
if (on_resolution) {
|
||||
auto handler_result = on_resolution(m_result_or_rejection->value());
|
||||
possibly_handle_rejection(handler_result);
|
||||
}
|
||||
}
|
||||
|
||||
void cancel(Error error)
|
||||
void reject(ErrorType&& error)
|
||||
{
|
||||
m_pending_or_error = move(error);
|
||||
m_result_or_rejection = move(error);
|
||||
possibly_handle_rejection(*m_result_or_rejection);
|
||||
}
|
||||
|
||||
bool is_canceled()
|
||||
bool is_rejected()
|
||||
{
|
||||
return m_pending_or_error.has_value() && m_pending_or_error->is_error();
|
||||
return m_result_or_rejection.has_value() && m_result_or_rejection->is_error();
|
||||
}
|
||||
|
||||
bool is_resolved() const
|
||||
{
|
||||
return m_pending_or_error.has_value() && !m_pending_or_error->is_error();
|
||||
return m_result_or_rejection.has_value() && !m_result_or_rejection->is_error();
|
||||
}
|
||||
|
||||
ErrorOr<Result> await()
|
||||
ErrorOr<Result, ErrorType>& await()
|
||||
{
|
||||
while (!m_pending_or_error.has_value())
|
||||
while (!m_result_or_rejection.has_value())
|
||||
Core::EventLoop::current().pump();
|
||||
|
||||
return m_pending_or_error.release_value();
|
||||
return *m_result_or_rejection;
|
||||
}
|
||||
|
||||
// Converts a Promise<A> to a Promise<B> using a function func: A -> B
|
||||
template<typename T>
|
||||
RefPtr<Promise<T>> map(T func(Result&))
|
||||
RefPtr<Promise<T>> map(Function<T(Result&)> func)
|
||||
{
|
||||
RefPtr<Promise<T>> new_promise = Promise<T>::construct();
|
||||
on_resolved = [new_promise, func](Result& result) {
|
||||
auto t = func(result);
|
||||
return new_promise->resolve(move(t));
|
||||
on_resolution = [new_promise, func = move(func)](Result& result) -> ErrorOr<void> {
|
||||
new_promise->resolve(func(result));
|
||||
return {};
|
||||
};
|
||||
on_rejection = [new_promise](ErrorType& error) {
|
||||
new_promise->reject(move(error));
|
||||
};
|
||||
return new_promise;
|
||||
}
|
||||
|
||||
template<CallableAs<void, Result&> F>
|
||||
Promise& when_resolved(F handler)
|
||||
{
|
||||
on_resolution = [handler = move(handler)](Result& result) { return handler(result); };
|
||||
if (is_resolved()) {
|
||||
auto handler_result = on_resolution(m_result_or_rejection->value());
|
||||
possibly_handle_rejection(handler_result);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<CallableAs<ErrorOr<void>, Result&> F>
|
||||
Promise& when_resolved(F handler)
|
||||
{
|
||||
on_resolution = move(handler);
|
||||
if (is_resolved()) {
|
||||
auto handler_result = on_resolution(m_result_or_rejection->value());
|
||||
possibly_handle_rejection(handler_result);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<CallableAs<void, ErrorType&> F>
|
||||
Promise& when_rejected(F handler)
|
||||
{
|
||||
on_rejection = move(handler);
|
||||
if (is_rejected())
|
||||
on_rejection(m_result_or_rejection->error());
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
template<typename T>
|
||||
void possibly_handle_rejection(ErrorOr<T>& result)
|
||||
{
|
||||
if (result.is_error() && on_rejection)
|
||||
on_rejection(result.error());
|
||||
}
|
||||
|
||||
Promise() = default;
|
||||
Promise(Object* parent)
|
||||
: Object(parent)
|
||||
{
|
||||
}
|
||||
|
||||
Optional<ErrorOr<Result>> m_pending_or_error;
|
||||
Optional<ErrorOr<Result, ErrorType>> m_result_or_rejection;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue