react-native-navigation的迁移库

RNNReactComponentRegistry.m 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #import "RNNReactComponentRegistry.h"
  2. @interface RNNReactComponentRegistry () {
  3. id<RNNComponentViewCreator> _creator;
  4. NSMapTable* _componentStore;
  5. }
  6. @end
  7. @implementation RNNReactComponentRegistry
  8. - (instancetype)initWithCreator:(id<RNNComponentViewCreator>)creator {
  9. self = [super init];
  10. _creator = creator;
  11. _componentStore = [NSMapTable new];
  12. return self;
  13. }
  14. - (RNNReactButtonView *)createComponentIfNotExists:(RNNComponentOptions *)component parentComponentId:(NSString *)parentComponentId componentType:(RNNComponentType)componentType reactViewReadyBlock:(RNNReactViewReadyCompletionBlock)reactViewReadyBlock {
  15. RNNReactView* reactView = [self findComponent:component.componentId.get parentComponentId:parentComponentId];
  16. if (!reactView) {
  17. reactView = [_creator createRootView:component.name.get rootViewId:component.componentId.get ofType:componentType reactViewReadyBlock:reactViewReadyBlock];
  18. [self storeComponent:reactView componentId:component.componentId.get parentComponentId:parentComponentId];
  19. } else if (reactViewReadyBlock) {
  20. reactViewReadyBlock();
  21. }
  22. return (RNNReactButtonView *)reactView;
  23. }
  24. - (RNNReactView *)findComponent:(NSString *)componentId parentComponentId:(NSString *)parentComponentId {
  25. NSMapTable* parentComponentDict = [self componentsForParentId:parentComponentId];
  26. return [parentComponentDict objectForKey:componentId];
  27. }
  28. - (void)storeComponent:(RNNReactView *)component componentId:(NSString *)componentId parentComponentId:(NSString *)parentComponentId {
  29. NSMapTable* parentComponentDict = [self componentsForParentId:parentComponentId];
  30. [parentComponentDict setObject:component forKey:componentId];
  31. }
  32. - (NSMapTable *)componentsForParentId:(NSString *)parentComponentId {
  33. if (![_componentStore objectForKey:parentComponentId]) {
  34. [_componentStore setObject:[NSMapTable weakToStrongObjectsMapTable] forKey:parentComponentId];;
  35. }
  36. return [_componentStore objectForKey:parentComponentId];;
  37. }
  38. - (void)clearComponentsForParentId:(NSString *)parentComponentId {
  39. [_componentStore removeObjectForKey:parentComponentId];;
  40. }
  41. - (void)removeComponent:(NSString *)componentId {
  42. if ([_componentStore objectForKey:componentId]) {
  43. [_componentStore removeObjectForKey:componentId];
  44. }
  45. }
  46. - (void)removeChildComponent:(NSString *)childId {
  47. NSMapTable* parent;
  48. NSEnumerator *enumerator = _componentStore.objectEnumerator;
  49. while ((parent = enumerator.nextObject)) {
  50. if ([parent objectForKey:childId]) {
  51. [parent removeObjectForKey:childId];
  52. return;
  53. }
  54. }
  55. }
  56. - (void)clear {
  57. [_componentStore removeAllObjects];
  58. }
  59. @end