PromiseRejectionEvent.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibJS/Runtime/Promise.h>
  8. #include <LibJS/Runtime/Value.h>
  9. #include <LibWeb/DOM/Event.h>
  10. namespace Web::HTML {
  11. struct PromiseRejectionEventInit : public DOM::EventInit {
  12. JS::Handle<JS::Promise> promise;
  13. JS::Value reason;
  14. };
  15. class PromiseRejectionEvent : public DOM::Event {
  16. public:
  17. using WrapperType = Bindings::PromiseRejectionEventWrapper;
  18. static NonnullRefPtr<PromiseRejectionEvent> create(FlyString const& event_name, PromiseRejectionEventInit const& event_init = {})
  19. {
  20. return adopt_ref(*new PromiseRejectionEvent(event_name, event_init));
  21. }
  22. static NonnullRefPtr<PromiseRejectionEvent> create_with_global_object(Bindings::WindowObject&, FlyString const& event_name, PromiseRejectionEventInit const& event_init)
  23. {
  24. return PromiseRejectionEvent::create(event_name, event_init);
  25. }
  26. virtual ~PromiseRejectionEvent() override = default;
  27. // Needs to return a pointer for the generated JS bindings to work.
  28. JS::Promise const* promise() const { return m_promise.cell(); }
  29. JS::Value reason() const { return m_reason; }
  30. protected:
  31. PromiseRejectionEvent(FlyString const& event_name, PromiseRejectionEventInit const& event_init)
  32. : DOM::Event(event_name, event_init)
  33. , m_promise(event_init.promise)
  34. , m_reason(event_init.reason)
  35. {
  36. }
  37. JS::Handle<JS::Promise> m_promise;
  38. // FIXME: Protect this from GC! Currently we have no handle for arbitrary JS::Value.
  39. JS::Value m_reason;
  40. };
  41. }