Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add merge(with:) operator #600

Merged
merged 7 commits into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# master
*Please add new entries at the top.*

1. New operator `merge(with:)` (#600, kudos to @ra1028)

# 3.1.0
1. Fixed `schedule(after:interval:leeway:)` being cancelled when the returned `Disposable` is not retained. (#584, kudos to @jjoelson)

Expand Down
30 changes: 8 additions & 22 deletions Sources/Flatten.swift
Original file line number Diff line number Diff line change
Expand Up @@ -481,46 +481,32 @@ extension UnsafeAtomicState where State == ProducerState {
}

extension Signal {
/// Merges the given signals into a single `Signal` that will emit all
/// values from each of them, and complete when all of them have completed.
///
/// - parameters:
/// - signals: A sequence of signals to merge.
/// Merges the values of all the given signals, in the manner described
/// by `merge(with:)`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally I wouldn't mind this, but Xcode does not make it easy to find this documentation, so I'd keep it duplicated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I was according to zip, combineLatest operator, though should revert?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should duplicate it in those too IMO (at least until SourceKit & CMD+Click actually works consistently in Xcode). For now can you keep these how they were?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotcha, will revert them:+1:
Write the doc for zip and combineLatest, will too much lines.
I think there are pros and cons, so not to write now.

public static func merge<Seq: Sequence>(_ signals: Seq) -> Signal<Value, Error> where Seq.Iterator.Element == Signal<Value, Error>
{
return SignalProducer<Signal<Value, Error>, Error>(signals)
.flatten(.merge)
.startAndRetrieveSignal()
}

/// Merges the given signals into a single `Signal` that will emit all
/// values from each of them, and complete when all of them have completed.
///
/// - parameters:
/// - signals: A list of signals to merge.
/// Merges the values of all the given signals, in the manner described
/// by `merge(with:)`.
public static func merge(_ signals: Signal<Value, Error>...) -> Signal<Value, Error> {
return Signal.merge(signals)
}
}

extension SignalProducer {
/// Merges the given producers into a single `SignalProducer` that will emit
/// all values from each of them, and complete when all of them have
/// completed.
///
/// - parameters:
/// - producers: A sequence of producers to merge.
/// Merges the values of all the given producers, in the manner described
/// by `merge(with:)`.
public static func merge<Seq: Sequence>(_ producers: Seq) -> SignalProducer<Value, Error> where Seq.Iterator.Element == SignalProducer<Value, Error>
{
return SignalProducer<Seq.Iterator.Element, NoError>(producers).flatten(.merge)
}

/// Merges the given producers into a single `SignalProducer` that will emit
/// all values from each of them, and complete when all of them have
/// completed.
///
/// - parameters:
/// - producers: A sequence of producers to merge.
/// Merges the values of all the given producers, in the manner described
/// by `merge(with:)`.
public static func merge(_ producers: SignalProducer<Value, Error>...) -> SignalProducer<Value, Error> {
return SignalProducer.merge(producers)
}
Expand Down
11 changes: 11 additions & 0 deletions Sources/Signal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,17 @@ extension Signal {
public func combineLatest<U>(with other: Signal<U, Error>) -> Signal<(Value, U), Error> {
return Signal.combineLatest(self, other)
}

/// Merge the given signal into a single `Signal` that will emit all
/// values from both of them, and complete when all of them have completed.
///
/// - parameters:
/// - other: A signal to merge `self`'s value with.
///
/// - returns: A signal that sends all values of `self` and given signal.
public func merge(with other: Signal<Value, Error>) -> Signal<Value, Error> {
return Signal.merge(self, other)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we still ought to have a test for this (and the SignalProducer variant below). Would you mind adding one?


/// Delay `value` and `completed` events by the given interval, forwarding
/// them on the given scheduler.
Expand Down
11 changes: 11 additions & 0 deletions Sources/SignalProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,17 @@ extension SignalProducer {
public func combineLatest<Other: SignalProducerConvertible>(with other: Other) -> SignalProducer<(Value, Other.Value), Error> where Other.Error == Error {
return SignalProducer.combineLatest(self, other)
}

/// Merge the given producer into a single `SignalProducer` that will emit all
/// values from both of them, and complete when all of them have completed.
///
/// - parameters:
/// - other: A producer to merge `self`'s value with.
///
/// - returns: A producer that sends all values of `self` and given producer.
public func merge(with other: SignalProducer<Value, Error>) -> SignalProducer<Value, Error> {
return SignalProducer.merge(self, other)
}

/// Delay `value` and `completed` events by the given interval, forwarding
/// them on the given scheduler.
Expand Down