mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-24 05:11:16 +00:00
Bug 1809895 - Part 1: Move PromisifyAlgorithm to StreamUtils.h r=smaug
Differential Revision: https://phabricator.services.mozilla.com/D166805
This commit is contained in:
parent
20a1ba5105
commit
b6f871ac8b
@ -6,6 +6,9 @@
|
||||
#define mozilla_dom_StreamUtils_h
|
||||
|
||||
#include "mozilla/ErrorResult.h"
|
||||
#include "mozilla/dom/Promise.h"
|
||||
|
||||
class nsIGlobalObject;
|
||||
|
||||
namespace mozilla::dom {
|
||||
|
||||
@ -14,6 +17,37 @@ struct QueuingStrategy;
|
||||
double ExtractHighWaterMark(const QueuingStrategy& aStrategy,
|
||||
double aDefaultHWM, ErrorResult& aRv);
|
||||
|
||||
// Promisification algorithm, shared among:
|
||||
// Step 2 and 3 of https://streams.spec.whatwg.org/#readablestream-set-up
|
||||
// Step 2 and 3 of
|
||||
// https://streams.spec.whatwg.org/#readablestream-set-up-with-byte-reading-support
|
||||
// Step 2 and 3 of https://streams.spec.whatwg.org/#writablestream-set-up
|
||||
// Step 5 and 6 of https://streams.spec.whatwg.org/#transformstream-set-up
|
||||
template <typename T>
|
||||
MOZ_CAN_RUN_SCRIPT static already_AddRefed<Promise> PromisifyAlgorithm(
|
||||
nsIGlobalObject* aGlobal, T aFunc, mozilla::ErrorResult& aRv) {
|
||||
// Step 1. Let result be the result of running (algorithm). If this throws an
|
||||
// exception e, return a promise rejected with e.
|
||||
RefPtr<Promise> result;
|
||||
if constexpr (!std::is_same<decltype(aFunc(aRv)), void>::value) {
|
||||
result = aFunc(aRv);
|
||||
} else {
|
||||
aFunc(aRv);
|
||||
}
|
||||
|
||||
if (aRv.Failed()) {
|
||||
return Promise::CreateRejectedWithErrorResult(aGlobal, aRv);
|
||||
}
|
||||
|
||||
// Step 2. If result is a Promise, then return result.
|
||||
if (result) {
|
||||
return result.forget();
|
||||
}
|
||||
|
||||
// Step 3. Return a promise resolved with undefined.
|
||||
return Promise::CreateResolvedWithUndefined(aGlobal, aRv);
|
||||
}
|
||||
|
||||
} // namespace mozilla::dom
|
||||
|
||||
#endif // mozilla_dom_StreamUtils_h
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include "TransformerCallbackHelpers.h"
|
||||
|
||||
#include "StreamUtils.h"
|
||||
#include "mozilla/dom/Promise.h"
|
||||
#include "mozilla/dom/TransformStreamDefaultController.h"
|
||||
|
||||
@ -84,30 +85,11 @@ already_AddRefed<Promise> TransformerAlgorithms::FlushCallback(
|
||||
CallbackObject::eRethrowExceptions);
|
||||
}
|
||||
|
||||
// https://streams.spec.whatwg.org/#transformstream-set-up
|
||||
// Step 5 and 6.
|
||||
template <typename T>
|
||||
MOZ_CAN_RUN_SCRIPT static already_AddRefed<Promise> Promisify(
|
||||
nsIGlobalObject* aGlobal, T aFunc, mozilla::ErrorResult& aRv) {
|
||||
// Step 1. Let result be the result of running (algorithm). If this throws an
|
||||
// exception e, return a promise rejected with e.
|
||||
aFunc(aRv);
|
||||
if (aRv.Failed()) {
|
||||
return Promise::CreateRejectedWithErrorResult(aGlobal, aRv);
|
||||
}
|
||||
|
||||
// Step 2. If result is a Promise, then return result.
|
||||
// (This supports no return value since currently no subclass needs one)
|
||||
|
||||
// Step 3. Return a promise resolved with undefined.
|
||||
return Promise::CreateResolvedWithUndefined(aGlobal, aRv);
|
||||
}
|
||||
|
||||
already_AddRefed<Promise> TransformerAlgorithmsWrapper::TransformCallback(
|
||||
JSContext*, JS::Handle<JS::Value> aChunk,
|
||||
TransformStreamDefaultController& aController, ErrorResult& aRv) {
|
||||
nsCOMPtr<nsIGlobalObject> global = aController.GetParentObject();
|
||||
return Promisify(
|
||||
return PromisifyAlgorithm(
|
||||
global,
|
||||
[this, &aChunk, &aController](ErrorResult& aRv)
|
||||
MOZ_CAN_RUN_SCRIPT_FOR_DEFINITION {
|
||||
@ -120,7 +102,7 @@ already_AddRefed<Promise> TransformerAlgorithmsWrapper::FlushCallback(
|
||||
JSContext*, TransformStreamDefaultController& aController,
|
||||
ErrorResult& aRv) {
|
||||
nsCOMPtr<nsIGlobalObject> global = aController.GetParentObject();
|
||||
return Promisify(
|
||||
return PromisifyAlgorithm(
|
||||
global,
|
||||
[this, &aController](ErrorResult& aRv) MOZ_CAN_RUN_SCRIPT_FOR_DEFINITION {
|
||||
return FlushCallbackImpl(aController, aRv);
|
||||
|
Loading…
Reference in New Issue
Block a user