mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-20 16:28:54 +00:00
LibCore: Don't have Promise inherit from EventReceiver
This was only used for parenting promises to each other, but we can do that with a simple vector of children.
This commit is contained in:
parent
dfe776b722
commit
134fd8e413
Notes:
github-actions[bot]
2025-08-11 14:57:23 +00:00
Author: https://github.com/awesomekling
Commit: 134fd8e413
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5817
Reviewed-by: https://github.com/gmta ✅
3 changed files with 22 additions and 10 deletions
|
@ -14,13 +14,21 @@
|
|||
|
||||
namespace Core {
|
||||
|
||||
template<typename Result, typename TError>
|
||||
class Promise : public EventReceiver {
|
||||
C_OBJECT(Promise);
|
||||
class PromiseBase
|
||||
: public RefCounted<PromiseBase>
|
||||
, public Weakable<PromiseBase> {
|
||||
public:
|
||||
virtual ~PromiseBase() = default;
|
||||
};
|
||||
|
||||
template<typename Result, typename TError>
|
||||
class Promise : public PromiseBase {
|
||||
public:
|
||||
using ErrorType = TError;
|
||||
|
||||
virtual ~Promise() = default;
|
||||
static NonnullRefPtr<Promise> construct() { return adopt_ref(*new Promise()); }
|
||||
|
||||
Function<ErrorOr<void>(Result&)> on_resolution;
|
||||
Function<void(ErrorType&)> on_rejection;
|
||||
|
||||
|
@ -156,6 +164,8 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
void add_child(NonnullRefPtr<PromiseBase> child) { m_children.append(move(child)); }
|
||||
|
||||
private:
|
||||
template<typename T>
|
||||
void possibly_handle_rejection(ErrorOr<T>& result)
|
||||
|
@ -167,6 +177,8 @@ private:
|
|||
Promise() = default;
|
||||
|
||||
Optional<ErrorOr<Result, ErrorType>> m_result_or_rejection;
|
||||
|
||||
Vector<NonnullRefPtr<PromiseBase>> m_children;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -252,7 +252,7 @@ ErrorOr<NonnullRefPtr<Core::LocalServer>> Session::create_server(NonnullRefPtr<S
|
|||
|
||||
ErrorOr<void> Session::start(LaunchBrowserCallback const& launch_browser_callback)
|
||||
{
|
||||
auto promise = TRY(ServerPromise::try_create());
|
||||
auto promise = ServerPromise::construct();
|
||||
|
||||
m_web_content_socket_path = ByteString::formatted("{}/webdriver/session_{}_{}", TRY(Core::StandardPaths::runtime_directory()), getpid(), m_session_id);
|
||||
m_web_content_server = TRY(create_server(promise));
|
||||
|
|
|
@ -15,7 +15,7 @@ TEST_CASE(promise_await_async_event)
|
|||
{
|
||||
Core::EventLoop loop;
|
||||
|
||||
auto promise = MUST(Core::Promise<int>::try_create());
|
||||
auto promise = Core::Promise<int>::construct();
|
||||
|
||||
loop.deferred_invoke([=] {
|
||||
promise->resolve(42);
|
||||
|
@ -30,7 +30,7 @@ TEST_CASE(promise_await_async_event_rejection)
|
|||
{
|
||||
Core::EventLoop loop;
|
||||
|
||||
auto promise = MUST(Core::Promise<int>::try_create());
|
||||
auto promise = Core::Promise<int>::construct();
|
||||
|
||||
loop.deferred_invoke([=] {
|
||||
promise->reject(AK::Error::from_string_literal("lol no"));
|
||||
|
@ -48,7 +48,7 @@ TEST_CASE(promise_chain_handlers)
|
|||
bool resolved = false;
|
||||
bool rejected = false;
|
||||
|
||||
NonnullRefPtr<Core::Promise<int>> promise = MUST(Core::Promise<int>::try_create())
|
||||
NonnullRefPtr<Core::Promise<int>> promise = Core::Promise<int>::construct()
|
||||
->when_resolved([&](int&) -> ErrorOr<void> { resolved = true; return {}; })
|
||||
.when_rejected([&](AK::Error const&) { rejected = true; });
|
||||
|
||||
|
@ -68,7 +68,7 @@ TEST_CASE(infallible_promise_chain_handlers)
|
|||
bool resolved = false;
|
||||
bool rejected = false;
|
||||
|
||||
NonnullRefPtr<Core::Promise<int>> promise = MUST(Core::Promise<int>::try_create())
|
||||
NonnullRefPtr<Core::Promise<int>> promise = Core::Promise<int>::construct()
|
||||
->when_resolved([&](int&) { resolved = true; })
|
||||
.when_rejected([&](AK::Error const&) { rejected = true; });
|
||||
|
||||
|
@ -85,7 +85,7 @@ TEST_CASE(promise_map)
|
|||
{
|
||||
Core::EventLoop loop;
|
||||
|
||||
auto promise = MUST(Core::Promise<int>::try_create());
|
||||
auto promise = Core::Promise<int>::construct();
|
||||
auto mapped_promise = promise->map<int>([](int result) {
|
||||
return result * 2;
|
||||
});
|
||||
|
@ -103,7 +103,7 @@ TEST_CASE(promise_map_already_resolved)
|
|||
{
|
||||
Core::EventLoop loop;
|
||||
|
||||
auto promise = MUST(Core::Promise<int>::try_create());
|
||||
auto promise = Core::Promise<int>::construct();
|
||||
promise->resolve(21);
|
||||
|
||||
auto mapped_promise = promise->map<int>([](int result) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue