RCTGenericDelegateSplitter.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. NS_ASSUME_NONNULL_BEGIN
  9. /**
  10. * General purpose implementation of Delegate Splitter (or Multicast) pattern which allows subscribing multiple
  11. * `receiving` objects to single `sending` object (which normally does not support that feature by itself).
  12. *
  13. * In the case where only one receiving object is registered, using Splitter has zero performance overhead because the
  14. * receiver is being subscribed directly. In the case where more than one receiving objects are registered, using
  15. * Splitter introduces some performance overhead.
  16. */
  17. @interface RCTGenericDelegateSplitter<DelegateT> : NSObject
  18. @property (nonatomic, copy, nullable) void (^delegateUpdateBlock)(DelegateT _Nullable delegate);
  19. /*
  20. * Creates an object with a given block that will be used to connect a `sending` object with a given `receiving` object.
  21. * The class calls the block every time after each delegate adding or removing procedure, and it calls it twice: the
  22. * first time with `nil` and the second time with actual delegate. This is required to establish a proper connection
  23. * between sending and receiving objects (to reset caches storing information about supported (or not) optional
  24. * methods).
  25. */
  26. - (instancetype)initWithDelegateUpdateBlock:(void (^)(DelegateT _Nullable delegate))block;
  27. /*
  28. * Adds and removes a delegate.
  29. * The delegates will be called in order of registration.
  30. * If a delegate returns a value, the value from the last call will be passed to the `sending` object.
  31. */
  32. - (void)addDelegate:(DelegateT)delegate;
  33. - (void)removeDelegate:(DelegateT)delegate;
  34. - (void)removeAllDelegates;
  35. @end
  36. NS_ASSUME_NONNULL_END