1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/TestInterfaceAsyncIterableDoubleUnion.h"
8 #include "mozilla/dom/TestInterfaceJSMaplikeSetlikeIterableBinding.h"
9 #include "nsPIDOMWindow.h"
10 #include "mozilla/dom/BindingUtils.h"
11 #include "mozilla/dom/UnionTypes.h"
13 namespace mozilla::dom
{
15 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(TestInterfaceAsyncIterableDoubleUnion
,
18 NS_IMPL_CYCLE_COLLECTING_ADDREF(TestInterfaceAsyncIterableDoubleUnion
)
19 NS_IMPL_CYCLE_COLLECTING_RELEASE(TestInterfaceAsyncIterableDoubleUnion
)
21 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TestInterfaceAsyncIterableDoubleUnion
)
22 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
23 NS_INTERFACE_MAP_ENTRY(nsISupports
)
26 TestInterfaceAsyncIterableDoubleUnion::TestInterfaceAsyncIterableDoubleUnion(
27 nsPIDOMWindowInner
* aParent
)
31 mValues
.AppendElement(std::pair
<nsString
, OwningStringOrLong
>(u
"long"_ns
, a
));
32 a
.SetAsString() = u
"a"_ns
;
33 mValues
.AppendElement(
34 std::pair
<nsString
, OwningStringOrLong
>(u
"string"_ns
, a
));
38 already_AddRefed
<TestInterfaceAsyncIterableDoubleUnion
>
39 TestInterfaceAsyncIterableDoubleUnion::Constructor(const GlobalObject
& aGlobal
,
41 nsCOMPtr
<nsPIDOMWindowInner
> window
=
42 do_QueryInterface(aGlobal
.GetAsSupports());
44 aRv
.Throw(NS_ERROR_FAILURE
);
48 RefPtr
<TestInterfaceAsyncIterableDoubleUnion
> r
=
49 new TestInterfaceAsyncIterableDoubleUnion(window
);
53 JSObject
* TestInterfaceAsyncIterableDoubleUnion::WrapObject(
54 JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
) {
55 return TestInterfaceAsyncIterableDoubleUnion_Binding::Wrap(aCx
, this,
59 nsPIDOMWindowInner
* TestInterfaceAsyncIterableDoubleUnion::GetParentObject()
64 already_AddRefed
<Promise
>
65 TestInterfaceAsyncIterableDoubleUnion::GetNextIterationResult(
66 Iterator
* aIterator
, ErrorResult
& aRv
) {
67 RefPtr
<Promise
> promise
= Promise::Create(mParent
->AsGlobal(), aRv
);
68 if (NS_WARN_IF(aRv
.Failed())) {
72 NS_DispatchToMainThread(NewRunnableMethod
<RefPtr
<Iterator
>, RefPtr
<Promise
>>(
73 "TestInterfaceAsyncIterableDoubleUnion::GetNextIterationResult", this,
74 &TestInterfaceAsyncIterableDoubleUnion::ResolvePromise
, aIterator
,
77 return promise
.forget();
80 void TestInterfaceAsyncIterableDoubleUnion::ResolvePromise(Iterator
* aIterator
,
82 IteratorData
& data
= aIterator
->Data();
85 // [long, 1], [string, "a"]
86 uint32_t idx
= data
.mIndex
;
87 if (idx
>= mValues
.Length()) {
88 iterator_utils::ResolvePromiseForFinished(aPromise
);
90 switch (aIterator
->GetIteratorType()) {
91 case IterableIteratorBase::IteratorType::Keys
:
92 aPromise
->MaybeResolve(mValues
[idx
].first
);
94 case IterableIteratorBase::IteratorType::Values
:
95 aPromise
->MaybeResolve(mValues
[idx
].second
);
97 case IterableIteratorBase::IteratorType::Entries
:
98 iterator_utils::ResolvePromiseWithKeyAndValue(
99 aPromise
, mValues
[idx
].first
, mValues
[idx
].second
);
107 } // namespace mozilla::dom