react-native-navigation的迁移库

RNNReactComponentRegistry.m 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. - (RNNReactView *)createComponentIfNotExists:(RNNComponentOptions *)component parentComponentId:(NSString *)parentComponentId reactViewReadyBlock:(RNNReactViewReadyCompletionBlock)reactViewReadyBlock {
  15. NSMapTable* parentComponentDict = [self componentsForParentId:parentComponentId];
  16. RNNReactView* reactView = [parentComponentDict objectForKey:component.componentId.get];
  17. if (!reactView) {
  18. reactView = (RNNReactView *)[_creator createRootViewFromComponentOptions:component reactViewReadyBlock:reactViewReadyBlock];
  19. [parentComponentDict setObject:reactView forKey:component.componentId.get];
  20. } else if (reactViewReadyBlock) {
  21. reactViewReadyBlock();
  22. }
  23. return reactView;
  24. }
  25. - (NSMapTable *)componentsForParentId:(NSString *)parentComponentId {
  26. if (![_componentStore objectForKey:parentComponentId]) {
  27. [_componentStore setObject:[NSMapTable weakToStrongObjectsMapTable] forKey:parentComponentId];;
  28. }
  29. return [_componentStore objectForKey:parentComponentId];;
  30. }
  31. - (void)clearComponentsForParentId:(NSString *)parentComponentId {
  32. [_componentStore removeObjectForKey:parentComponentId];;
  33. }
  34. - (void)removeComponent:(NSString *)componentId {
  35. if ([_componentStore objectForKey:componentId]) {
  36. [_componentStore removeObjectForKey:componentId];
  37. }
  38. }
  39. - (void)removeChildComponent:(NSString *)childId {
  40. NSMapTable* parent;
  41. NSEnumerator *enumerator = _componentStore.objectEnumerator;
  42. while ((parent = enumerator.nextObject)) {
  43. if ([parent objectForKey:childId]) {
  44. [parent removeObjectForKey:childId];
  45. return;
  46. }
  47. }
  48. }
  49. - (void)clear {
  50. [_componentStore removeAllObjects];
  51. }
  52. @end