Bug 1677000 - Add a type alias for MozPromise<T, IOError, true> r=Gijs

To use this type alias, IOUtils::InvokeToIOPromise had to become either a
member fn or a friend.

Differential Revision: https://phabricator.services.mozilla.com/D99000
This commit is contained in:
Barret Rennie 2020-12-09 04:31:01 +00:00
parent 3aa75d55bb
commit c50f24f067
2 changed files with 16 additions and 11 deletions

View File

@ -150,8 +150,6 @@ Atomic<bool> IOUtils::sShutdownStarted = Atomic<bool>(false);
/* static */
template <typename OkT, typename Fn>
void IOUtils::RunOnBackgroundThread(Promise* aPromise, Fn aFunc) {
using MozPromiseT = MozPromise<OkT, IOError, true>;
nsCOMPtr<nsISerialEventTarget> bg = GetBackgroundEventTarget();
if (!bg) {
aPromise->MaybeRejectWithAbortError(
@ -159,15 +157,15 @@ void IOUtils::RunOnBackgroundThread(Promise* aPromise, Fn aFunc) {
return;
}
InvokeAsync(bg, __func__,
[func = std::move(aFunc)]() {
Result<OkT, IOError> result = func();
if (result.isErr()) {
return MozPromiseT::CreateAndReject(result.unwrapErr(),
__func__);
}
return MozPromiseT::CreateAndResolve(result.unwrap(), __func__);
})
InvokeAsync(
bg, __func__,
[func = std::move(aFunc)]() {
Result<OkT, IOError> result = func();
if (result.isErr()) {
return IOPromise<OkT>::CreateAndReject(result.unwrapErr(), __func__);
}
return IOPromise<OkT>::CreateAndResolve(result.unwrap(), __func__);
})
->Then(
GetCurrentSerialEventTarget(), __func__,
[promise = RefPtr(aPromise)](const OkT& ok) {

View File

@ -109,6 +109,9 @@ class IOUtils final {
private:
~IOUtils() = default;
template <typename T>
using IOPromise = MozPromise<T, IOError, true>;
friend class IOUtilsShutdownBlocker;
struct InternalFileInfo;
struct InternalWriteAtomicOpts;
@ -119,6 +122,10 @@ class IOUtils final {
static StaticRefPtr<nsIAsyncShutdownClient> sBarrier;
static Atomic<bool> sShutdownStarted;
template <typename OkT, typename Fn, typename... Args>
static RefPtr<IOUtils::IOPromise<OkT>> InvokeToIOPromise(Fn aFunc,
Args... aArgs);
static already_AddRefed<nsIAsyncShutdownClient> GetShutdownBarrier();
static already_AddRefed<nsISerialEventTarget> GetBackgroundEventTarget();