Scheduler.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <memory>
  9. #include <mutex>
  10. #include <react/components/root/RootComponentDescriptor.h>
  11. #include <react/config/ReactNativeConfig.h>
  12. #include <react/core/ComponentDescriptor.h>
  13. #include <react/core/LayoutConstraints.h>
  14. #include <react/uimanager/ComponentDescriptorFactory.h>
  15. #include <react/uimanager/ComponentDescriptorRegistry.h>
  16. #include <react/uimanager/SchedulerDelegate.h>
  17. #include <react/uimanager/SchedulerToolbox.h>
  18. #include <react/uimanager/UIManagerBinding.h>
  19. #include <react/uimanager/UIManagerDelegate.h>
  20. #include <react/utils/ContextContainer.h>
  21. #include <react/utils/RuntimeExecutor.h>
  22. namespace facebook {
  23. namespace react {
  24. /*
  25. * Scheduler coordinates Shadow Tree updates and event flows.
  26. */
  27. class Scheduler final : public UIManagerDelegate {
  28. public:
  29. Scheduler(SchedulerToolbox schedulerToolbox, SchedulerDelegate *delegate);
  30. ~Scheduler();
  31. #pragma mark - Surface Management
  32. void startSurface(
  33. SurfaceId surfaceId,
  34. const std::string &moduleName,
  35. const folly::dynamic &initialProps,
  36. const LayoutConstraints &layoutConstraints = {},
  37. const LayoutContext &layoutContext = {}) const;
  38. void renderTemplateToSurface(
  39. SurfaceId surfaceId,
  40. const std::string &uiTemplate);
  41. void stopSurface(SurfaceId surfaceId) const;
  42. Size measureSurface(
  43. SurfaceId surfaceId,
  44. const LayoutConstraints &layoutConstraints,
  45. const LayoutContext &layoutContext) const;
  46. /*
  47. * Applies given `layoutConstraints` and `layoutContext` to a Surface.
  48. * The user interface will be relaid out as a result. The operation will be
  49. * performed synchronously (including mounting) if the method is called
  50. * on the main thread.
  51. * Can be called from any thread.
  52. */
  53. void constraintSurfaceLayout(
  54. SurfaceId surfaceId,
  55. const LayoutConstraints &layoutConstraints,
  56. const LayoutContext &layoutContext) const;
  57. /*
  58. * This is broken. Please do not use.
  59. * `ComponentDescriptor`s are not designed to be used outside of `UIManager`,
  60. * there is no any garantees about their lifetime.
  61. */
  62. ComponentDescriptor const *
  63. findComponentDescriptorByHandle_DO_NOT_USE_THIS_IS_BROKEN(
  64. ComponentHandle handle) const;
  65. MountingCoordinator::Shared findMountingCoordinator(
  66. SurfaceId surfaceId) const;
  67. #pragma mark - Delegate
  68. /*
  69. * Sets and gets the Scheduler's delegate.
  70. * If you requesting a ComponentDescriptor and unsure that it's there, you are
  71. * doing something wrong.
  72. */
  73. void setDelegate(SchedulerDelegate *delegate);
  74. SchedulerDelegate *getDelegate() const;
  75. #pragma mark - UIManagerDelegate
  76. void uiManagerDidFinishTransaction(
  77. MountingCoordinator::Shared const &mountingCoordinator) override;
  78. void uiManagerDidCreateShadowNode(
  79. const ShadowNode::Shared &shadowNode) override;
  80. void uiManagerDidDispatchCommand(
  81. const ShadowNode::Shared &shadowNode,
  82. std::string const &commandName,
  83. folly::dynamic const args) override;
  84. void uiManagerDidSetJSResponder(
  85. SurfaceId surfaceId,
  86. const ShadowNode::Shared &shadowView,
  87. bool blockNativeResponder) override;
  88. void uiManagerDidClearJSResponder() override;
  89. private:
  90. SchedulerDelegate *delegate_;
  91. SharedComponentDescriptorRegistry componentDescriptorRegistry_;
  92. std::unique_ptr<const RootComponentDescriptor> rootComponentDescriptor_;
  93. RuntimeExecutor runtimeExecutor_;
  94. std::shared_ptr<UIManager> uiManager_;
  95. std::shared_ptr<const ReactNativeConfig> reactNativeConfig_;
  96. EventDispatcher::Shared eventDispatcher_;
  97. };
  98. } // namespace react
  99. } // namespace facebook