RCTSurfaceBackedComponent.mm 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 "RCTSurfaceBackedComponent.h"
  8. #import <UIKit/UIKit.h>
  9. #import <ComponentKit/CKComponentSubclass.h>
  10. #import <ComponentKit/CKOverlayLayoutComponent.h>
  11. #import <RCTSurfaceHostingComponent/RCTSurfaceHostingComponent.h>
  12. #import <React/RCTSurface.h>
  13. #import "RCTSurfaceBackedComponentState.h"
  14. @implementation RCTSurfaceBackedComponent
  15. + (id)initialState
  16. {
  17. return [RCTSurfaceBackedComponentState new];
  18. }
  19. + (instancetype)newWithBridge:(RCTBridge *)bridge
  20. moduleName:(NSString *)moduleName
  21. properties:(NSDictionary *)properties
  22. options:(RCTSurfaceHostingComponentOptions)options
  23. {
  24. CKComponentScope scope(self, moduleName);
  25. RCTSurfaceBackedComponentState *state = scope.state();
  26. if (state.surface == nil || ![state.surface.moduleName isEqualToString:moduleName]) {
  27. RCTSurface *surface =
  28. [[RCTSurface alloc] initWithBridge:bridge
  29. moduleName:moduleName
  30. initialProperties:properties];
  31. [surface start];
  32. state = [RCTSurfaceBackedComponentState newWithSurface:surface];
  33. CKComponentScope::replaceState(scope, state);
  34. }
  35. else {
  36. if (![state.surface.properties isEqualToDictionary:properties]) {
  37. state.surface.properties = properties;
  38. }
  39. }
  40. RCTSurfaceHostingComponent *surfaceHostingComponent =
  41. [RCTSurfaceHostingComponent newWithSurface:state.surface
  42. options:options];
  43. CKComponent *component;
  44. if (options.activityIndicatorComponentFactory == nil || RCTSurfaceStageIsRunning(state.surface.stage)) {
  45. component = surfaceHostingComponent;
  46. } else {
  47. component = [CKOverlayLayoutComponent newWithComponent:surfaceHostingComponent
  48. overlay:options.activityIndicatorComponentFactory()];
  49. }
  50. return [super newWithComponent:component];
  51. }
  52. @end