Skip to content

Commit

Permalink
Fix Future::on_completion (#5181)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbreams authored Jan 21, 2022
1 parent d8cfb35 commit b595667
Show file tree
Hide file tree
Showing 3 changed files with 677 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

### Internals
* SubscriptionStore's should be initialized with an implict empty SubscriptionSet so users can wait for query version zero to finish synchronizing. ((#5166)[https://github.com/realm/realm-core/pull/5166])
* Fixed `Future::on_completion()` and added missing testing for it ((#5181)[https://github.com/realm/realm-core/pull/5181])

----------------------------------------------

Expand Down
38 changes: 27 additions & 11 deletions src/realm/util/future.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*
**************************************************************************/

#pragma once

#include <condition_variable>
#include <mutex>
#include <type_traits>
Expand Down Expand Up @@ -793,32 +795,32 @@ class REALM_NODISCARD future_details::Future {
using Wrapper = StatusOrStatusWith<T>;
using Result = NormalizedCallResult<Func, StatusOrStatusWith<T>>;
if constexpr (!is_future<Result>) {
return generalImpl(
return general_impl(
// on ready success:
[&](T&& val) {
return Future<Result>::make_ready(
no_throw_call(std::forward<Func>(func), Wrapper(std::move(val))));
},
// on ready failure:
[&](Status&& status) {
return Future<Result>::make_Ready(
return Future<Result>::make_ready(
no_throw_call(std::forward<Func>(func), Wrapper(std::move(status))));
},
// on not ready yet:
[&] {
return make_continuation<Result>(
[func = std::forward<Func>(func)](SharedState<T>* input,
SharedState<Result>* output) mutable noexcept {
if (!input->status.is_ok())
return output->set_from(no_throw_call(func, Wrapper(std::move(input->status))));
if (!input->m_status.is_ok())
return output->set_from(no_throw_call(func, Wrapper(std::move(input->m_status))));

output->set_from(no_throw_call(func, Wrapper(std::move(*input->data))));
output->set_from(no_throw_call(func, Wrapper(std::move(*input->m_data))));
});
});
}
else {
using UnwrappedResult = typename Result::value_type;
return generalImpl(
return general_impl(
// on ready success:
[&](T&& val) {
try {
Expand All @@ -844,23 +846,23 @@ class REALM_NODISCARD future_details::Future {
return make_continuation<UnwrappedResult>(
[func = std::forward<Func>(func)](SharedState<T>* input,
SharedState<UnwrappedResult>* output) mutable noexcept {
if (!input->status.isOK()) {
if (!input->m_status.is_ok()) {
try {
throwing_call(func, Wrapper(std::move(input->m_status)))
.propagate_result_to(output);
}
catch (...) {
output->set_error(exception_to_status());
output->set_status(exception_to_status());
}

return;
}

try {
throwing_call(func, Wrapper(std::move(*input->data))).propagate_result_to(output);
throwing_call(func, Wrapper(std::move(*input->m_data))).propagate_result_to(output);
}
catch (...) {
output->set_error(exception_to_status());
output->set_status(exception_to_status());
}
});
});
Expand Down Expand Up @@ -937,6 +939,8 @@ class REALM_NODISCARD future_details::Future {
}
}

Future<void> ignore_value() && noexcept;

private:
template <typename T2>
friend class Future;
Expand Down Expand Up @@ -1115,7 +1119,13 @@ class REALM_NODISCARD future_details::Future<void> {
return std::move(inner).on_error(std::forward<Func>(func));
}

Future<void> ignoreValue() && noexcept
template <typename Func>
auto on_completion(Func&& func) && noexcept
{
return std::move(inner).on_completion(std::forward<Func>(func));
}

Future<void> ignore_value() && noexcept
{
return std::move(*this);
}
Expand Down Expand Up @@ -1207,4 +1217,10 @@ inline void Promise<T>::set_from(Future<T>&& future) noexcept
});
}

template <typename T>
inline Future<void> Future<T>::ignore_value() && noexcept
{
return std::move(*this).then([](auto&&) {});
}

} // namespace realm::util
Loading

0 comments on commit b595667

Please sign in to comment.