NSObject+RACSelectorSignal.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // NSObject+RACSelectorSignal.h
  3. // ReactiveCocoa
  4. //
  5. // Created by Josh Abernathy on 3/18/13.
  6. // Copyright (c) 2013 GitHub, Inc. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. @class RACSignal;
  10. /// The domain for any errors originating from -rac_signalForSelector:.
  11. extern NSString * const RACSelectorSignalErrorDomain;
  12. /// -rac_signalForSelector: was going to add a new method implementation for
  13. /// `selector`, but another thread added an implementation before it was able to.
  14. ///
  15. /// This will _not_ occur for cases where a method implementation exists before
  16. /// -rac_signalForSelector: is invoked.
  17. extern const NSInteger RACSelectorSignalErrorMethodSwizzlingRace;
  18. @interface NSObject (RACSelectorSignal)
  19. /// Creates a signal associated with the receiver, which will send a tuple of the
  20. /// method's arguments each time the given selector is invoked.
  21. ///
  22. /// If the selector is already implemented on the receiver, the existing
  23. /// implementation will be invoked _before_ the signal fires.
  24. ///
  25. /// If the selector is not yet implemented on the receiver, the injected
  26. /// implementation will have a `void` return type and accept only object
  27. /// arguments. Invoking the added implementation with non-object values, or
  28. /// expecting a return value, will result in undefined behavior.
  29. ///
  30. /// This is useful for changing an event or delegate callback into a signal. For
  31. /// example, on an NSView:
  32. ///
  33. /// [[view rac_signalForSelector:@selector(mouseDown:)] subscribeNext:^(RACTuple *args) {
  34. /// NSEvent *event = args.first;
  35. /// NSLog(@"mouse button pressed: %@", event);
  36. /// }];
  37. ///
  38. /// selector - The selector for whose invocations are to be observed. If it
  39. /// doesn't exist, it will be implemented to accept object arguments
  40. /// and return void. This cannot have C arrays or unions as arguments
  41. /// or C arrays, unions, structs, complex or vector types as return
  42. /// type.
  43. ///
  44. /// Returns a signal which will send a tuple of arguments upon each invocation of
  45. /// the selector, then completes when the receiver is deallocated. `next` events
  46. /// will be sent synchronously from the thread that invoked the method. If
  47. /// a runtime call fails, the signal will send an error in the
  48. /// RACSelectorSignalErrorDomain.
  49. - (RACSignal *)rac_signalForSelector:(SEL)selector;
  50. /// Behaves like -rac_signalForSelector:, but if the selector is not yet
  51. /// implemented on the receiver, its method signature is looked up within
  52. /// `protocol`, and may accept non-object arguments.
  53. ///
  54. /// If the selector is not yet implemented and has a return value, the injected
  55. /// method will return all zero bits (equal to `nil`, `NULL`, 0, 0.0f, etc.).
  56. ///
  57. /// selector - The selector for whose invocations are to be observed. If it
  58. /// doesn't exist, it will be implemented using information from
  59. /// `protocol`, and may accept non-object arguments and return
  60. /// a value. This cannot have C arrays or unions as arguments or
  61. /// return type.
  62. /// protocol - The protocol in which `selector` is declared. This will be used
  63. /// for type information if the selector is not already implemented on
  64. /// the receiver. This must not be `NULL`, and `selector` must exist
  65. /// in this protocol.
  66. ///
  67. /// Returns a signal which will send a tuple of arguments on each invocation of
  68. /// the selector, or an error in RACSelectorSignalErrorDomain if a runtime
  69. /// call fails.
  70. - (RACSignal *)rac_signalForSelector:(SEL)selector fromProtocol:(Protocol *)protocol;
  71. @end