PICyourBrain
PICyourBrain

Reputation: 10284

ReactiveCocoa merge that completes when input signal completes

In ReactiveCocoa, is there a mechanism similar to merge: that completes when any of the signals that are being merged complete?

I found a workaround that involves concatenating the input signal with a [RACSignal return:foo] and then adding a take:1 after the merge, but that seems rather long-winded. Is there a simpler way?

Upvotes: 0

Views: 120

Answers (1)

Ian Henry
Ian Henry

Reputation: 22403

Not built-in to ReactiveCoca. This is probably something you should define in a helper category on RACSignal, so that any long-windedness is hidden behind a nice method abstraction.

Here's an (untested) example using materialize, which will give you a signal of signal events so you don't need to append anything onto your input signals:

+ (RACSignal *)sheepishMerge:(NSArray *)signals {
    RACSequence *completions = [signals.rac_sequence map:^(RACSignal *signal) {
        return [[signal materialize] filter:^(RACEvent *event) {
            return event.eventType == RACEventTypeCompleted;
        }];
    }];

    RACSignal *firstCompletion = [[RACSignal merge:completions] take:1];

    return [[RACSignal merge:signals] takeUntil:firstCompletion];
}

Upvotes: 4

Related Questions