RCTAnimatedNode.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 <React/RCTAnimatedNode.h>
  8. #import <React/RCTDefines.h>
  9. @implementation RCTAnimatedNode
  10. {
  11. NSMapTable<NSNumber *, RCTAnimatedNode *> *_childNodes;
  12. NSMapTable<NSNumber *, RCTAnimatedNode *> *_parentNodes;
  13. }
  14. - (instancetype)initWithTag:(NSNumber *)tag
  15. config:(NSDictionary<NSString *, id> *)config
  16. {
  17. if ((self = [super init])) {
  18. _nodeTag = tag;
  19. _config = [config copy];
  20. }
  21. return self;
  22. }
  23. RCT_NOT_IMPLEMENTED(- (instancetype)init)
  24. - (NSMapTable<NSNumber *, RCTAnimatedNode *> *)childNodes
  25. {
  26. return _childNodes;
  27. }
  28. - (NSMapTable<NSNumber *, RCTAnimatedNode *> *)parentNodes
  29. {
  30. return _parentNodes;
  31. }
  32. - (void)addChild:(RCTAnimatedNode *)child
  33. {
  34. if (!_childNodes) {
  35. _childNodes = [NSMapTable strongToWeakObjectsMapTable];
  36. }
  37. if (child) {
  38. [_childNodes setObject:child forKey:child.nodeTag];
  39. [child onAttachedToNode:self];
  40. }
  41. }
  42. - (void)removeChild:(RCTAnimatedNode *)child
  43. {
  44. if (!_childNodes) {
  45. return;
  46. }
  47. if (child) {
  48. [_childNodes removeObjectForKey:child.nodeTag];
  49. [child onDetachedFromNode:self];
  50. }
  51. }
  52. - (void)onAttachedToNode:(RCTAnimatedNode *)parent
  53. {
  54. if (!_parentNodes) {
  55. _parentNodes = [NSMapTable strongToWeakObjectsMapTable];
  56. }
  57. if (parent) {
  58. [_parentNodes setObject:parent forKey:parent.nodeTag];
  59. }
  60. }
  61. - (void)onDetachedFromNode:(RCTAnimatedNode *)parent
  62. {
  63. if (!_parentNodes) {
  64. return;
  65. }
  66. if (parent) {
  67. [_parentNodes removeObjectForKey:parent.nodeTag];
  68. }
  69. }
  70. - (void)detachNode
  71. {
  72. for (RCTAnimatedNode *parent in _parentNodes.objectEnumerator) {
  73. [parent removeChild:self];
  74. }
  75. for (RCTAnimatedNode *child in _childNodes.objectEnumerator) {
  76. [self removeChild:child];
  77. }
  78. }
  79. - (void)setNeedsUpdate
  80. {
  81. _needsUpdate = YES;
  82. for (RCTAnimatedNode *child in _childNodes.objectEnumerator) {
  83. [child setNeedsUpdate];
  84. }
  85. }
  86. - (void)updateNodeIfNecessary
  87. {
  88. if (_needsUpdate) {
  89. for (RCTAnimatedNode *parent in _parentNodes.objectEnumerator) {
  90. [parent updateNodeIfNecessary];
  91. }
  92. [self performUpdate];
  93. }
  94. }
  95. - (void)performUpdate
  96. {
  97. _needsUpdate = NO;
  98. // To be overridden by subclasses
  99. // This method is called on a node only if it has been marked for update
  100. // during the current update loop
  101. }
  102. - (BOOL)isManagedByFabric
  103. {
  104. for (RCTAnimatedNode *child in _childNodes.objectEnumerator) {
  105. if ([child isManagedByFabric]) {
  106. return YES;
  107. }
  108. }
  109. return NO;
  110. }
  111. @end