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"
13 #include "mozilla/Maybe.h"
14 #include "nsISupports.h"
22 * PromiseNativeHandler allows C++ to react to a Promise being
23 * rejected/resolved. A PromiseNativeHandler can be appended to a Promise using
24 * Promise::AppendNativeHandler().
26 class PromiseNativeHandler
: public nsISupports
{
28 virtual ~PromiseNativeHandler() = default;
32 virtual void ResolvedCallback(JSContext
* aCx
,
33 JS::Handle
<JS::Value
> aValue
) = 0;
36 virtual void RejectedCallback(JSContext
* aCx
,
37 JS::Handle
<JS::Value
> aValue
) = 0;
40 // This class is used to set C++ callbacks once a dom Promise a resolved or
42 class DomPromiseListener final
: public PromiseNativeHandler
{
46 using CallbackTypeResolved
=
47 std::function
<void(JSContext
*, JS::Handle
<JS::Value
>)>;
48 using CallbackTypeRejected
= std::function
<void(nsresult
)>;
50 explicit DomPromiseListener(Promise
* aDOMPromise
);
51 DomPromiseListener(Promise
* aDOMPromise
, CallbackTypeResolved
&& aResolve
,
52 CallbackTypeRejected
&& aReject
);
53 void SetResolvers(CallbackTypeResolved
&& aResolve
,
54 CallbackTypeRejected
&& aReject
);
55 void ResolvedCallback(JSContext
* aCx
, JS::Handle
<JS::Value
> aValue
) override
;
56 void RejectedCallback(JSContext
* aCx
, JS::Handle
<JS::Value
> aValue
) override
;
59 ~DomPromiseListener();
60 Maybe
<CallbackTypeResolved
> mResolve
;
61 Maybe
<CallbackTypeRejected
> mReject
;
65 } // namespace mozilla
67 #endif // mozilla_dom_PromiseNativeHandler_h