mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-23 12:51:06 +00:00
Bug 1866402 - QM: Generalize the helper for mapping MozPromise values to support both exclusive and non-exclusive promises; r=dom-storage-reviewers,jari
Differential Revision: https://phabricator.services.mozilla.com/D223276
This commit is contained in:
parent
dca4a3ceec
commit
61bffae5df
@ -8,11 +8,31 @@
|
||||
#define DOM_QUOTA_MOZPROMISEUTILS_H_
|
||||
|
||||
#include "mozilla/MozPromise.h"
|
||||
#include "nsThreadUtils.h"
|
||||
|
||||
namespace mozilla::dom::quota {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename T>
|
||||
struct IsExclusiveMozPromise {
|
||||
static constexpr bool value = false;
|
||||
};
|
||||
|
||||
// Specialization for MozPromise
|
||||
template <typename ResolveValueT, typename RejectValueT, bool IsExclusive>
|
||||
struct IsExclusiveMozPromise<
|
||||
MozPromise<ResolveValueT, RejectValueT, IsExclusive>> {
|
||||
static constexpr bool value = IsExclusive;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <typename T, typename U, typename F>
|
||||
RefPtr<T> Map(RefPtr<U> aPromise, F&& aFunc) {
|
||||
auto Map(RefPtr<U> aPromise, F&& aFunc)
|
||||
-> std::enable_if_t<
|
||||
detail::IsExclusiveMozPromise<RemoveSmartPointer<U>>::value,
|
||||
RefPtr<T>> {
|
||||
return aPromise->Then(
|
||||
GetCurrentSerialEventTarget(), __func__,
|
||||
[func =
|
||||
@ -27,6 +47,25 @@ RefPtr<T> Map(RefPtr<U> aPromise, F&& aFunc) {
|
||||
});
|
||||
}
|
||||
|
||||
template <typename T, typename U, typename F>
|
||||
auto Map(RefPtr<U> aPromise, F&& aFunc)
|
||||
-> std::enable_if_t<
|
||||
!detail::IsExclusiveMozPromise<RemoveSmartPointer<U>>::value,
|
||||
RefPtr<T>> {
|
||||
return aPromise->Then(GetCurrentSerialEventTarget(), __func__,
|
||||
[func = std::forward<F>(aFunc)](
|
||||
const typename U::ResolveOrRejectValue& aValue) {
|
||||
if (aValue.IsReject()) {
|
||||
return T::CreateAndReject(aValue.RejectValue(),
|
||||
__func__);
|
||||
}
|
||||
|
||||
auto value = func(aValue);
|
||||
|
||||
return T::CreateAndResolve(value, __func__);
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace mozilla::dom::quota
|
||||
|
||||
#endif // DOM_QUOTA_MOZPROMISEUTILS_H_
|
||||
|
@ -9,6 +9,16 @@
|
||||
|
||||
namespace mozilla::dom::quota::test {
|
||||
|
||||
TEST(DOM_Quota_MozPromiseUtils, BoolPromiseToBoolPromise)
|
||||
{
|
||||
auto value = QuotaManagerDependencyFixture::Await(Map<BoolPromise>(
|
||||
BoolPromise::CreateAndResolve(true, __func__),
|
||||
[](const BoolPromise::ResolveOrRejectValue& aValue) { return false; }));
|
||||
|
||||
ASSERT_TRUE(value.IsResolve());
|
||||
ASSERT_FALSE(value.ResolveValue());
|
||||
}
|
||||
|
||||
TEST(DOM_Quota_MozPromiseUtils, ExclusiveBoolPromiseToBoolPromise)
|
||||
{
|
||||
auto value = QuotaManagerDependencyFixture::Await(
|
||||
|
Loading…
Reference in New Issue
Block a user