NSObject+RACPropertySubscribing.h 4.5 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/EXTKeyPathCoding.h>
  10. #import "metamacros.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 RACSignal<__covariant ValueType>;
  65. NS_ASSUME_NONNULL_BEGIN
  66. @interface NSObject (RACPropertySubscribing)
  67. /// Creates a signal to observe the value at the given key path.
  68. ///
  69. /// The initial value is sent on subscription, the subsequent values are sent
  70. /// from whichever thread the change occured on, even if it doesn't have a valid
  71. /// scheduler.
  72. ///
  73. /// Returns a signal that immediately sends the receiver's current value at the
  74. /// given keypath, then any changes thereafter.
  75. #if OS_OBJECT_HAVE_OBJC_SUPPORT
  76. - (RACSignal *)rac_valuesForKeyPath:(NSString *)keyPath observer:(__weak NSObject *)observer;
  77. #else
  78. // Swift builds with OS_OBJECT_HAVE_OBJC_SUPPORT=0 for Playgrounds and LLDB :(
  79. - (RACSignal *)rac_valuesForKeyPath:(NSString *)keyPath observer:(NSObject *)observer;
  80. #endif
  81. /// Creates a signal to observe the changes of the given key path.
  82. ///
  83. /// The initial value is sent on subscription if `NSKeyValueObservingOptionInitial` is set.
  84. /// The subsequent values are sent from whichever thread the change occured on,
  85. /// even if it doesn't have a valid scheduler.
  86. ///
  87. /// Returns a signal that sends tuples containing the current value at the key
  88. /// path and the change dictionary for each KVO callback.
  89. #if OS_OBJECT_HAVE_OBJC_SUPPORT
  90. - (RACSignal *)rac_valuesAndChangesForKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options observer:(__weak NSObject *)observer;
  91. #else
  92. - (RACSignal *)rac_valuesAndChangesForKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options observer:(NSObject *)observer;
  93. #endif
  94. @end
  95. NS_ASSUME_NONNULL_END
  96. #define RACAble(...) \
  97. metamacro_if_eq(1, metamacro_argcount(__VA_ARGS__)) \
  98. (_RACAbleObject(self, __VA_ARGS__)) \
  99. (_RACAbleObject(__VA_ARGS__))
  100. #define _RACAbleObject(object, property) [object rac_signalForKeyPath:@keypath(object, property) observer:self]
  101. #define RACAbleWithStart(...) \
  102. metamacro_if_eq(1, metamacro_argcount(__VA_ARGS__)) \
  103. (_RACAbleWithStartObject(self, __VA_ARGS__)) \
  104. (_RACAbleWithStartObject(__VA_ARGS__))
  105. #define _RACAbleWithStartObject(object, property) [object rac_signalWithStartingValueForKeyPath:@keypath(object, property) observer:self]