diff options
author | Timothy <timmot@users.noreply.github.com> | 2021-07-15 14:13:02 +1000 |
---|---|---|
committer | Gunnar Beutner <gunnar@beutner.name> | 2021-07-15 11:11:14 +0200 |
commit | 2eb93f2628775d8316dbd4d278e1471fb461386c (patch) | |
tree | b3fcecc7aa39713bcd8e9d826f369bf5d8368856 | |
parent | e4f05a904694dafdd7c62be0df328ee381a975d1 (diff) | |
download | serenity-2eb93f2628775d8316dbd4d278e1471fb461386c.zip |
LibCore+LibIMAP: Move Promise to LibCore
This makes Promise available without having to link LibIMAP.
-rw-r--r-- | Userland/Libraries/LibCore/Promise.h | 55 | ||||
-rw-r--r-- | Userland/Libraries/LibIMAP/Client.h | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibIMAP/Objects.h | 43 |
3 files changed, 59 insertions, 43 deletions
diff --git a/Userland/Libraries/LibCore/Promise.h b/Userland/Libraries/LibCore/Promise.h new file mode 100644 index 0000000000..d6622cfc48 --- /dev/null +++ b/Userland/Libraries/LibCore/Promise.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2021, Kyle Pereira <hey@xylepereira.me> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <LibCore/EventLoop.h> +#include <LibCore/Object.h> + +namespace Core { +template<typename Result> +class Promise : public Object { + C_OBJECT(Promise); + +private: + Optional<Result> m_pending; + +public: + Function<void(Result&)> on_resolved; + + void resolve(Result&& result) + { + m_pending = move(result); + if (on_resolved) + on_resolved(m_pending.value()); + } + + bool is_resolved() + { + return m_pending.has_value(); + }; + + Result await() + { + while (!is_resolved()) { + Core::EventLoop::current().pump(); + } + return m_pending.release_value(); + } + + // 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>> new_promise = Promise<T>::construct(); + on_resolved = [new_promise, func](Result& result) mutable { + auto t = func(result); + new_promise->resolve(move(t)); + }; + return new_promise; + } +}; +} diff --git a/Userland/Libraries/LibIMAP/Client.h b/Userland/Libraries/LibIMAP/Client.h index b61ad1d609..e39f88d158 100644 --- a/Userland/Libraries/LibIMAP/Client.h +++ b/Userland/Libraries/LibIMAP/Client.h @@ -7,10 +7,14 @@ #pragma once #include <AK/Function.h> +#include <LibCore/Promise.h> #include <LibIMAP/Parser.h> #include <LibTLS/TLSv12.h> namespace IMAP { +template<typename T> +using Promise = Core::Promise<T>; + class Client { friend class Parser; diff --git a/Userland/Libraries/LibIMAP/Objects.h b/Userland/Libraries/LibIMAP/Objects.h index 127c825674..f571e16e6d 100644 --- a/Userland/Libraries/LibIMAP/Objects.h +++ b/Userland/Libraries/LibIMAP/Objects.h @@ -11,7 +11,6 @@ #include <AK/Tuple.h> #include <AK/Variant.h> #include <LibCore/DateTime.h> -#include <LibCore/EventLoop.h> #include <LibCore/Object.h> #include <utility> @@ -762,48 +761,6 @@ struct ContinueRequest { String data; }; -template<typename Result> -class Promise : public Core::Object { - C_OBJECT(Promise); - -private: - Optional<Result> m_pending; - -public: - Function<void(Result&)> on_resolved; - - void resolve(Result&& result) - { - m_pending = move(result); - if (on_resolved) - on_resolved(m_pending.value()); - } - - bool is_resolved() - { - return m_pending.has_value(); - }; - - Result await() - { - while (!is_resolved()) { - Core::EventLoop::current().pump(); - } - return m_pending.release_value(); - } - - // 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>> new_promise = Promise<T>::construct(); - on_resolved = [new_promise, func](Result& result) mutable { - auto t = func(result); - new_promise->resolve(move(t)); - }; - return new_promise; - } -}; using Response = Variant<SolidResponse, ContinueRequest>; } |