Promise.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2021, Kyle Pereira <hey@xylepereira.me>
  3. * Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
  4. * Copyright (c) 2021-2023, Ali Mohammad Pur <mpfard@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/Concepts.h>
  10. #include <LibCore/EventLoop.h>
  11. #include <LibCore/EventReceiver.h>
  12. namespace Core {
  13. template<typename Result, typename TError>
  14. class Promise : public EventReceiver {
  15. C_OBJECT(Promise);
  16. public:
  17. using ErrorType = TError;
  18. Function<ErrorOr<void>(Result&)> on_resolution;
  19. Function<void(ErrorType&)> on_rejection;
  20. void resolve(Result&& result)
  21. {
  22. m_result_or_rejection = move(result);
  23. if (on_resolution) {
  24. auto handler_result = on_resolution(m_result_or_rejection->value());
  25. possibly_handle_rejection(handler_result);
  26. }
  27. }
  28. void reject(ErrorType&& error)
  29. {
  30. m_result_or_rejection = move(error);
  31. possibly_handle_rejection(*m_result_or_rejection);
  32. }
  33. bool is_rejected()
  34. {
  35. return m_result_or_rejection.has_value() && m_result_or_rejection->is_error();
  36. }
  37. bool is_resolved() const
  38. {
  39. return m_result_or_rejection.has_value() && !m_result_or_rejection->is_error();
  40. }
  41. ErrorOr<Result, ErrorType> await()
  42. {
  43. while (!m_result_or_rejection.has_value())
  44. Core::EventLoop::current().pump();
  45. return m_result_or_rejection.release_value();
  46. }
  47. // Converts a Promise<A> to a Promise<B> using a function func: A -> B
  48. template<typename T>
  49. RefPtr<Promise<T>> map(Function<T(Result&)> func)
  50. {
  51. RefPtr<Promise<T>> new_promise = Promise<T>::construct();
  52. on_resolution = [new_promise, func = move(func)](Result& result) -> ErrorOr<void> {
  53. new_promise->resolve(func(result));
  54. return {};
  55. };
  56. on_rejection = [new_promise](ErrorType& error) {
  57. new_promise->reject(move(error));
  58. };
  59. return new_promise;
  60. }
  61. template<CallableAs<void, Result&> F>
  62. Promise& when_resolved(F handler)
  63. {
  64. on_resolution = [handler = move(handler)](Result& result) { return handler(result); };
  65. if (is_resolved()) {
  66. auto handler_result = on_resolution(m_result_or_rejection->value());
  67. possibly_handle_rejection(handler_result);
  68. }
  69. return *this;
  70. }
  71. template<CallableAs<ErrorOr<void>, Result&> F>
  72. Promise& when_resolved(F handler)
  73. {
  74. on_resolution = move(handler);
  75. if (is_resolved()) {
  76. auto handler_result = on_resolution(m_result_or_rejection->value());
  77. possibly_handle_rejection(handler_result);
  78. }
  79. return *this;
  80. }
  81. template<CallableAs<void, ErrorType&> F>
  82. Promise& when_rejected(F handler)
  83. {
  84. on_rejection = move(handler);
  85. if (is_rejected())
  86. on_rejection(m_result_or_rejection->error());
  87. return *this;
  88. }
  89. private:
  90. template<typename T>
  91. void possibly_handle_rejection(ErrorOr<T>& result)
  92. {
  93. if (result.is_error() && on_rejection)
  94. on_rejection(result.error());
  95. }
  96. Promise() = default;
  97. Promise(EventReceiver* parent)
  98. : EventReceiver(parent)
  99. {
  100. }
  101. Optional<ErrorOr<Result, ErrorType>> m_result_or_rejection;
  102. };
  103. }