UIManagerBinding.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #pragma once
  8. #include <folly/dynamic.h>
  9. #include <jsi/jsi.h>
  10. #include <react/uimanager/UIManager.h>
  11. #include <react/uimanager/primitives.h>
  12. namespace facebook {
  13. namespace react {
  14. /*
  15. * Exposes UIManager to JavaScript realm.
  16. */
  17. class UIManagerBinding : public jsi::HostObject {
  18. public:
  19. /*
  20. * Installs UIManagerBinding into JavaScript runtime if needed.
  21. * Creates and sets `UIManagerBinding` into the global namespace.
  22. * In case if the global namespace already has a `UIManagerBinding` installed,
  23. * returns that.
  24. * Thread synchronization must be enforced externally.
  25. */
  26. static std::shared_ptr<UIManagerBinding> createAndInstallIfNeeded(
  27. jsi::Runtime &runtime);
  28. ~UIManagerBinding();
  29. /*
  30. * Establish a relationship between `UIManager` and `UIManagerBinding` by
  31. * setting internal pointers to each other.
  32. * Must be called on JavaScript thread or during VM destruction.
  33. */
  34. void attach(std::shared_ptr<UIManager> const &uiManager);
  35. /*
  36. * Starts React Native Surface with given id, moduleName, and props.
  37. * Thread synchronization must be enforced externally.
  38. */
  39. void startSurface(
  40. jsi::Runtime &runtime,
  41. SurfaceId surfaceId,
  42. const std::string &moduleName,
  43. const folly::dynamic &initalProps) const;
  44. /*
  45. * Stops React Native Surface with given id.
  46. * Thread synchronization must be enforced externally.
  47. */
  48. void stopSurface(jsi::Runtime &runtime, SurfaceId surfaceId) const;
  49. /*
  50. * Delivers raw event data to JavaScript.
  51. * Thread synchronization must be enforced externally.
  52. */
  53. void dispatchEvent(
  54. jsi::Runtime &runtime,
  55. const EventTarget *eventTarget,
  56. const std::string &type,
  57. const ValueFactory &payloadFactory) const;
  58. /*
  59. * Invalidates the binding and underlying UIManager.
  60. * Allows to save some resources and prevents UIManager's delegate to be
  61. * called.
  62. * Calling public methods of this class after calling this method is UB.
  63. * Can be called on any thread.
  64. */
  65. void invalidate() const;
  66. /*
  67. * `jsi::HostObject` specific overloads.
  68. */
  69. jsi::Value get(jsi::Runtime &runtime, const jsi::PropNameID &name) override;
  70. private:
  71. std::shared_ptr<UIManager> uiManager_;
  72. std::unique_ptr<const EventHandler> eventHandler_;
  73. };
  74. } // namespace react
  75. } // namespace facebook