RACSignal+Operations.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. //
  2. // RACSignal+Operations.h
  3. // ReactiveObjC
  4. //
  5. // Created by Justin Spahr-Summers on 2012-09-06.
  6. // Copyright (c) 2012 GitHub, Inc. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "RACSignal.h"
  10. @class RACCommand;
  11. @class RACDisposable;
  12. @class RACMulticastConnection<__covariant ValueType>;
  13. @class RACScheduler;
  14. @class RACSequence<__covariant ValueType>;
  15. @class RACSubject<ValueType>;
  16. @class RACTuple;
  17. @class RACTwoTuple<__covariant First, __covariant Second>;
  18. @class RACEvent<__covariant ValueType>;
  19. @class RACGroupedSignal;
  20. @protocol RACSubscriber;
  21. NS_ASSUME_NONNULL_BEGIN
  22. /// The domain for errors originating in RACSignal operations.
  23. extern NSErrorDomain const RACSignalErrorDomain;
  24. typedef NS_ERROR_ENUM(RACSignalErrorDomain, RACSignalError) {
  25. /// The error code used with -timeout:.
  26. RACSignalErrorTimedOut = 1,
  27. /// The error code used when a value passed into +switch:cases:default: does not
  28. /// match any of the cases, and no default was given.
  29. RACSignalErrorNoMatchingCase = 2,
  30. };
  31. @interface RACSignal<__covariant ValueType> (Operations)
  32. /// Do the given block on `next`. This should be used to inject side effects into
  33. /// the signal.
  34. - (RACSignal<ValueType> *)doNext:(void (^)(ValueType _Nullable x))block RAC_WARN_UNUSED_RESULT;
  35. /// Do the given block on `error`. This should be used to inject side effects
  36. /// into the signal.
  37. - (RACSignal<ValueType> *)doError:(void (^)(NSError * _Nonnull error))block RAC_WARN_UNUSED_RESULT;
  38. /// Do the given block on `completed`. This should be used to inject side effects
  39. /// into the signal.
  40. - (RACSignal<ValueType> *)doCompleted:(void (^)(void))block RAC_WARN_UNUSED_RESULT;
  41. /// Sends `next`s only if we don't receive another `next` in `interval` seconds.
  42. ///
  43. /// If a `next` is received, and then another `next` is received before
  44. /// `interval` seconds have passed, the first value is discarded.
  45. ///
  46. /// After `interval` seconds have passed since the most recent `next` was sent,
  47. /// the most recent `next` is forwarded on the scheduler that the value was
  48. /// originally received on. If +[RACScheduler currentScheduler] was nil at the
  49. /// time, a private background scheduler is used.
  50. ///
  51. /// Returns a signal which sends throttled and delayed `next` events. Completion
  52. /// and errors are always forwarded immediately.
  53. - (RACSignal<ValueType> *)throttle:(NSTimeInterval)interval RAC_WARN_UNUSED_RESULT;
  54. /// Throttles `next`s for which `predicate` returns YES.
  55. ///
  56. /// When `predicate` returns YES for a `next`:
  57. ///
  58. /// 1. If another `next` is received before `interval` seconds have passed, the
  59. /// prior value is discarded. This happens regardless of whether the new
  60. /// value will be throttled.
  61. /// 2. After `interval` seconds have passed since the value was originally
  62. /// received, it will be forwarded on the scheduler that it was received
  63. /// upon. If +[RACScheduler currentScheduler] was nil at the time, a private
  64. /// background scheduler is used.
  65. ///
  66. /// When `predicate` returns NO for a `next`, it is forwarded immediately,
  67. /// without any throttling.
  68. ///
  69. /// interval - The number of seconds for which to buffer the latest value that
  70. /// passes `predicate`.
  71. /// predicate - Passed each `next` from the receiver, this block returns
  72. /// whether the given value should be throttled. This argument must
  73. /// not be nil.
  74. ///
  75. /// Returns a signal which sends `next` events, throttled when `predicate`
  76. /// returns YES. Completion and errors are always forwarded immediately.
  77. - (RACSignal<ValueType> *)throttle:(NSTimeInterval)interval valuesPassingTest:(BOOL (^)(id _Nullable next))predicate RAC_WARN_UNUSED_RESULT;
  78. /// Forwards `next` and `completed` events after delaying for `interval` seconds
  79. /// on the current scheduler (on which the events were delivered).
  80. ///
  81. /// If +[RACScheduler currentScheduler] is nil when `next` or `completed` is
  82. /// received, a private background scheduler is used.
  83. ///
  84. /// Returns a signal which sends delayed `next` and `completed` events. Errors
  85. /// are always forwarded immediately.
  86. - (RACSignal<ValueType> *)delay:(NSTimeInterval)interval RAC_WARN_UNUSED_RESULT;
  87. /// Resubscribes when the signal completes.
  88. - (RACSignal<ValueType> *)repeat RAC_WARN_UNUSED_RESULT;
  89. /// Executes the given block each time a subscription is created.
  90. ///
  91. /// block - A block which defines the subscription side effects. Cannot be `nil`.
  92. ///
  93. /// Example:
  94. ///
  95. /// // Write new file, with backup.
  96. /// [[[[fileManager
  97. /// rac_createFileAtPath:path contents:data]
  98. /// initially:^{
  99. /// // 2. Second, backup current file
  100. /// [fileManager moveItemAtPath:path toPath:backupPath error:nil];
  101. /// }]
  102. /// initially:^{
  103. /// // 1. First, acquire write lock.
  104. /// [writeLock lock];
  105. /// }]
  106. /// finally:^{
  107. /// [writeLock unlock];
  108. /// }];
  109. ///
  110. /// Returns a signal that passes through all events of the receiver, plus
  111. /// introduces side effects which occur prior to any subscription side effects
  112. /// of the receiver.
  113. - (RACSignal<ValueType> *)initially:(void (^)(void))block RAC_WARN_UNUSED_RESULT;
  114. /// Executes the given block when the signal completes or errors.
  115. - (RACSignal<ValueType> *)finally:(void (^)(void))block RAC_WARN_UNUSED_RESULT;
  116. /// Divides the receiver's `next`s into buffers which deliver every `interval`
  117. /// seconds.
  118. ///
  119. /// interval - The interval in which values are grouped into one buffer.
  120. /// scheduler - The scheduler upon which the returned signal will deliver its
  121. /// values. This must not be nil or +[RACScheduler
  122. /// immediateScheduler].
  123. ///
  124. /// Returns a signal which sends RACTuples of the buffered values at each
  125. /// interval on `scheduler`. When the receiver completes, any currently-buffered
  126. /// values will be sent immediately.
  127. - (RACSignal<RACTuple *> *)bufferWithTime:(NSTimeInterval)interval onScheduler:(RACScheduler *)scheduler;
  128. /// Collects all receiver's `next`s into a NSArray. Nil values will be converted
  129. /// to NSNull.
  130. ///
  131. /// This corresponds to the `ToArray` method in Rx.
  132. ///
  133. /// Returns a signal which sends a single NSArray when the receiver completes
  134. /// successfully.
  135. - (RACSignal<NSArray<ValueType> *> *)collect RAC_WARN_UNUSED_RESULT;
  136. /// Takes the last `count` `next`s after the receiving signal completes.
  137. - (RACSignal<ValueType> *)takeLast:(NSUInteger)count RAC_WARN_UNUSED_RESULT;
  138. /// Combines the latest values from the receiver and the given signal into
  139. /// 2-tuples, once both have sent at least one `next`.
  140. ///
  141. /// Any additional `next`s will result in a new RACTuple with the latest values
  142. /// from both signals.
  143. ///
  144. /// signal - The signal to combine with. This argument must not be nil.
  145. ///
  146. /// Returns a signal which sends RACTuples of the combined values, forwards any
  147. /// `error` events, and completes when both input signals complete.
  148. - (RACSignal<RACTwoTuple<ValueType, id> *> *)combineLatestWith:(RACSignal *)signal RAC_WARN_UNUSED_RESULT;
  149. /// Combines the latest values from the given signals into RACTuples, once all
  150. /// the signals have sent at least one `next`.
  151. ///
  152. /// Any additional `next`s will result in a new RACTuple with the latest values
  153. /// from all signals.
  154. ///
  155. /// signals - The signals to combine. If this collection is empty, the returned
  156. /// signal will immediately complete upon subscription.
  157. ///
  158. /// Returns a signal which sends RACTuples of the combined values, forwards any
  159. /// `error` events, and completes when all input signals complete.
  160. + (RACSignal<RACTuple *> *)combineLatest:(id<NSFastEnumeration>)signals RAC_WARN_UNUSED_RESULT;
  161. /// Combines signals using +combineLatest:, then reduces the resulting tuples
  162. /// into a single value using -reduceEach:.
  163. ///
  164. /// signals - The signals to combine. If this collection is empty, the
  165. /// returned signal will immediately complete upon subscription.
  166. /// reduceBlock - The block which reduces the latest values from all the
  167. /// signals into one value. It must take as many arguments as the
  168. /// number of signals given. Each argument will be an object
  169. /// argument. The return value must be an object. This argument
  170. /// must not be nil.
  171. ///
  172. /// Example:
  173. ///
  174. /// [RACSignal combineLatest:@[ stringSignal, intSignal ] reduce:^(NSString *string, NSNumber *number) {
  175. /// return [NSString stringWithFormat:@"%@: %@", string, number];
  176. /// }];
  177. ///
  178. /// Returns a signal which sends the results from each invocation of
  179. /// `reduceBlock`.
  180. + (RACSignal<ValueType> *)combineLatest:(id<NSFastEnumeration>)signals reduce:(RACGenericReduceBlock)reduceBlock RAC_WARN_UNUSED_RESULT;
  181. /// Merges the receiver and the given signal with `+merge:` and returns the
  182. /// resulting signal.
  183. - (RACSignal *)merge:(RACSignal *)signal RAC_WARN_UNUSED_RESULT;
  184. /// Sends the latest `next` from any of the signals.
  185. ///
  186. /// Returns a signal that passes through values from each of the given signals,
  187. /// and sends `completed` when all of them complete. If any signal sends an error,
  188. /// the returned signal sends `error` immediately.
  189. + (RACSignal<ValueType> *)merge:(id<NSFastEnumeration>)signals RAC_WARN_UNUSED_RESULT;
  190. /// Merges the signals sent by the receiver into a flattened signal, but only
  191. /// subscribes to `maxConcurrent` number of signals at a time. New signals are
  192. /// queued and subscribed to as other signals complete.
  193. ///
  194. /// If an error occurs on any of the signals, it is sent on the returned signal.
  195. /// It completes only after the receiver and all sent signals have completed.
  196. ///
  197. /// This corresponds to `Merge<TSource>(IObservable<IObservable<TSource>>, Int32)`
  198. /// in Rx.
  199. ///
  200. /// maxConcurrent - the maximum number of signals to subscribe to at a
  201. /// time. If 0, it subscribes to an unlimited number of
  202. /// signals.
  203. - (RACSignal *)flatten:(NSUInteger)maxConcurrent RAC_WARN_UNUSED_RESULT;
  204. /// Ignores all `next`s from the receiver, waits for the receiver to complete,
  205. /// then subscribes to a new signal.
  206. ///
  207. /// block - A block which will create or obtain a new signal to subscribe to,
  208. /// executed only after the receiver completes. This block must not be
  209. /// nil, and it must not return a nil signal.
  210. ///
  211. /// Returns a signal which will pass through the events of the signal created in
  212. /// `block`. If the receiver errors out, the returned signal will error as well.
  213. - (RACSignal *)then:(RACSignal * (^)(void))block RAC_WARN_UNUSED_RESULT;
  214. /// Concats the inner signals of a signal of signals.
  215. - (RACSignal *)concat RAC_WARN_UNUSED_RESULT;
  216. /// Aggregates the `next` values of the receiver into a single combined value.
  217. ///
  218. /// The algorithm proceeds as follows:
  219. ///
  220. /// 1. `start` is passed into the block as the `running` value, and the first
  221. /// element of the receiver is passed into the block as the `next` value.
  222. /// 2. The result of the invocation (`running`) and the next element of the
  223. /// receiver (`next`) is passed into `reduceBlock`.
  224. /// 3. Steps 2 and 3 are repeated until all values have been processed.
  225. /// 4. The last result of `reduceBlock` is sent on the returned signal.
  226. ///
  227. /// This method is similar to -scanWithStart:reduce:, except that only the
  228. /// final result is sent on the returned signal.
  229. ///
  230. /// start - The value to be combined with the first element of the
  231. /// receiver. This value may be `nil`.
  232. /// reduceBlock - The block that describes how to combine values of the
  233. /// receiver. If the receiver is empty, this block will never be
  234. /// invoked. Cannot be nil.
  235. ///
  236. /// Returns a signal that will send the aggregated value when the receiver
  237. /// completes, then itself complete. If the receiver never sends any values,
  238. /// `start` will be sent instead.
  239. - (RACSignal *)aggregateWithStart:(id)start reduce:(id (^)(id running, id next))reduceBlock RAC_WARN_UNUSED_RESULT;
  240. /// Aggregates the `next` values of the receiver into a single combined value.
  241. /// This is indexed version of -aggregateWithStart:reduce:.
  242. ///
  243. /// start - The value to be combined with the first element of the
  244. /// receiver. This value may be `nil`.
  245. /// reduceBlock - The block that describes how to combine values of the
  246. /// receiver. This block takes zero-based index value as the last
  247. /// parameter. If the receiver is empty, this block will never be
  248. /// invoked. Cannot be nil.
  249. ///
  250. /// Returns a signal that will send the aggregated value when the receiver
  251. /// completes, then itself complete. If the receiver never sends any values,
  252. /// `start` will be sent instead.
  253. - (RACSignal *)aggregateWithStart:(id)start reduceWithIndex:(id (^)(id running, id next, NSUInteger index))reduceBlock RAC_WARN_UNUSED_RESULT;
  254. /// Aggregates the `next` values of the receiver into a single combined value.
  255. ///
  256. /// This invokes `startFactory` block on each subscription, then calls
  257. /// -aggregateWithStart:reduce: with the return value of the block as start value.
  258. ///
  259. /// startFactory - The block that returns start value which will be combined
  260. /// with the first element of the receiver. Cannot be nil.
  261. /// reduceBlock - The block that describes how to combine values of the
  262. /// receiver. If the receiver is empty, this block will never be
  263. /// invoked. Cannot be nil.
  264. ///
  265. /// Returns a signal that will send the aggregated value when the receiver
  266. /// completes, then itself complete. If the receiver never sends any values,
  267. /// the return value of `startFactory` will be sent instead.
  268. - (RACSignal *)aggregateWithStartFactory:(id (^)(void))startFactory reduce:(id (^)(id running, id next))reduceBlock RAC_WARN_UNUSED_RESULT;
  269. /// Invokes -setKeyPath:onObject:nilValue: with `nil` for the nil value.
  270. ///
  271. /// WARNING: Under certain conditions, this method is known to be thread-unsafe.
  272. /// See the description in -setKeyPath:onObject:nilValue:.
  273. - (RACDisposable *)setKeyPath:(NSString *)keyPath onObject:(NSObject *)object;
  274. /// Binds the receiver to an object, automatically setting the given key path on
  275. /// every `next`. When the signal completes, the binding is automatically
  276. /// disposed of.
  277. ///
  278. /// WARNING: Under certain conditions, this method is known to be thread-unsafe.
  279. /// A crash can result if `object` is deallocated concurrently on
  280. /// another thread within a window of time between a value being sent
  281. /// on this signal and immediately prior to the invocation of
  282. /// -setValue:forKeyPath:, which sets the property. To prevent this,
  283. /// ensure `object` is deallocated on the same thread the receiver
  284. /// sends on, or ensure that the returned disposable is disposed of
  285. /// before `object` deallocates.
  286. /// See https://github.com/ReactiveCocoa/ReactiveCocoa/pull/1184
  287. ///
  288. /// Sending an error on the signal is considered undefined behavior, and will
  289. /// generate an assertion failure in Debug builds.
  290. ///
  291. /// A given key on an object should only have one active signal bound to it at any
  292. /// given time. Binding more than one signal to the same property is considered
  293. /// undefined behavior.
  294. ///
  295. /// keyPath - The key path to update with `next`s from the receiver.
  296. /// object - The object that `keyPath` is relative to.
  297. /// nilValue - The value to set at the key path whenever `nil` is sent by the
  298. /// receiver. This may be nil when binding to object properties, but
  299. /// an NSValue should be used for primitive properties, to avoid an
  300. /// exception if `nil` is sent (which might occur if an intermediate
  301. /// object is set to `nil`).
  302. ///
  303. /// Returns a disposable which can be used to terminate the binding.
  304. - (RACDisposable *)setKeyPath:(NSString *)keyPath onObject:(NSObject *)object nilValue:(nullable id)nilValue;
  305. /// Sends NSDate.date every `interval` seconds.
  306. ///
  307. /// interval - The time interval in seconds at which the current time is sent.
  308. /// scheduler - The scheduler upon which the current NSDate should be sent. This
  309. /// must not be nil or +[RACScheduler immediateScheduler].
  310. ///
  311. /// Returns a signal that sends the current date/time every `interval` on
  312. /// `scheduler`.
  313. + (RACSignal<NSDate *> *)interval:(NSTimeInterval)interval onScheduler:(RACScheduler *)scheduler RAC_WARN_UNUSED_RESULT;
  314. /// Sends NSDate.date at intervals of at least `interval` seconds, up to
  315. /// approximately `interval` + `leeway` seconds.
  316. ///
  317. /// The created signal will defer sending each `next` for at least `interval`
  318. /// seconds, and for an additional amount of time up to `leeway` seconds in the
  319. /// interest of performance or power consumption. Note that some additional
  320. /// latency is to be expected, even when specifying a `leeway` of 0.
  321. ///
  322. /// interval - The base interval between `next`s.
  323. /// scheduler - The scheduler upon which the current NSDate should be sent. This
  324. /// must not be nil or +[RACScheduler immediateScheduler].
  325. /// leeway - The maximum amount of additional time the `next` can be deferred.
  326. ///
  327. /// Returns a signal that sends the current date/time at intervals of at least
  328. /// `interval seconds` up to approximately `interval` + `leeway` seconds on
  329. /// `scheduler`.
  330. + (RACSignal<NSDate *> *)interval:(NSTimeInterval)interval onScheduler:(RACScheduler *)scheduler withLeeway:(NSTimeInterval)leeway RAC_WARN_UNUSED_RESULT;
  331. /// Takes `next`s until the `signalTrigger` sends `next` or `completed`.
  332. ///
  333. /// Returns a signal which passes through all events from the receiver until
  334. /// `signalTrigger` sends `next` or `completed`, at which point the returned signal
  335. /// will send `completed`.
  336. - (RACSignal<ValueType> *)takeUntil:(RACSignal *)signalTrigger RAC_WARN_UNUSED_RESULT;
  337. /// Takes `next`s until the `replacement` sends an event.
  338. ///
  339. /// replacement - The signal which replaces the receiver as soon as it sends an
  340. /// event.
  341. ///
  342. /// Returns a signal which passes through `next`s and `error` from the receiver
  343. /// until `replacement` sends an event, at which point the returned signal will
  344. /// send that event and switch to passing through events from `replacement`
  345. /// instead, regardless of whether the receiver has sent events already.
  346. - (RACSignal *)takeUntilReplacement:(RACSignal *)replacement RAC_WARN_UNUSED_RESULT;
  347. /// Subscribes to the returned signal when an error occurs.
  348. - (RACSignal *)catch:(RACSignal * (^)(NSError * _Nonnull error))catchBlock RAC_WARN_UNUSED_RESULT;
  349. /// Subscribes to the given signal when an error occurs.
  350. - (RACSignal *)catchTo:(RACSignal *)signal RAC_WARN_UNUSED_RESULT;
  351. /// Returns a signal that will either immediately send the return value of
  352. /// `tryBlock` and complete, or error using the `NSError` passed out from the
  353. /// block.
  354. ///
  355. /// tryBlock - An action that performs some computation that could fail. If the
  356. /// block returns nil, the block must return an error via the
  357. /// `errorPtr` parameter.
  358. ///
  359. /// Example:
  360. ///
  361. /// [RACSignal try:^(NSError **error) {
  362. /// return [NSJSONSerialization JSONObjectWithData:someJSONData options:0 error:error];
  363. /// }];
  364. + (RACSignal<ValueType> *)try:(nullable ValueType (^)(NSError **errorPtr))tryBlock RAC_WARN_UNUSED_RESULT;
  365. /// Runs `tryBlock` against each of the receiver's values, passing values
  366. /// until `tryBlock` returns NO, or the receiver completes.
  367. ///
  368. /// tryBlock - An action to run against each of the receiver's values.
  369. /// The block should return YES to indicate that the action was
  370. /// successful. This block must not be nil.
  371. ///
  372. /// Example:
  373. ///
  374. /// // The returned signal will send an error if data values cannot be
  375. /// // written to `someFileURL`.
  376. /// [signal try:^(NSData *data, NSError **errorPtr) {
  377. /// return [data writeToURL:someFileURL options:NSDataWritingAtomic error:errorPtr];
  378. /// }];
  379. ///
  380. /// Returns a signal which passes through all the values of the receiver. If
  381. /// `tryBlock` fails for any value, the returned signal will error using the
  382. /// `NSError` passed out from the block.
  383. - (RACSignal<ValueType> *)try:(BOOL (^)(id _Nullable value, NSError **errorPtr))tryBlock RAC_WARN_UNUSED_RESULT;
  384. /// Runs `mapBlock` against each of the receiver's values, mapping values until
  385. /// `mapBlock` returns nil, or the receiver completes.
  386. ///
  387. /// mapBlock - An action to map each of the receiver's values. The block should
  388. /// return a non-nil value to indicate that the action was successful.
  389. /// This block must not be nil.
  390. ///
  391. /// Example:
  392. ///
  393. /// // The returned signal will send an error if data cannot be read from
  394. /// // `fileURL`.
  395. /// [signal tryMap:^(NSURL *fileURL, NSError **errorPtr) {
  396. /// return [NSData dataWithContentsOfURL:fileURL options:0 error:errorPtr];
  397. /// }];
  398. ///
  399. /// Returns a signal which transforms all the values of the receiver. If
  400. /// `mapBlock` returns nil for any value, the returned signal will error using
  401. /// the `NSError` passed out from the block.
  402. - (RACSignal *)tryMap:(id (^)(id _Nullable value, NSError **errorPtr))mapBlock RAC_WARN_UNUSED_RESULT;
  403. /// Returns the first `next`. Note that this is a blocking call.
  404. - (nullable ValueType)first;
  405. /// Returns the first `next` or `defaultValue` if the signal completes or errors
  406. /// without sending a `next`. Note that this is a blocking call.
  407. - (nullable ValueType)firstOrDefault:(nullable ValueType)defaultValue;
  408. /// Returns the first `next` or `defaultValue` if the signal completes or errors
  409. /// without sending a `next`. If an error occurs success will be NO and error
  410. /// will be populated. Note that this is a blocking call.
  411. ///
  412. /// Both success and error may be NULL.
  413. - (nullable ValueType)firstOrDefault:(nullable ValueType)defaultValue success:(nullable BOOL *)success error:(NSError * _Nullable * _Nullable)error;
  414. /// Blocks the caller and waits for the signal to complete.
  415. ///
  416. /// error - If not NULL, set to any error that occurs.
  417. ///
  418. /// Returns whether the signal completed successfully. If NO, `error` will be set
  419. /// to the error that occurred.
  420. - (BOOL)waitUntilCompleted:(NSError * _Nullable * _Nullable)error;
  421. /// Defers creation of a signal until the signal's actually subscribed to.
  422. ///
  423. /// This can be used to effectively turn a hot signal into a cold signal.
  424. + (RACSignal<ValueType> *)defer:(RACSignal<ValueType> * (^)(void))block RAC_WARN_UNUSED_RESULT;
  425. /// Every time the receiver sends a new RACSignal, subscribes and sends `next`s and
  426. /// `error`s only for that signal.
  427. ///
  428. /// The receiver must be a signal of signals.
  429. ///
  430. /// Returns a signal which passes through `next`s and `error`s from the latest
  431. /// signal sent by the receiver, and sends `completed` when both the receiver and
  432. /// the last sent signal complete.
  433. - (RACSignal *)switchToLatest RAC_WARN_UNUSED_RESULT;
  434. /// Switches between the signals in `cases` as well as `defaultSignal` based on
  435. /// the latest value sent by `signal`.
  436. ///
  437. /// signal - A signal of objects used as keys in the `cases` dictionary.
  438. /// This argument must not be nil.
  439. /// cases - A dictionary that has signals as values. This argument must
  440. /// not be nil. A RACTupleNil key in this dictionary will match
  441. /// nil `next` events that are received on `signal`.
  442. /// defaultSignal - The signal to pass through after `signal` sends a value for
  443. /// which `cases` does not contain a signal. If nil, any
  444. /// unmatched values will result in
  445. /// a RACSignalErrorNoMatchingCase error.
  446. ///
  447. /// Returns a signal which passes through `next`s and `error`s from one of the
  448. /// the signals in `cases` or `defaultSignal`, and sends `completed` when both
  449. /// `signal` and the last used signal complete. If no `defaultSignal` is given,
  450. /// an unmatched `next` will result in an error on the returned signal.
  451. + (RACSignal<ValueType> *)switch:(RACSignal *)signal cases:(NSDictionary *)cases default:(nullable RACSignal *)defaultSignal RAC_WARN_UNUSED_RESULT;
  452. /// Switches between `trueSignal` and `falseSignal` based on the latest value
  453. /// sent by `boolSignal`.
  454. ///
  455. /// boolSignal - A signal of BOOLs determining whether `trueSignal` or
  456. /// `falseSignal` should be active. This argument must not be nil.
  457. /// trueSignal - The signal to pass through after `boolSignal` has sent YES.
  458. /// This argument must not be nil.
  459. /// falseSignal - The signal to pass through after `boolSignal` has sent NO. This
  460. /// argument must not be nil.
  461. ///
  462. /// Returns a signal which passes through `next`s and `error`s from `trueSignal`
  463. /// and/or `falseSignal`, and sends `completed` when both `boolSignal` and the
  464. /// last switched signal complete.
  465. + (RACSignal<ValueType> *)if:(RACSignal<NSNumber *> *)boolSignal then:(RACSignal *)trueSignal else:(RACSignal *)falseSignal RAC_WARN_UNUSED_RESULT;
  466. /// Adds every `next` to an array. Nils are represented by NSNulls. Note that
  467. /// this is a blocking call.
  468. ///
  469. /// **This is not the same as the `ToArray` method in Rx.** See -collect for
  470. /// that behavior instead.
  471. ///
  472. /// Returns the array of `next` values, or nil if an error occurs.
  473. - (nullable NSArray<ValueType> *)toArray;
  474. /// Adds every `next` to a sequence. Nils are represented by NSNulls.
  475. ///
  476. /// This corresponds to the `ToEnumerable` method in Rx.
  477. ///
  478. /// Returns a sequence which provides values from the signal as they're sent.
  479. /// Trying to retrieve a value from the sequence which has not yet been sent will
  480. /// block.
  481. @property (nonatomic, strong, readonly) RACSequence<ValueType> *sequence;
  482. /// Creates and returns a multicast connection. This allows you to share a single
  483. /// subscription to the underlying signal.
  484. - (RACMulticastConnection<ValueType> *)publish RAC_WARN_UNUSED_RESULT;
  485. /// Creates and returns a multicast connection that pushes values into the given
  486. /// subject. This allows you to share a single subscription to the underlying
  487. /// signal.
  488. - (RACMulticastConnection<ValueType> *)multicast:(RACSubject<ValueType> *)subject RAC_WARN_UNUSED_RESULT;
  489. /// Multicasts the signal to a RACReplaySubject of unlimited capacity, and
  490. /// immediately connects to the resulting RACMulticastConnection.
  491. ///
  492. /// Returns the connected, multicasted signal.
  493. - (RACSignal<ValueType> *)replay;
  494. /// Multicasts the signal to a RACReplaySubject of capacity 1, and immediately
  495. /// connects to the resulting RACMulticastConnection.
  496. ///
  497. /// Returns the connected, multicasted signal.
  498. - (RACSignal<ValueType> *)replayLast;
  499. /// Multicasts the signal to a RACReplaySubject of unlimited capacity, and
  500. /// lazily connects to the resulting RACMulticastConnection.
  501. ///
  502. /// This means the returned signal will subscribe to the multicasted signal only
  503. /// when the former receives its first subscription.
  504. ///
  505. /// Returns the lazily connected, multicasted signal.
  506. - (RACSignal<ValueType> *)replayLazily;
  507. /// Sends an error after `interval` seconds if the source doesn't complete
  508. /// before then.
  509. ///
  510. /// The error will be in the RACSignalErrorDomain and have a code of
  511. /// RACSignalErrorTimedOut.
  512. ///
  513. /// interval - The number of seconds after which the signal should error out.
  514. /// scheduler - The scheduler upon which any timeout error should be sent. This
  515. /// must not be nil or +[RACScheduler immediateScheduler].
  516. ///
  517. /// Returns a signal that passes through the receiver's events, until the stream
  518. /// finishes or times out, at which point an error will be sent on `scheduler`.
  519. - (RACSignal<ValueType> *)timeout:(NSTimeInterval)interval onScheduler:(RACScheduler *)scheduler RAC_WARN_UNUSED_RESULT;
  520. /// Creates and returns a signal that delivers its events on the given scheduler.
  521. /// Any side effects of the receiver will still be performed on the original
  522. /// thread.
  523. ///
  524. /// This is ideal when the signal already performs its work on the desired
  525. /// thread, but you want to handle its events elsewhere.
  526. ///
  527. /// This corresponds to the `ObserveOn` method in Rx.
  528. - (RACSignal<ValueType> *)deliverOn:(RACScheduler *)scheduler RAC_WARN_UNUSED_RESULT;
  529. /// Creates and returns a signal that executes its side effects and delivers its
  530. /// events on the given scheduler.
  531. ///
  532. /// Use of this operator should be avoided whenever possible, because the
  533. /// receiver's side effects may not be safe to run on another thread. If you just
  534. /// want to receive the signal's events on `scheduler`, use -deliverOn: instead.
  535. - (RACSignal<ValueType> *)subscribeOn:(RACScheduler *)scheduler RAC_WARN_UNUSED_RESULT;
  536. /// Creates and returns a signal that delivers its events on the main thread.
  537. /// If events are already being sent on the main thread, they may be passed on
  538. /// without delay. An event will instead be queued for later delivery on the main
  539. /// thread if sent on another thread, or if a previous event is already being
  540. /// processed, or has been queued.
  541. ///
  542. /// Any side effects of the receiver will still be performed on the original
  543. /// thread.
  544. ///
  545. /// This can be used when a signal will cause UI updates, to avoid potential
  546. /// flicker caused by delayed delivery of events, such as the first event from
  547. /// a RACObserve at view instantiation.
  548. - (RACSignal<ValueType> *)deliverOnMainThread RAC_WARN_UNUSED_RESULT;
  549. /// Groups each received object into a group, as determined by calling `keyBlock`
  550. /// with that object. The object sent is transformed by calling `transformBlock`
  551. /// with the object. If `transformBlock` is nil, it sends the original object.
  552. ///
  553. /// The returned signal is a signal of RACGroupedSignal.
  554. - (RACSignal<RACGroupedSignal *> *)groupBy:(id<NSCopying> _Nullable (^)(id _Nullable object))keyBlock transform:(nullable id _Nullable (^)(id _Nullable object))transformBlock RAC_WARN_UNUSED_RESULT;
  555. /// Calls -[RACSignal groupBy:keyBlock transform:nil].
  556. - (RACSignal<RACGroupedSignal *> *)groupBy:(id<NSCopying> _Nullable (^)(id _Nullable object))keyBlock RAC_WARN_UNUSED_RESULT;
  557. /// Sends an [NSNumber numberWithBool:YES] if the receiving signal sends any
  558. /// objects.
  559. - (RACSignal<NSNumber *> *)any RAC_WARN_UNUSED_RESULT;
  560. /// Sends an [NSNumber numberWithBool:YES] if the receiving signal sends any
  561. /// objects that pass `predicateBlock`.
  562. ///
  563. /// predicateBlock - cannot be nil.
  564. - (RACSignal<NSNumber *> *)any:(BOOL (^)(id _Nullable object))predicateBlock RAC_WARN_UNUSED_RESULT;
  565. /// Sends an [NSNumber numberWithBool:YES] if all the objects the receiving
  566. /// signal sends pass `predicateBlock`.
  567. ///
  568. /// predicateBlock - cannot be nil.
  569. - (RACSignal<NSNumber *> *)all:(BOOL (^)(id _Nullable object))predicateBlock RAC_WARN_UNUSED_RESULT;
  570. /// Resubscribes to the receiving signal if an error occurs, up until it has
  571. /// retried the given number of times.
  572. ///
  573. /// retryCount - if 0, it keeps retrying until it completes.
  574. - (RACSignal<ValueType> *)retry:(NSInteger)retryCount RAC_WARN_UNUSED_RESULT;
  575. /// Resubscribes to the receiving signal if an error occurs.
  576. - (RACSignal<ValueType> *)retry RAC_WARN_UNUSED_RESULT;
  577. /// Sends the latest value from the receiver only when `sampler` sends a value.
  578. /// The returned signal could repeat values if `sampler` fires more often than
  579. /// the receiver. Values from `sampler` are ignored before the receiver sends
  580. /// its first value.
  581. ///
  582. /// sampler - The signal that controls when the latest value from the receiver
  583. /// is sent. Cannot be nil.
  584. - (RACSignal<ValueType> *)sample:(RACSignal *)sampler RAC_WARN_UNUSED_RESULT;
  585. /// Ignores all `next`s from the receiver.
  586. ///
  587. /// Returns a signal which only passes through `error` or `completed` events from
  588. /// the receiver.
  589. - (RACSignal *)ignoreValues RAC_WARN_UNUSED_RESULT;
  590. /// Converts each of the receiver's events into a RACEvent object.
  591. ///
  592. /// Returns a signal which sends the receiver's events as RACEvents, and
  593. /// completes after the receiver sends `completed` or `error`.
  594. - (RACSignal<RACEvent<ValueType> *> *)materialize RAC_WARN_UNUSED_RESULT;
  595. /// Converts each RACEvent in the receiver back into "real" RACSignal events.
  596. ///
  597. /// Returns a signal which sends `next` for each value RACEvent, `error` for each
  598. /// error RACEvent, and `completed` for each completed RACEvent.
  599. - (RACSignal *)dematerialize RAC_WARN_UNUSED_RESULT;
  600. /// Inverts each NSNumber-wrapped BOOL sent by the receiver. It will assert if
  601. /// the receiver sends anything other than NSNumbers.
  602. ///
  603. /// Returns a signal of inverted NSNumber-wrapped BOOLs.
  604. - (RACSignal<NSNumber *> *)not RAC_WARN_UNUSED_RESULT;
  605. /// Performs a boolean AND on all of the RACTuple of NSNumbers in sent by the receiver.
  606. ///
  607. /// Asserts if the receiver sends anything other than a RACTuple of one or more NSNumbers.
  608. ///
  609. /// Returns a signal that applies AND to each NSNumber in the tuple.
  610. - (RACSignal<NSNumber *> *)and RAC_WARN_UNUSED_RESULT;
  611. /// Performs a boolean OR on all of the RACTuple of NSNumbers in sent by the receiver.
  612. ///
  613. /// Asserts if the receiver sends anything other than a RACTuple of one or more NSNumbers.
  614. ///
  615. /// Returns a signal that applies OR to each NSNumber in the tuple.
  616. - (RACSignal<NSNumber *> *)or RAC_WARN_UNUSED_RESULT;
  617. /// Sends the result of calling the block with arguments as packed in each RACTuple
  618. /// sent by the receiver.
  619. ///
  620. /// The receiver must send tuple values, where the first element of the tuple is
  621. /// a block, taking a number of parameters equal to the count of the remaining
  622. /// elements of the tuple, and returning an object. Each block must take at least
  623. /// one argument, so each tuple must contain at least 2 elements.
  624. ///
  625. /// Example:
  626. ///
  627. /// RACSignal *adder = [RACSignal return:^(NSNumber *a, NSNumber *b) {
  628. /// return @(a.intValue + b.intValue);
  629. /// }];
  630. /// RACSignal *sums = [[RACSignal
  631. /// combineLatest:@[ adder, as, bs ]]
  632. /// reduceApply];
  633. ///
  634. /// Returns a signal of the result of applying the first element of each tuple
  635. /// to the remaining elements.
  636. - (RACSignal *)reduceApply RAC_WARN_UNUSED_RESULT;
  637. @end
  638. NS_ASSUME_NONNULL_END