RACSubscriptingAssignmentTrampoline.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //
  2. // RACSubscriptingAssignmentTrampoline.h
  3. // ReactiveObjC
  4. //
  5. // Created by Josh Abernathy on 9/24/12.
  6. // Copyright (c) 2012 GitHub, Inc. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import <ReactiveObjC/RACEXTKeyPathCoding.h>
  10. @class RACSignal<__covariant ValueType>;
  11. NS_ASSUME_NONNULL_BEGIN
  12. /// Assigns a signal to an object property, automatically setting the given key
  13. /// path on every `next`. When the signal completes, the binding is automatically
  14. /// disposed of.
  15. ///
  16. /// There are two different versions of this macro:
  17. ///
  18. /// - RAC(TARGET, KEYPATH, NILVALUE) will bind the `KEYPATH` of `TARGET` to the
  19. /// given signal. If the signal ever sends a `nil` value, the property will be
  20. /// set to `NILVALUE` instead. `NILVALUE` may itself be `nil` for object
  21. /// properties, but an NSValue should be used for primitive properties, to
  22. /// avoid an exception if `nil` is sent (which might occur if an intermediate
  23. /// object is set to `nil`).
  24. /// - RAC(TARGET, KEYPATH) is the same as the above, but `NILVALUE` defaults to
  25. /// `nil`.
  26. ///
  27. /// See -[RACSignal setKeyPath:onObject:nilValue:] for more information about the
  28. /// binding's semantics.
  29. ///
  30. /// Examples
  31. ///
  32. /// RAC(self, objectProperty) = objectSignal;
  33. /// RAC(self, stringProperty, @"foobar") = stringSignal;
  34. /// RAC(self, integerProperty, @42) = integerSignal;
  35. ///
  36. /// WARNING: Under certain conditions, use of this macro can be thread-unsafe.
  37. /// See the documentation of -setKeyPath:onObject:nilValue:.
  38. #define RAC(TARGET, ...) \
  39. metamacro_if_eq(1, metamacro_argcount(__VA_ARGS__)) \
  40. (RAC_(TARGET, __VA_ARGS__, nil)) \
  41. (RAC_(TARGET, __VA_ARGS__))
  42. /// Do not use this directly. Use the RAC macro above.
  43. #define RAC_(TARGET, KEYPATH, NILVALUE) \
  44. [[RACSubscriptingAssignmentTrampoline alloc] initWithTarget:(TARGET) nilValue:(NILVALUE)][@keypath(TARGET, KEYPATH)]
  45. @interface RACSubscriptingAssignmentTrampoline : NSObject
  46. - (nullable instancetype)initWithTarget:(nullable id)target nilValue:(nullable id)nilValue;
  47. - (void)setObject:(RACSignal *)signal forKeyedSubscript:(NSString *)keyPath;
  48. @end
  49. NS_ASSUME_NONNULL_END