NSObject+RACPropertySubscribing.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // NSObject+RACPropertySubscribing.h
  3. // ReactiveObjC
  4. //
  5. // Created by Josh Abernathy on 3/2/12.
  6. // Copyright (c) 2012 GitHub, Inc. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import <ReactiveObjC/RACEXTKeyPathCoding.h>
  10. #import "RACmetamacros.h"
  11. /// Creates a signal which observes `KEYPATH` on `TARGET` for changes.
  12. ///
  13. /// In either case, the observation continues until `TARGET` _or self_ is
  14. /// deallocated. If any intermediate object is deallocated instead, it will be
  15. /// assumed to have been set to nil.
  16. ///
  17. /// Make sure to `@strongify(self)` when using this macro within a block! The
  18. /// macro will _always_ reference `self`, which can silently introduce a retain
  19. /// cycle within a block. As a result, you should make sure that `self` is a weak
  20. /// reference (e.g., created by `@weakify` and `@strongify`) before the
  21. /// expression that uses `RACObserve`.
  22. ///
  23. /// Examples
  24. ///
  25. /// // Observes self, and doesn't stop until self is deallocated.
  26. /// RACSignal *selfSignal = RACObserve(self, arrayController.items);
  27. ///
  28. /// // Observes the array controller, and stops when self _or_ the array
  29. /// // controller is deallocated.
  30. /// RACSignal *arrayControllerSignal = RACObserve(self.arrayController, items);
  31. ///
  32. /// // Observes obj.arrayController, and stops when self _or_ the array
  33. /// // controller is deallocated.
  34. /// RACSignal *signal2 = RACObserve(obj.arrayController, items);
  35. ///
  36. /// @weakify(self);
  37. /// RACSignal *signal3 = [anotherSignal flattenMap:^(NSArrayController *arrayController) {
  38. /// // Avoids a retain cycle because of RACObserve implicitly referencing
  39. /// // self.
  40. /// @strongify(self);
  41. /// return RACObserve(arrayController, items);
  42. /// }];
  43. ///
  44. /// Returns a signal which sends the current value of the key path on
  45. /// subscription, then sends the new value every time it changes, and sends
  46. /// completed if self or observer is deallocated.
  47. #define _RACObserve(TARGET, KEYPATH) \
  48. ({ \
  49. __weak id target_ = (TARGET); \
  50. [target_ rac_valuesForKeyPath:@keypath(TARGET, KEYPATH) observer:self]; \
  51. })
  52. #if __clang__ && (__clang_major__ >= 8)
  53. #define RACObserve(TARGET, KEYPATH) _RACObserve(TARGET, KEYPATH)
  54. #else
  55. #define RACObserve(TARGET, KEYPATH) \
  56. ({ \
  57. _Pragma("clang diagnostic push") \
  58. _Pragma("clang diagnostic ignored \"-Wreceiver-is-weak\"") \
  59. _RACObserve(TARGET, KEYPATH) \
  60. _Pragma("clang diagnostic pop") \
  61. })
  62. #endif
  63. @class RACDisposable;
  64. @class RACTwoTuple<__covariant First, __covariant Second>;
  65. @class RACSignal<__covariant ValueType>;
  66. NS_ASSUME_NONNULL_BEGIN
  67. @interface NSObject (RACPropertySubscribing)
  68. /// Creates a signal to observe the value at the given key path.
  69. ///
  70. /// The initial value is sent on subscription, the subsequent values are sent
  71. /// from whichever thread the change occured on, even if it doesn't have a valid
  72. /// scheduler.
  73. ///
  74. /// Returns a signal that immediately sends the receiver's current value at the
  75. /// given keypath, then any changes thereafter.
  76. #if OS_OBJECT_HAVE_OBJC_SUPPORT
  77. - (RACSignal *)rac_valuesForKeyPath:(NSString *)keyPath observer:(__weak NSObject *)observer;
  78. #else
  79. // Swift builds with OS_OBJECT_HAVE_OBJC_SUPPORT=0 for Playgrounds and LLDB :(
  80. - (RACSignal *)rac_valuesForKeyPath:(NSString *)keyPath observer:(NSObject *)observer;
  81. #endif
  82. /// Creates a signal to observe the changes of the given key path.
  83. ///
  84. /// The initial value is sent on subscription if `NSKeyValueObservingOptionInitial` is set.
  85. /// The subsequent values are sent from whichever thread the change occured on,
  86. /// even if it doesn't have a valid scheduler.
  87. ///
  88. /// Returns a signal that sends tuples containing the current value at the key
  89. /// path and the change dictionary for each KVO callback.
  90. #if OS_OBJECT_HAVE_OBJC_SUPPORT
  91. - (RACSignal<RACTwoTuple<id, NSDictionary *> *> *)rac_valuesAndChangesForKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options observer:(__weak NSObject *)observer;
  92. #else
  93. - (RACSignal<RACTwoTuple<id, NSDictionary *> *> *)rac_valuesAndChangesForKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options observer:(NSObject *)observer;
  94. #endif
  95. @end
  96. NS_ASSUME_NONNULL_END
  97. #define RACAble(...) \
  98. metamacro_if_eq(1, metamacro_argcount(__VA_ARGS__)) \
  99. (_RACAbleObject(self, __VA_ARGS__)) \
  100. (_RACAbleObject(__VA_ARGS__))
  101. #define _RACAbleObject(object, property) [object rac_signalForKeyPath:@keypath(object, property) observer:self]
  102. #define RACAbleWithStart(...) \
  103. metamacro_if_eq(1, metamacro_argcount(__VA_ARGS__)) \
  104. (_RACAbleWithStartObject(self, __VA_ARGS__)) \
  105. (_RACAbleWithStartObject(__VA_ARGS__))
  106. #define _RACAbleWithStartObject(object, property) [object rac_signalWithStartingValueForKeyPath:@keypath(object, property) observer:self]