RCTUIManagerObserverCoordinator.mm 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 "RCTUIManagerObserverCoordinator.h"
  8. #import <mutex>
  9. #import "RCTUIManager.h"
  10. @implementation RCTUIManagerObserverCoordinator {
  11. NSHashTable<id<RCTUIManagerObserver>> *_observers;
  12. std::mutex _mutex;
  13. }
  14. - (instancetype)init
  15. {
  16. if (self = [super init]) {
  17. _observers = [[NSHashTable alloc] initWithOptions:NSHashTableWeakMemory capacity:0];
  18. }
  19. return self;
  20. }
  21. - (void)addObserver:(id<RCTUIManagerObserver>)observer
  22. {
  23. std::lock_guard<std::mutex> lock(_mutex);
  24. [self->_observers addObject:observer];
  25. }
  26. - (void)removeObserver:(id<RCTUIManagerObserver>)observer
  27. {
  28. std::lock_guard<std::mutex> lock(_mutex);
  29. [self->_observers removeObject:observer];
  30. }
  31. #pragma mark - RCTUIManagerObserver
  32. - (void)uiManagerWillPerformLayout:(RCTUIManager *)manager
  33. {
  34. std::lock_guard<std::mutex> lock(_mutex);
  35. for (id<RCTUIManagerObserver> observer in _observers) {
  36. if ([observer respondsToSelector:@selector(uiManagerWillPerformLayout:)]) {
  37. [observer uiManagerWillPerformLayout:manager];
  38. }
  39. }
  40. }
  41. - (void)uiManagerDidPerformLayout:(RCTUIManager *)manager
  42. {
  43. std::lock_guard<std::mutex> lock(_mutex);
  44. for (id<RCTUIManagerObserver> observer in _observers) {
  45. if ([observer respondsToSelector:@selector(uiManagerDidPerformLayout:)]) {
  46. [observer uiManagerDidPerformLayout:manager];
  47. }
  48. }
  49. }
  50. - (void)uiManagerWillPerformMounting:(RCTUIManager *)manager
  51. {
  52. std::lock_guard<std::mutex> lock(_mutex);
  53. for (id<RCTUIManagerObserver> observer in _observers) {
  54. if ([observer respondsToSelector:@selector(uiManagerWillPerformMounting:)]) {
  55. [observer uiManagerWillPerformMounting:manager];
  56. }
  57. }
  58. }
  59. - (BOOL)uiManager:(RCTUIManager *)manager performMountingWithBlock:(RCTUIManagerMountingBlock)block
  60. {
  61. std::lock_guard<std::mutex> lock(_mutex);
  62. for (id<RCTUIManagerObserver> observer in _observers) {
  63. if ([observer respondsToSelector:@selector(uiManager:performMountingWithBlock:)]) {
  64. if ([observer uiManager:manager performMountingWithBlock:block]) {
  65. return YES;
  66. }
  67. }
  68. }
  69. return NO;
  70. }
  71. - (void)uiManagerDidPerformMounting:(RCTUIManager *)manager
  72. {
  73. std::lock_guard<std::mutex> lock(_mutex);
  74. for (id<RCTUIManagerObserver> observer in _observers) {
  75. if ([observer respondsToSelector:@selector(uiManagerDidPerformMounting:)]) {
  76. [observer uiManagerDidPerformMounting:manager];
  77. }
  78. }
  79. }
  80. @end