Permalink
Browse files

Merge pull request #362 from ReactiveCocoa/execute

Added -execute:
  • Loading branch information...
2 parents 42836ad + 2171061 commit aaacf08bace2840170a60c59a9d58af8c32183a3 @jspahrsummers jspahrsummers committed Mar 9, 2013
@@ -21,6 +21,7 @@ extern const NSInteger RACSignalErrorTimedOut;
@class RACSequence;
@class RACSubject;
@class RACTuple;
+@class RACCommand;
@protocol RACSubscriber;
@interface RACSignal (Operations)
@@ -417,4 +418,15 @@ extern const NSInteger RACSignalErrorTimedOut;
// Returns a signal of inverted NSNumber-wrapped BOOLs.
- (RACSignal *)not;
+// Subscribes to the receiver and executes the command with each `next`.
+//
+// This can be useful when you want to execute a command based off a signal:
+//
+// [[textField.rac_textSignal throttle:0.3] executeCommand:searchCommand];
+//
+// command - The command to execute. Cannot be nil.
+//
+// Returns the disposable for the underlying subscription.
+- (RACDisposable *)executeCommand:(RACCommand *)command;
+
@end
@@ -25,6 +25,7 @@
#import "RACUnit.h"
#import "RACMulticastConnection+Private.h"
#import "RACReplaySubject.h"
+#import "RACCommand.h"
#import <libkern/OSAtomic.h>
#import <objc/runtime.h>
@@ -1366,4 +1367,12 @@ - (RACSignal *)not {
}] setNameWithFormat:@"[%@] -not", self.name];
}
+- (RACDisposable *)executeCommand:(RACCommand *)command {
+ NSParameterAssert(command != nil);
+
+ return [self subscribeNext:^(id x) {
+ [command execute:x];
+ }];
+}
+
@end
@@ -23,6 +23,7 @@
#import "RACTestObject.h"
#import "RACTuple.h"
#import "RACUnit.h"
+#import "RACCommand.h"
#define RACSignalTestError [NSError errorWithDomain:@"foo" code:100 userInfo:nil]
@@ -2451,4 +2452,19 @@
});
});
+describe(@"-executeCommand:", ^{
+ it(@"should execute the command with each next", ^{
+ RACCommand *command = [RACCommand command];
+
+ __block id value;
+ [command subscribeNext:^(id x) {
+ value = x;
+ }];
+
+ [[RACSignal return:@1] executeCommand:command];
+
+ expect(value).to.equal(@1);
+ });
+});
+
SpecEnd

0 comments on commit aaacf08

Please sign in to comment.