Bug 1700051: part 26) Correct typo in comment of `mozInlineSpellWordUtil::BuildSoftTe...
[gecko.git] / dom / promise / PromiseNativeHandler.h
blobf8d955133fa126bfa8a6a681cd7326a4a189d5ef
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
10 #include <functional>
11 #include "js/TypeDecls.h"
12 #include "js/Value.h"
13 #include "mozilla/Maybe.h"
14 #include "nsISupports.h"
16 namespace mozilla {
17 namespace dom {
19 class Promise;
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 {
27 protected:
28 virtual ~PromiseNativeHandler() = default;
30 public:
31 MOZ_CAN_RUN_SCRIPT
32 virtual void ResolvedCallback(JSContext* aCx,
33 JS::Handle<JS::Value> aValue) = 0;
35 MOZ_CAN_RUN_SCRIPT
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
41 // rejected.
42 class DomPromiseListener final : public PromiseNativeHandler {
43 NS_DECL_ISUPPORTS
45 public:
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;
58 private:
59 ~DomPromiseListener();
60 Maybe<CallbackTypeResolved> mResolve;
61 Maybe<CallbackTypeRejected> mReject;
64 } // namespace dom
65 } // namespace mozilla
67 #endif // mozilla_dom_PromiseNativeHandler_h