RCTComponentViewFactory.mm 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 "RCTComponentViewFactory.h"
  8. #import <React/RCTAssert.h>
  9. #import <react/core/ReactPrimitives.h>
  10. #import "RCTActivityIndicatorViewComponentView.h"
  11. #import "RCTImageComponentView.h"
  12. #import "RCTParagraphComponentView.h"
  13. #import "RCTRootComponentView.h"
  14. #import "RCTScrollViewComponentView.h"
  15. #import "RCTSliderComponentView.h"
  16. #import "RCTSwitchComponentView.h"
  17. #import "RCTViewComponentView.h"
  18. using namespace facebook::react;
  19. @implementation RCTComponentViewFactory {
  20. std::unordered_map<ComponentHandle, Class<RCTComponentViewProtocol>> _registry;
  21. }
  22. + (RCTComponentViewFactory *)standardComponentViewFactory
  23. {
  24. RCTAssertMainQueue();
  25. RCTComponentViewFactory *componentViewFactory = [[RCTComponentViewFactory alloc] init];
  26. [componentViewFactory registerComponentViewClass:[RCTViewComponentView class]];
  27. [componentViewFactory registerComponentViewClass:[RCTRootComponentView class]];
  28. [componentViewFactory registerComponentViewClass:[RCTScrollViewComponentView class]];
  29. [componentViewFactory registerComponentViewClass:[RCTImageComponentView class]];
  30. [componentViewFactory registerComponentViewClass:[RCTParagraphComponentView class]];
  31. [componentViewFactory registerComponentViewClass:[RCTActivityIndicatorViewComponentView class]];
  32. [componentViewFactory registerComponentViewClass:[RCTSliderComponentView class]];
  33. [componentViewFactory registerComponentViewClass:[RCTSwitchComponentView class]];
  34. return componentViewFactory;
  35. }
  36. - (void)registerComponentViewClass:(Class<RCTComponentViewProtocol>)componentViewClass
  37. {
  38. RCTAssertMainQueue();
  39. ComponentHandle componentHandle = [componentViewClass componentHandle];
  40. _registry[componentHandle] = componentViewClass;
  41. }
  42. - (UIView<RCTComponentViewProtocol> *)createComponentViewWithComponentHandle:
  43. (facebook::react::ComponentHandle)componentHandle
  44. {
  45. RCTAssertMainQueue();
  46. auto iterator = _registry.find(componentHandle);
  47. RCTAssert(
  48. iterator != _registry.end(),
  49. @"ComponentView with componentHandle `%lli` (`%s`) not found.",
  50. componentHandle,
  51. (char *)componentHandle);
  52. Class componentViewClass = iterator->second;
  53. return [[componentViewClass alloc] init];
  54. }
  55. @end