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/TestInterfaceAsyncIterableDouble.h"
8 #include "mozilla/dom/TestInterfaceJSMaplikeSetlikeIterableBinding.h"
9 #include "nsPIDOMWindow.h"
10 #include "mozilla/dom/BindingUtils.h"
12 namespace mozilla::dom
{
14 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(TestInterfaceAsyncIterableDouble
, mParent
)
16 NS_IMPL_CYCLE_COLLECTING_ADDREF(TestInterfaceAsyncIterableDouble
)
17 NS_IMPL_CYCLE_COLLECTING_RELEASE(TestInterfaceAsyncIterableDouble
)
19 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TestInterfaceAsyncIterableDouble
)
20 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
21 NS_INTERFACE_MAP_ENTRY(nsISupports
)
24 TestInterfaceAsyncIterableDouble::TestInterfaceAsyncIterableDouble(
25 nsPIDOMWindowInner
* aParent
)
27 mValues
.AppendElement(std::pair
<nsString
, nsString
>(u
"a"_ns
, u
"b"_ns
));
28 mValues
.AppendElement(std::pair
<nsString
, nsString
>(u
"c"_ns
, u
"d"_ns
));
29 mValues
.AppendElement(std::pair
<nsString
, nsString
>(u
"e"_ns
, u
"f"_ns
));
33 already_AddRefed
<TestInterfaceAsyncIterableDouble
>
34 TestInterfaceAsyncIterableDouble::Constructor(const GlobalObject
& aGlobal
,
36 nsCOMPtr
<nsPIDOMWindowInner
> window
=
37 do_QueryInterface(aGlobal
.GetAsSupports());
39 aRv
.Throw(NS_ERROR_FAILURE
);
43 RefPtr
<TestInterfaceAsyncIterableDouble
> r
=
44 new TestInterfaceAsyncIterableDouble(window
);
48 JSObject
* TestInterfaceAsyncIterableDouble::WrapObject(
49 JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
) {
50 return TestInterfaceAsyncIterableDouble_Binding::Wrap(aCx
, this, aGivenProto
);
53 nsPIDOMWindowInner
* TestInterfaceAsyncIterableDouble::GetParentObject() const {
57 already_AddRefed
<Promise
>
58 TestInterfaceAsyncIterableDouble::GetNextIterationResult(Iterator
* aIterator
,
60 RefPtr
<Promise
> promise
= Promise::Create(mParent
->AsGlobal(), aRv
);
61 if (NS_WARN_IF(aRv
.Failed())) {
65 NS_DispatchToMainThread(NewRunnableMethod
<RefPtr
<Iterator
>, RefPtr
<Promise
>>(
66 "TestInterfaceAsyncIterableDouble::GetNextIterationResult", this,
67 &TestInterfaceAsyncIterableDouble::ResolvePromise
, aIterator
, promise
));
69 return promise
.forget();
72 void TestInterfaceAsyncIterableDouble::ResolvePromise(Iterator
* aIterator
,
74 IteratorData
& data
= aIterator
->Data();
76 // Test data: ['a', 'b'], ['c', 'd'], ['e', 'f']
77 uint32_t idx
= data
.mIndex
;
78 if (idx
>= mValues
.Length()) {
79 iterator_utils::ResolvePromiseForFinished(aPromise
);
81 switch (aIterator
->GetIteratorType()) {
82 case IterableIteratorBase::IteratorType::Keys
:
83 aPromise
->MaybeResolve(mValues
[idx
].first
);
85 case IterableIteratorBase::IteratorType::Values
:
86 aPromise
->MaybeResolve(mValues
[idx
].second
);
88 case IterableIteratorBase::IteratorType::Entries
:
89 iterator_utils::ResolvePromiseWithKeyAndValue(
90 aPromise
, mValues
[idx
].first
, mValues
[idx
].second
);
98 } // namespace mozilla::dom