RCTScheduler.mm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #import "RCTScheduler.h"
  8. #import <react/debug/SystraceSection.h>
  9. #import <react/uimanager/ComponentDescriptorFactory.h>
  10. #import <react/uimanager/Scheduler.h>
  11. #import <react/uimanager/SchedulerDelegate.h>
  12. #import <React/RCTFollyConvert.h>
  13. #import "RCTConversions.h"
  14. using namespace facebook::react;
  15. class SchedulerDelegateProxy : public SchedulerDelegate {
  16. public:
  17. SchedulerDelegateProxy(void *scheduler) : scheduler_(scheduler) {}
  18. void schedulerDidFinishTransaction(MountingCoordinator::Shared const &mountingCoordinator) override
  19. {
  20. RCTScheduler *scheduler = (__bridge RCTScheduler *)scheduler_;
  21. [scheduler.delegate schedulerDidFinishTransaction:mountingCoordinator];
  22. }
  23. void schedulerDidRequestPreliminaryViewAllocation(SurfaceId surfaceId, const ShadowView &shadowView) override
  24. {
  25. // Does nothing.
  26. // Preemptive allocation of native views on iOS does not require this call.
  27. }
  28. void schedulerDidDispatchCommand(
  29. const ShadowView &shadowView,
  30. const std::string &commandName,
  31. const folly::dynamic args) override
  32. {
  33. RCTScheduler *scheduler = (__bridge RCTScheduler *)scheduler_;
  34. [scheduler.delegate schedulerDidDispatchCommand:shadowView commandName:commandName args:args];
  35. }
  36. void schedulerDidSetJSResponder(
  37. SurfaceId surfaceId,
  38. const ShadowView &shadowView,
  39. const ShadowView &initialShadowView,
  40. bool blockNativeResponder) override
  41. {
  42. // Does nothing for now.
  43. }
  44. void schedulerDidClearJSResponder() override
  45. {
  46. // Does nothing for now.
  47. }
  48. private:
  49. void *scheduler_;
  50. };
  51. @implementation RCTScheduler {
  52. std::shared_ptr<Scheduler> _scheduler;
  53. std::shared_ptr<SchedulerDelegateProxy> _delegateProxy;
  54. }
  55. - (instancetype)initWithToolbox:(facebook::react::SchedulerToolbox)toolbox
  56. {
  57. if (self = [super init]) {
  58. _delegateProxy = std::make_shared<SchedulerDelegateProxy>((__bridge void *)self);
  59. _scheduler = std::make_shared<Scheduler>(toolbox, _delegateProxy.get());
  60. }
  61. return self;
  62. }
  63. - (void)dealloc
  64. {
  65. _scheduler->setDelegate(nullptr);
  66. }
  67. - (void)startSurfaceWithSurfaceId:(SurfaceId)surfaceId
  68. moduleName:(NSString *)moduleName
  69. initialProps:(NSDictionary *)initialProps
  70. layoutConstraints:(LayoutConstraints)layoutConstraints
  71. layoutContext:(LayoutContext)layoutContext
  72. {
  73. SystraceSection s("-[RCTScheduler startSurfaceWithSurfaceId:...]");
  74. auto props = convertIdToFollyDynamic(initialProps);
  75. _scheduler->startSurface(surfaceId, RCTStringFromNSString(moduleName), props, layoutConstraints, layoutContext);
  76. _scheduler->renderTemplateToSurface(
  77. surfaceId, props.getDefault("navigationConfig").getDefault("initialUITemplate", "").getString());
  78. }
  79. - (void)stopSurfaceWithSurfaceId:(SurfaceId)surfaceId
  80. {
  81. SystraceSection s("-[RCTScheduler stopSurfaceWithSurfaceId:]");
  82. _scheduler->stopSurface(surfaceId);
  83. }
  84. - (CGSize)measureSurfaceWithLayoutConstraints:(LayoutConstraints)layoutConstraints
  85. layoutContext:(LayoutContext)layoutContext
  86. surfaceId:(SurfaceId)surfaceId
  87. {
  88. SystraceSection s("-[RCTScheduler measureSurfaceWithLayoutConstraints:]");
  89. return RCTCGSizeFromSize(_scheduler->measureSurface(surfaceId, layoutConstraints, layoutContext));
  90. }
  91. - (void)constraintSurfaceLayoutWithLayoutConstraints:(LayoutConstraints)layoutConstraints
  92. layoutContext:(LayoutContext)layoutContext
  93. surfaceId:(SurfaceId)surfaceId
  94. {
  95. SystraceSection s("-[RCTScheduler constraintSurfaceLayoutWithLayoutConstraints:]");
  96. _scheduler->constraintSurfaceLayout(surfaceId, layoutConstraints, layoutContext);
  97. }
  98. - (ComponentDescriptor const *)findComponentDescriptorByHandle_DO_NOT_USE_THIS_IS_BROKEN:(ComponentHandle)handle
  99. {
  100. return _scheduler->findComponentDescriptorByHandle_DO_NOT_USE_THIS_IS_BROKEN(handle);
  101. }
  102. - (MountingCoordinator::Shared)mountingCoordinatorWithSurfaceId:(SurfaceId)surfaceId
  103. {
  104. return _scheduler->findMountingCoordinator(surfaceId);
  105. }
  106. @end