RCTMountingTransactionObserving.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 <UIKit/UIKit.h>
  8. #import <react/mounting/MountingTransactionMetadata.h>
  9. NS_ASSUME_NONNULL_BEGIN
  10. /*
  11. * # Achtung!
  12. * Remember, with great power comes great responsibility.
  13. * Observers of this protocol are being called several times on every single mount transaction. Any thoughtless or
  14. * suboptimal implementation of this protocol will slow down the whole app. Please, be responsible.
  15. *
  16. * # Usecases
  17. * React Native platform-specific mounting layer has limitations when it comes to notifying view components about
  18. * (coming or just happened) changes in the view tree. Implementing that generically for all components would make
  19. * everything way to slow. For instance, the mounting layer does not have dedicated APIs to notify some component that:
  20. * - Some ancestor of the component was reparented;
  21. * - Some descendant of the component was added, removed or reparented;
  22. * - Some ancestor of the component got new layout metrics (which might affect the absolute position of the component);
  23. * - The transaction which affected the component's children just finished.
  24. *
  25. * If some very specific component (e.g. a performance logger) needs to handle some of the similar use-cases, it might
  26. * rely on this protocol.
  27. *
  28. * # How to use
  29. * - Declare conformance to this protocol for the ComponentView class.
  30. * - Implement methods *only* suitable for a particular use case. Do not implement all methods if it is not strictly
  31. * required.
  32. * - Alternatively, an observer can be registered explicitly via `RCTSurface`.
  33. *
  34. * # Implementation details
  35. * The framework checks all registered view classes for conformance to the protocol and for a set of implemented
  36. * methods, then it stores this information for future use. When a view got created, the framework checks the info
  37. * associated with the class and adds the view object to the list of listeners of the particular events (if needed).
  38. * When a view got destroyed, the framework removes the view from suitable collections.
  39. */
  40. @protocol RCTMountingTransactionObserving <NSObject>
  41. @optional
  42. /*
  43. * Called right before the fist mutation instruction is executed.
  44. * Is not being called for a component view which is being mounted as part of the transaction (because the view is not
  45. * registered as an observer yet).
  46. */
  47. - (void)mountingTransactionWillMountWithMetadata:(facebook::react::MountingTransactionMetadata const &)metadata;
  48. /*
  49. * Called right after the last mutation instruction is executed.
  50. * Is not being called for a component view which was being unmounted as part of the transaction (because the view is
  51. * not registered as an observer already).
  52. */
  53. - (void)mountingTransactionDidMountWithMetadata:(facebook::react::MountingTransactionMetadata const &)metadata;
  54. @end
  55. NS_ASSUME_NONNULL_END