Skip to content

Commit

Permalink
Joining empty list of producers fix (#774)
Browse files Browse the repository at this point in the history
* joining an empty list of producers returns a single event of the collected results, which is the empty array - this becomes relevant, when the list of producers is calculated and you are merging the resulting joined signal with some other signal. the end result will stall, if no event is sent from the joined producers.

* updated changelog

* no upstream sentinal is optional

* updated changelog

* typo

Co-authored-by: Nikolas Mayr <nikolas.mayr@rocket-internet.de>
  • Loading branch information
rocketnik and Nikolas Mayr committed May 22, 2020
1 parent 339c9b2 commit 6f308d6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# master
*Please add new entries at the top.*

1. Joining an empty sequence of producers can now send an event on the joined signal producer by providing the `noUpstreamSentinel` parameter. This becomes relevant, when the sequence of producers is calculated from some other Signal and the signal resulting from the joined producers is observed. If no event is sent only when the producers sequence is empty, then the observer gets stalled and e.g. the ui won't update.

# 6.2.1

1. Improved performance of joining signals by a factor of around 5. This enables joining of 1000 and more signals in a reasonable amount of time.
1. Fixed `SignalProducer.debounce` operator that, when started more than once, would not deliver values on producers started after the first time. (#772, kudos to @gpambrozio)
1. `FlattenStrategy.throttle` is introduced. (#713, kudos to @inamiy)
Expand All @@ -20,6 +23,7 @@
1. Add `<~` binding operator to `Signal.Observer` (#635, kudos to @Marcocanc)

# 6.0.0

1. Dropped support for Swift 4.2 (Xcode 9)
2. Removed dependency on https://github.com/antitypical/Result (#702, kudos to @NachoSoto and @mdiep)

Expand All @@ -30,6 +34,7 @@
* Replace all cases where `AnyError` was used in a `Signal` or `SignalProducer` with `Swift.Error`

# 5.0.1

1. Fix warnings in Xcode 10.2

# 5.0.0
Expand Down
8 changes: 4 additions & 4 deletions Sources/Property.swift
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,13 @@ extension PropertyProtocol {

/// Combines the values of all the given producers, in the manner described by
/// `combineLatest(with:)`. Returns nil if the sequence is empty.
public static func combineLatest<S: Sequence>(_ properties: S) -> Property<[S.Iterator.Element.Value]>? where S.Iterator.Element: PropertyProtocol {
public static func combineLatest<S: Sequence>(_ properties: S, noUpstreamSentinel: [S.Iterator.Element.Value]? = nil) -> Property<[S.Iterator.Element.Value]>? where S.Iterator.Element: PropertyProtocol {
let producers = properties.map { $0.producer }
guard !producers.isEmpty else {
return nil
}

return Property(unsafeProducer: SignalProducer.combineLatest(producers))
return Property(unsafeProducer: SignalProducer.combineLatest(producers, noUpstreamSentinel: noUpstreamSentinel))
}

/// Zips the values of all the given properties, in the manner described by
Expand Down Expand Up @@ -383,13 +383,13 @@ extension PropertyProtocol {

/// Zips the values of all the given properties, in the manner described by
/// `zip(with:)`. Returns nil if the sequence is empty.
public static func zip<S: Sequence>(_ properties: S) -> Property<[S.Iterator.Element.Value]>? where S.Iterator.Element: PropertyProtocol {
public static func zip<S: Sequence>(_ properties: S, noUpstreamSentinel: [S.Iterator.Element.Value]? = nil) -> Property<[S.Iterator.Element.Value]>? where S.Iterator.Element: PropertyProtocol {
let producers = properties.map { $0.producer }
guard !producers.isEmpty else {
return nil
}

return Property(unsafeProducer: SignalProducer.zip(producers))
return Property(unsafeProducer: SignalProducer.zip(producers, noUpstreamSentinel: noUpstreamSentinel))
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Signal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1680,7 +1680,7 @@ extension Signal {
return true
}

_haveAllSentInitial = values.allSatisfy{ !($0 is Placeholder) }
_haveAllSentInitial = values.allSatisfy { !($0 is Placeholder) }
return _haveAllSentInitial
}
}
Expand Down
17 changes: 10 additions & 7 deletions Sources/SignalProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2098,8 +2098,8 @@ extension SignalProducer {

/// Combines the values of all the given producers, in the manner described by
/// `combineLatest(with:)`. Will return an empty `SignalProducer` if the sequence is empty.
public static func combineLatest<S: Sequence>(_ producers: S) -> SignalProducer<[Value], Error> where S.Iterator.Element: SignalProducerConvertible, S.Iterator.Element.Value == Value, S.Iterator.Element.Error == Error {
return start(producers, Signal.combineLatest)
public static func combineLatest<S: Sequence>(_ producers: S, noUpstreamSentinel: [S.Iterator.Element.Value]? = nil) -> SignalProducer<[Value], Error> where S.Iterator.Element: SignalProducerConvertible, S.Iterator.Element.Value == Value, S.Iterator.Element.Error == Error {
return start(producers, noUpstreamSentinel: noUpstreamSentinel, Signal.combineLatest)
}

/// Zips the values of all the given producers, in the manner described by
Expand Down Expand Up @@ -2176,18 +2176,21 @@ extension SignalProducer {

/// Zips the values of all the given producers, in the manner described by
/// `zipWith`. Will return an empty `SignalProducer` if the sequence is empty.
public static func zip<S: Sequence>(_ producers: S) -> SignalProducer<[Value], Error> where S.Iterator.Element: SignalProducerConvertible, S.Iterator.Element.Value == Value, S.Iterator.Element.Error == Error {
return start(producers, Signal.zip)
public static func zip<S: Sequence>(_ producers: S, noUpstreamSentinel: [S.Iterator.Element.Value]? = nil) -> SignalProducer<[Value], Error> where S.Iterator.Element: SignalProducerConvertible, S.Iterator.Element.Value == Value, S.Iterator.Element.Error == Error {
return start(producers, noUpstreamSentinel: noUpstreamSentinel, Signal.zip)
}

private static func start<S: Sequence>(_ producers: S, _ transform: @escaping (AnySequence<Signal<Value, Error>>) -> Signal<[Value], Error>) -> SignalProducer<[Value], Error> where S.Iterator.Element: SignalProducerConvertible, S.Iterator.Element.Value == Value, S.Iterator.Element.Error == Error
private static func start<S: Sequence>(_ producers: S, noUpstreamSentinel: [S.Iterator.Element.Value]?, _ transform: @escaping (AnySequence<Signal<Value, Error>>) -> Signal<[Value], Error>) -> SignalProducer<[Value], Error> where S.Iterator.Element: SignalProducerConvertible, S.Iterator.Element.Value == Value, S.Iterator.Element.Error == Error
{
return SignalProducer<[Value], Error> { observer, lifetime in
let setup = producers.map {
(producer: $0.producer, pipe: Signal<Value, Error>.pipe())
}

guard !setup.isEmpty else {
if let noUpstreamSentinel = noUpstreamSentinel {
observer.send(value: noUpstreamSentinel)
}
observer.sendCompleted()
return
}
Expand Down Expand Up @@ -2717,7 +2720,7 @@ extension SignalProducer where Value == Bool {
///
/// - returns: A producer that emits the logical AND results.
public static func all<BooleansCollection: Collection>(_ booleans: BooleansCollection) -> SignalProducer<Value, Error> where BooleansCollection.Element == SignalProducer<Value, Error> {
return combineLatest(booleans).map { $0.reduce(true) { $0 && $1 } }
return combineLatest(booleans, noUpstreamSentinel: []).map { $0.reduce(true) { $0 && $1 } }
}

/// Create a producer that computes a logical AND between the latest values of `booleans`.
Expand Down Expand Up @@ -2759,7 +2762,7 @@ extension SignalProducer where Value == Bool {
///
/// - returns: A producer that emits the logical OR results.
public static func any<BooleansCollection: Collection>(_ booleans: BooleansCollection) -> SignalProducer<Value, Error> where BooleansCollection.Element == SignalProducer<Value, Error> {
return combineLatest(booleans).map { $0.reduce(false) { $0 || $1 } }
return combineLatest(booleans, noUpstreamSentinel: []).map { $0.reduce(false) { $0 || $1 } }
}

/// Create a producer that computes a logical OR between the latest values of `booleans`.
Expand Down

0 comments on commit 6f308d6

Please sign in to comment.