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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "PromiseNativeHandler.h"
8 #include "mozilla/dom/Promise.h"
9 #include "nsISupportsImpl.h"
14 NS_IMPL_ISUPPORTS0(DomPromiseListener
)
16 DomPromiseListener::DomPromiseListener(dom::Promise
* aDOMPromise
) {
17 aDOMPromise
->AppendNativeHandler(this);
20 DomPromiseListener::DomPromiseListener(dom::Promise
* aDOMPromise
,
21 CallbackTypeResolved
&& aResolve
,
22 CallbackTypeRejected
&& aReject
)
23 : mResolve(Some(std::move(aResolve
))), mReject(Some(std::move(aReject
))) {
24 aDOMPromise
->AppendNativeHandler(this);
27 DomPromiseListener::~DomPromiseListener() {
29 (*mReject
)(NS_BINDING_ABORTED
);
33 void DomPromiseListener::ResolvedCallback(JSContext
* aCx
,
34 JS::Handle
<JS::Value
> aValue
) {
36 (*mResolve
)(aCx
, aValue
);
38 // Let's clear the lambdas in case we have a cycle to ourselves.
43 void DomPromiseListener::RejectedCallback(JSContext
* aCx
,
44 JS::Handle
<JS::Value
> aValue
) {
47 if (!aValue
.isInt32()) {
48 errorCode
= NS_ERROR_DOM_NOT_NUMBER_ERR
;
50 errorCode
= nsresult(aValue
.toInt32());
52 (*mReject
)(errorCode
);
54 // Let's clear the lambdas in case we have a cycle to ourselves.
59 void DomPromiseListener::SetResolvers(CallbackTypeResolved
&& aResolve
,
60 CallbackTypeRejected
&& aReject
) {
61 mResolve
= Some(std::move(aResolve
));
62 mReject
= Some(std::move(aReject
));
66 } // namespace mozilla