react-native-navigation的迁移库

RNNReactComponentRegistry.m 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. 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)removeChildComponent:(NSString *)childId {
  40. NSMutableDictionary* parent;
  41. while ((parent = _componentStore.objectEnumerator.nextObject)) {
  42. if ([parent objectForKey:childId]) {
  43. [parent removeObjectForKey:childId];
  44. return;
  45. }
  46. }
  47. }
  48. - (void)clear {
  49. [_componentStore removeAllObjects];
  50. }
  51. @end