react-native-navigation的迁移库

RNNReactComponentRegistry.m 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #import "RNNReactComponentRegistry.h"
  2. @interface RNNReactComponentRegistry () {
  3. id<RNNRootViewCreator> _creator;
  4. NSMapTable* _componentStore;
  5. }
  6. @end
  7. @implementation RNNReactComponentRegistry
  8. - (instancetype)initWithCreator:(id<RNNRootViewCreator>)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. NSMutableDictionary* 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. - (NSMutableDictionary *)componentsForParentId:(NSString *)parentComponentId {
  26. if (![_componentStore objectForKey:parentComponentId]) {
  27. [_componentStore setObject:[NSMutableDictionary new] 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)clear {
  40. [_componentStore removeAllObjects];
  41. }
  42. @end