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 #ifndef mozilla_dom_PromiseNativeHandler_h
8 #define mozilla_dom_PromiseNativeHandler_h
11 #include "js/TypeDecls.h"
12 #include "nsISupports.h"
20 * PromiseNativeHandler allows C++ to react to a Promise being
21 * rejected/resolved. A PromiseNativeHandler can be appended to a Promise using
22 * Promise::AppendNativeHandler().
24 class PromiseNativeHandler
: public nsISupports
{
26 virtual ~PromiseNativeHandler() = default;
30 virtual void ResolvedCallback(JSContext
* aCx
,
31 JS::Handle
<JS::Value
> aValue
) = 0;
34 virtual void RejectedCallback(JSContext
* aCx
,
35 JS::Handle
<JS::Value
> aValue
) = 0;
38 // This class is used to set C++ callbacks once a dom Promise a resolved or
40 class DomPromiseListener final
: public PromiseNativeHandler
{
44 using CallbackTypeResolved
=
45 std::function
<void(JSContext
*, JS::Handle
<JS::Value
>)>;
46 using CallbackTypeRejected
= std::function
<void(nsresult
)>;
48 explicit DomPromiseListener(Promise
* aDOMPromise
);
49 DomPromiseListener(Promise
* aDOMPromise
, CallbackTypeResolved
&& aResolve
,
50 CallbackTypeRejected
&& aReject
);
51 void SetResolvers(CallbackTypeResolved
&& aResolve
,
52 CallbackTypeRejected
&& aReject
);
53 void ResolvedCallback(JSContext
* aCx
, JS::Handle
<JS::Value
> aValue
) override
;
54 void RejectedCallback(JSContext
* aCx
, JS::Handle
<JS::Value
> aValue
) override
;
57 ~DomPromiseListener();
58 Maybe
<CallbackTypeResolved
> mResolve
;
59 Maybe
<CallbackTypeRejected
> mReject
;
63 } // namespace mozilla
65 #endif // mozilla_dom_PromiseNativeHandler_h