1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef mozilla_dom_StreamUtils_h
6 #define mozilla_dom_StreamUtils_h
8 #include "mozilla/ErrorResult.h"
9 #include "mozilla/dom/Promise.h"
11 class nsIGlobalObject
;
13 namespace mozilla::dom
{
15 struct QueuingStrategy
;
17 double ExtractHighWaterMark(const QueuingStrategy
& aStrategy
,
18 double aDefaultHWM
, ErrorResult
& aRv
);
20 // Promisification algorithm, shared among:
21 // Step 2 and 3 of https://streams.spec.whatwg.org/#readablestream-set-up
23 // https://streams.spec.whatwg.org/#readablestream-set-up-with-byte-reading-support
24 // Step 2 and 3 of https://streams.spec.whatwg.org/#writablestream-set-up
25 // Step 5 and 6 of https://streams.spec.whatwg.org/#transformstream-set-up
27 MOZ_CAN_RUN_SCRIPT
static already_AddRefed
<Promise
> PromisifyAlgorithm(
28 nsIGlobalObject
* aGlobal
, T aFunc
, mozilla::ErrorResult
& aRv
) {
29 // Step 1. Let result be the result of running (algorithm). If this throws an
30 // exception e, return a promise rejected with e.
31 RefPtr
<Promise
> result
;
32 if constexpr (!std::is_same
<decltype(aFunc(aRv
)), void>::value
) {
38 if (aRv
.IsUncatchableException()) {
43 return Promise::CreateRejectedWithErrorResult(aGlobal
, aRv
);
46 // Step 2. If result is a Promise, then return result.
48 return result
.forget();
51 // Step 3. Return a promise resolved with undefined.
52 return Promise::CreateResolvedWithUndefined(aGlobal
, aRv
);
55 } // namespace mozilla::dom
57 #endif // mozilla_dom_StreamUtils_h