Skip to content

Commit

Permalink
Add -[RACSignal mergeWith:]
Browse files Browse the repository at this point in the history
  • Loading branch information
mdiep committed Mar 11, 2014
1 parent 3976d39 commit edae076
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ReactiveCocoaFramework/ReactiveCocoa/RACSignal+Operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ extern const NSInteger RACSignalErrorNoMatchingCase;
/// `reduceBlock`.
+ (RACSignal *)combineLatest:(id<NSFastEnumeration>)signals reduce:(id (^)())reduceBlock;

/// Returns a signal that merges the receiver and the given signal with `+merge:`.
- (RACSignal *)mergeWith:(RACSignal *)signal;

/// Sends the latest `next` from any of the signals.
///
/// Returns a signal that passes through values from each of the given signals,
Expand Down
6 changes: 6 additions & 0 deletions ReactiveCocoaFramework/ReactiveCocoa/RACSignal+Operations.m
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,12 @@ + (RACSignal *)combineLatest:(id<NSFastEnumeration>)signals reduce:(id (^)())red
return [result setNameWithFormat:@"+combineLatest: %@ reduce:", signals];
}

- (RACSignal *)mergeWith:(RACSignal *)signal {
return [[RACSignal
merge:@[ self, signal ]]
setNameWithFormat:@"[%@] -mergeWith: %@", self.name, signal];
}

+ (RACSignal *)merge:(id<NSFastEnumeration>)signals {
NSMutableArray *copiedSignals = [[NSMutableArray alloc] init];
for (RACSignal *signal in signals) {
Expand Down
26 changes: 26 additions & 0 deletions ReactiveCocoaFramework/ReactiveCocoaTests/RACSignalSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,32 @@
});
});

describe(@"-mergeWith:", ^{
__block RACSubject *sub1;
__block RACSubject *sub2;
__block RACSignal *merged;
beforeEach(^{
sub1 = [RACSubject subject];
sub2 = [RACSubject subject];
merged = [sub1 mergeWith:sub2];
});

it(@"should send all values from both signals", ^{
NSMutableArray *values = [NSMutableArray array];
[merged subscribeNext:^(id x) {
[values addObject:x];
}];

[sub1 sendNext:@1];
[sub2 sendNext:@2];
[sub2 sendNext:@3];
[sub1 sendNext:@4];

NSArray *expected = @[ @1, @2, @3, @4 ];
expect(values).to.equal(expected);
});
});

describe(@"+merge:", ^{
__block RACSubject *sub1;
__block RACSubject *sub2;
Expand Down

0 comments on commit edae076

Please sign in to comment.