RACSignal+Operations.h 32 KB

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