Skip to content

Commit

Permalink
Addressed comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
andersio committed May 11, 2017
1 parent efd0d87 commit c9fc25f
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 75 deletions.
30 changes: 19 additions & 11 deletions Sources/Signal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1941,22 +1941,22 @@ extension Signal {
private var completionCount: Int
private let action: (ContiguousArray<Any>) -> Void

private var _hasAllSentInitial: Bool
private var hasAllSentInitial: Bool {
private var _haveAllSentInitial: Bool
private var haveAllSentInitial: Bool {
mutating get {
if _hasAllSentInitial {
if _haveAllSentInitial {
return true
}

_hasAllSentInitial = values.reduce(true) { $0 && !($1 is Placeholder) }
return _hasAllSentInitial
_haveAllSentInitial = values.reduce(true) { $0 && !($1 is Placeholder) }
return _haveAllSentInitial
}
}

mutating func update(_ value: Any, at position: Int) -> Bool {
values[position] = value

if hasAllSentInitial {
if haveAllSentInitial {
action(values)
}

Expand All @@ -1971,7 +1971,7 @@ extension Signal {
init(count: Int, action: @escaping (ContiguousArray<Any>) -> Void) {
values = ContiguousArray(repeating: Placeholder.none, count: count)
completionCount = 0
_hasAllSentInitial = false
_haveAllSentInitial = false
self.action = action
}
}
Expand All @@ -1981,9 +1981,17 @@ extension Signal {
private var isCompleted: ContiguousArray<Bool>
private let action: (ContiguousArray<Any>) -> Void

private var hasCompletedAndEmptiedSignal: Bool { return Swift.zip(values, isCompleted).contains(where: { $0.isEmpty && $1 }) }
private var canEmit: Bool { return values.reduce(true) { $0 && !$1.isEmpty } }
private var isAllCompleted: Bool { return isCompleted.reduce(true) { $0 && $1 } }
private var hasCompletedAndEmptiedSignal: Bool {
return Swift.zip(values, isCompleted).contains(where: { $0.isEmpty && $1 })
}

private var canEmit: Bool {
return values.reduce(true) { $0 && !$1.isEmpty }
}

private var areAllCompleted: Bool {
return isCompleted.reduce(true) { $0 && $1 }
}

mutating func update(_ value: Any, at position: Int) -> Bool {
values[position].append(value)
Expand Down Expand Up @@ -2011,7 +2019,7 @@ extension Signal {

// `zip` completes when all signals has completed, or any of the signals
// has completed without any buffered value.
return hasCompletedAndEmptiedSignal || isAllCompleted
return hasCompletedAndEmptiedSignal || areAllCompleted
}

init(count: Int, action: @escaping (ContiguousArray<Any>) -> Void) {
Expand Down
197 changes: 133 additions & 64 deletions Sources/SignalProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,19 @@ extension SignalProducer {

return result
}

/// Create a `Signal` from `self` in the manner described by `startWithSignal`, and
/// put the interrupt handle into the given `CompositeDisposable`.
///
/// - parameters:
/// - disposable: The `CompositeDisposable` the interrupt handle to be added to.
/// - setup: A closure that accepts the produced `Signal`.
fileprivate func startWithSignal(interruptingBy disposable: CompositeDisposable, setup: (Signal<Value, Error>) -> Void) {
startWithSignal { signal, interruptHandle in
disposable += interruptHandle
setup(signal)
}
}
}

extension SignalProducer where Error == NoError {
Expand Down Expand Up @@ -384,7 +397,7 @@ extension SignalProducer {
/// That is, the argument producer will be started before the receiver. When
/// both producers are synchronous this order can be important depending on
/// the operator to generate correct results.
private func liftRight<U, F, V, G>(_ transform: @escaping (Signal<Value, Error>) -> (Signal<U, F>) -> Signal<V, G>) -> (SignalProducer<U, F>) -> SignalProducer<V, G> {
fileprivate func liftRight<U, F, V, G>(_ transform: @escaping (Signal<Value, Error>) -> (Signal<U, F>) -> Signal<V, G>) -> (SignalProducer<U, F>) -> SignalProducer<V, G> {
return { otherProducer in
return SignalProducer<V, G> { observer, outerDisposable in
self.startWithSignal { signal, disposable in
Expand Down Expand Up @@ -438,7 +451,126 @@ extension SignalProducer {
return self.liftRight(transform)(SignalProducer<U, F>(otherSignal))
}
}
}

/// Create `Signal`s from the given producers, and start the producers in the argument
/// order.
///
/// - parameters:
/// - disposable: The `CompositeDisposable` to collect the interrupt handles of all
/// produced `Signal`s.
/// - start: The closure to accept all produced `Signal`s at once.
private func flattenStart<A, B, Error>(_ disposable: CompositeDisposable, _ a: SignalProducer<A, Error>, _ b: SignalProducer<B, Error>, _ start: (Signal<A, Error>, Signal<B, Error>) -> Void) {
b.startWithSignal(interruptingBy: disposable) { b in
a.startWithSignal(interruptingBy: disposable) { start($0, b) }
}
}

/// Create `Signal`s from the given producers, and start the producers in the argument
/// order.
///
/// - parameters:
/// - disposable: The `CompositeDisposable` to collect the interrupt handles of all
/// produced `Signal`s.
/// - start: The closure to accept all produced `Signal`s at once.
private func flattenStart<A, B, C, Error>(_ disposable: CompositeDisposable, _ a: SignalProducer<A, Error>, _ b: SignalProducer<B, Error>, _ c: SignalProducer<C, Error>, _ start: (Signal<A, Error>, Signal<B, Error>, Signal<C, Error>) -> Void) {
c.startWithSignal(interruptingBy: disposable) { c in
flattenStart(disposable, a, b) { start($0, $1, c) }
}
}

/// Create `Signal`s from the given producers, and start the producers in the argument
/// order.
///
/// - parameters:
/// - disposable: The `CompositeDisposable` to collect the interrupt handles of all
/// produced `Signal`s.
/// - start: The closure to accept all produced `Signal`s at once.
private func flattenStart<A, B, C, D, Error>(_ disposable: CompositeDisposable, _ a: SignalProducer<A, Error>, _ b: SignalProducer<B, Error>, _ c: SignalProducer<C, Error>, _ d: SignalProducer<D, Error>, _ start: (Signal<A, Error>, Signal<B, Error>, Signal<C, Error>, Signal<D, Error>) -> Void) {
d.startWithSignal(interruptingBy: disposable) { d in
flattenStart(disposable, a, b, c) { start($0, $1, $2, d) }
}
}

/// Create `Signal`s from the given producers, and start the producers in the argument
/// order.
///
/// - parameters:
/// - disposable: The `CompositeDisposable` to collect the interrupt handles of all
/// produced `Signal`s.
/// - start: The closure to accept all produced `Signal`s at once.
private func flattenStart<A, B, C, D, E, Error>(_ disposable: CompositeDisposable, _ a: SignalProducer<A, Error>, _ b: SignalProducer<B, Error>, _ c: SignalProducer<C, Error>, _ d: SignalProducer<D, Error>, _ e: SignalProducer<E, Error>, _ start: (Signal<A, Error>, Signal<B, Error>, Signal<C, Error>, Signal<D, Error>, Signal<E, Error>) -> Void) {
e.startWithSignal(interruptingBy: disposable) { e in
flattenStart(disposable, a, b, c, d) { start($0, $1, $2, $3, e) }
}
}

/// Create `Signal`s from the given producers, and start the producers in the argument
/// order.
///
/// - parameters:
/// - disposable: The `CompositeDisposable` to collect the interrupt handles of all
/// produced `Signal`s.
/// - start: The closure to accept all produced `Signal`s at once.
private func flattenStart<A, B, C, D, E, F, Error>(_ disposable: CompositeDisposable, _ a: SignalProducer<A, Error>, _ b: SignalProducer<B, Error>, _ c: SignalProducer<C, Error>, _ d: SignalProducer<D, Error>, _ e: SignalProducer<E, Error>, _ f: SignalProducer<F, Error>, _ start: (Signal<A, Error>, Signal<B, Error>, Signal<C, Error>, Signal<D, Error>, Signal<E, Error>, Signal<F, Error>) -> Void) {
f.startWithSignal(interruptingBy: disposable) { f in
flattenStart(disposable, a, b, c, d, e) { start($0, $1, $2, $3, $4, f) }
}
}

/// Create `Signal`s from the given producers, and start the producers in the argument
/// order.
///
/// - parameters:
/// - disposable: The `CompositeDisposable` to collect the interrupt handles of all
/// produced `Signal`s.
/// - start: The closure to accept all produced `Signal`s at once.
private func flattenStart<A, B, C, D, E, F, G, Error>(_ disposable: CompositeDisposable, _ a: SignalProducer<A, Error>, _ b: SignalProducer<B, Error>, _ c: SignalProducer<C, Error>, _ d: SignalProducer<D, Error>, _ e: SignalProducer<E, Error>, _ f: SignalProducer<F, Error>, _ g: SignalProducer<G, Error>, _ start: (Signal<A, Error>, Signal<B, Error>, Signal<C, Error>, Signal<D, Error>, Signal<E, Error>, Signal<F, Error>, Signal<G, Error>) -> Void) {
g.startWithSignal(interruptingBy: disposable) { g in
flattenStart(disposable, a, b, c, d, e, f) { start($0, $1, $2, $3, $4, $5, g) }
}
}

/// Create `Signal`s from the given producers, and start the producers in the argument
/// order.
///
/// - parameters:
/// - disposable: The `CompositeDisposable` to collect the interrupt handles of all
/// produced `Signal`s.
/// - start: The closure to accept all produced `Signal`s at once.
private func flattenStart<A, B, C, D, E, F, G, H, Error>(_ disposable: CompositeDisposable, _ a: SignalProducer<A, Error>, _ b: SignalProducer<B, Error>, _ c: SignalProducer<C, Error>, _ d: SignalProducer<D, Error>, _ e: SignalProducer<E, Error>, _ f: SignalProducer<F, Error>, _ g: SignalProducer<G, Error>, _ h: SignalProducer<H, Error>, _ start: (Signal<A, Error>, Signal<B, Error>, Signal<C, Error>, Signal<D, Error>, Signal<E, Error>, Signal<F, Error>, Signal<G, Error>, Signal<H, Error>) -> Void) {
h.startWithSignal(interruptingBy: disposable) { h in
flattenStart(disposable, a, b, c, d, e, f, g) { start($0, $1, $2, $3, $4, $5, $6, h) }
}
}

/// Create `Signal`s from the given producers, and start the producers in the argument
/// order.
///
/// - parameters:
/// - disposable: The `CompositeDisposable` to collect the interrupt handles of all
/// produced `Signal`s.
/// - start: The closure to accept all produced `Signal`s at once.
private func flattenStart<A, B, C, D, E, F, G, H, I, Error>(_ disposable: CompositeDisposable, _ a: SignalProducer<A, Error>, _ b: SignalProducer<B, Error>, _ c: SignalProducer<C, Error>, _ d: SignalProducer<D, Error>, _ e: SignalProducer<E, Error>, _ f: SignalProducer<F, Error>, _ g: SignalProducer<G, Error>, _ h: SignalProducer<H, Error>, _ i: SignalProducer<I, Error>, _ start: (Signal<A, Error>, Signal<B, Error>, Signal<C, Error>, Signal<D, Error>, Signal<E, Error>, Signal<F, Error>, Signal<G, Error>, Signal<H, Error>, Signal<I, Error>) -> Void) {
i.startWithSignal(interruptingBy: disposable) { i in
flattenStart(disposable, a, b, c, d, e, f, g, h) { start($0, $1, $2, $3, $4, $5, $6, $7, i) }
}
}

/// Create `Signal`s from the given producers, and start the producers in the argument
/// order.
///
/// - parameters:
/// - disposable: The `CompositeDisposable` to collect the interrupt handles of all
/// produced `Signal`s.
/// - start: The closure to accept all produced `Signal`s at once.
private func flattenStart<A, B, C, D, E, F, G, H, I, J, Error>(_ disposable: CompositeDisposable, _ a: SignalProducer<A, Error>, _ b: SignalProducer<B, Error>, _ c: SignalProducer<C, Error>, _ d: SignalProducer<D, Error>, _ e: SignalProducer<E, Error>, _ f: SignalProducer<F, Error>, _ g: SignalProducer<G, Error>, _ h: SignalProducer<H, Error>, _ i: SignalProducer<I, Error>, _ j: SignalProducer<J, Error>, _ start: (Signal<A, Error>, Signal<B, Error>, Signal<C, Error>, Signal<D, Error>, Signal<E, Error>, Signal<F, Error>, Signal<G, Error>, Signal<H, Error>, Signal<I, Error>, Signal<J, Error>) -> Void) {
j.startWithSignal(interruptingBy: disposable) { j in
flattenStart(disposable, a, b, c, d, e, f, g, h, i) { start($0, $1, $2, $3, $4, $5, $6, $7, $8, j) }
}
}

extension SignalProducer {
/// Map each value in the producer to a new value.
///
/// - parameters:
Expand Down Expand Up @@ -2276,66 +2408,3 @@ extension SignalProducer where Value == Date, Error == NoError {
}
}
}

extension SignalProducer {
fileprivate func startWithSignal(interruptingBy disposable: CompositeDisposable, setup: (Signal<Value, Error>) -> Void) {
startWithSignal { signal, interruptHandle in
disposable += interruptHandle
setup(signal)
}
}
}

private func flattenStart<A, B, Error>(_ disposable: CompositeDisposable, _ a: SignalProducer<A, Error>, _ b: SignalProducer<B, Error>, _ start: (Signal<A, Error>, Signal<B, Error>) -> Void) {
b.startWithSignal(interruptingBy: disposable) { b in
a.startWithSignal(interruptingBy: disposable) { start($0, b) }
}
}

private func flattenStart<A, B, C, Error>(_ disposable: CompositeDisposable, _ a: SignalProducer<A, Error>, _ b: SignalProducer<B, Error>, _ c: SignalProducer<C, Error>, _ start: (Signal<A, Error>, Signal<B, Error>, Signal<C, Error>) -> Void) {
c.startWithSignal(interruptingBy: disposable) { c in
flattenStart(disposable, a, b) { start($0, $1, c) }
}
}

private func flattenStart<A, B, C, D, Error>(_ disposable: CompositeDisposable, _ a: SignalProducer<A, Error>, _ b: SignalProducer<B, Error>, _ c: SignalProducer<C, Error>, _ d: SignalProducer<D, Error>, _ start: (Signal<A, Error>, Signal<B, Error>, Signal<C, Error>, Signal<D, Error>) -> Void) {
d.startWithSignal(interruptingBy: disposable) { d in
flattenStart(disposable, a, b, c) { start($0, $1, $2, d) }
}
}

private func flattenStart<A, B, C, D, E, Error>(_ disposable: CompositeDisposable, _ a: SignalProducer<A, Error>, _ b: SignalProducer<B, Error>, _ c: SignalProducer<C, Error>, _ d: SignalProducer<D, Error>, _ e: SignalProducer<E, Error>, _ start: (Signal<A, Error>, Signal<B, Error>, Signal<C, Error>, Signal<D, Error>, Signal<E, Error>) -> Void) {
e.startWithSignal(interruptingBy: disposable) { e in
flattenStart(disposable, a, b, c, d) { start($0, $1, $2, $3, e) }
}
}

private func flattenStart<A, B, C, D, E, F, Error>(_ disposable: CompositeDisposable, _ a: SignalProducer<A, Error>, _ b: SignalProducer<B, Error>, _ c: SignalProducer<C, Error>, _ d: SignalProducer<D, Error>, _ e: SignalProducer<E, Error>, _ f: SignalProducer<F, Error>, _ start: (Signal<A, Error>, Signal<B, Error>, Signal<C, Error>, Signal<D, Error>, Signal<E, Error>, Signal<F, Error>) -> Void) {
f.startWithSignal(interruptingBy: disposable) { f in
flattenStart(disposable, a, b, c, d, e) { start($0, $1, $2, $3, $4, f) }
}
}

private func flattenStart<A, B, C, D, E, F, G, Error>(_ disposable: CompositeDisposable, _ a: SignalProducer<A, Error>, _ b: SignalProducer<B, Error>, _ c: SignalProducer<C, Error>, _ d: SignalProducer<D, Error>, _ e: SignalProducer<E, Error>, _ f: SignalProducer<F, Error>, _ g: SignalProducer<G, Error>, _ start: (Signal<A, Error>, Signal<B, Error>, Signal<C, Error>, Signal<D, Error>, Signal<E, Error>, Signal<F, Error>, Signal<G, Error>) -> Void) {
g.startWithSignal(interruptingBy: disposable) { g in
flattenStart(disposable, a, b, c, d, e, f) { start($0, $1, $2, $3, $4, $5, g) }
}
}

private func flattenStart<A, B, C, D, E, F, G, H, Error>(_ disposable: CompositeDisposable, _ a: SignalProducer<A, Error>, _ b: SignalProducer<B, Error>, _ c: SignalProducer<C, Error>, _ d: SignalProducer<D, Error>, _ e: SignalProducer<E, Error>, _ f: SignalProducer<F, Error>, _ g: SignalProducer<G, Error>, _ h: SignalProducer<H, Error>, _ start: (Signal<A, Error>, Signal<B, Error>, Signal<C, Error>, Signal<D, Error>, Signal<E, Error>, Signal<F, Error>, Signal<G, Error>, Signal<H, Error>) -> Void) {
h.startWithSignal(interruptingBy: disposable) { h in
flattenStart(disposable, a, b, c, d, e, f, g) { start($0, $1, $2, $3, $4, $5, $6, h) }
}
}

private func flattenStart<A, B, C, D, E, F, G, H, I, Error>(_ disposable: CompositeDisposable, _ a: SignalProducer<A, Error>, _ b: SignalProducer<B, Error>, _ c: SignalProducer<C, Error>, _ d: SignalProducer<D, Error>, _ e: SignalProducer<E, Error>, _ f: SignalProducer<F, Error>, _ g: SignalProducer<G, Error>, _ h: SignalProducer<H, Error>, _ i: SignalProducer<I, Error>, _ start: (Signal<A, Error>, Signal<B, Error>, Signal<C, Error>, Signal<D, Error>, Signal<E, Error>, Signal<F, Error>, Signal<G, Error>, Signal<H, Error>, Signal<I, Error>) -> Void) {
i.startWithSignal(interruptingBy: disposable) { i in
flattenStart(disposable, a, b, c, d, e, f, g, h) { start($0, $1, $2, $3, $4, $5, $6, $7, i) }
}
}

private func flattenStart<A, B, C, D, E, F, G, H, I, J, Error>(_ disposable: CompositeDisposable, _ a: SignalProducer<A, Error>, _ b: SignalProducer<B, Error>, _ c: SignalProducer<C, Error>, _ d: SignalProducer<D, Error>, _ e: SignalProducer<E, Error>, _ f: SignalProducer<F, Error>, _ g: SignalProducer<G, Error>, _ h: SignalProducer<H, Error>, _ i: SignalProducer<I, Error>, _ j: SignalProducer<J, Error>, _ start: (Signal<A, Error>, Signal<B, Error>, Signal<C, Error>, Signal<D, Error>, Signal<E, Error>, Signal<F, Error>, Signal<G, Error>, Signal<H, Error>, Signal<I, Error>, Signal<J, Error>) -> Void) {
j.startWithSignal(interruptingBy: disposable) { j in
flattenStart(disposable, a, b, c, d, e, f, g, h, i) { start($0, $1, $2, $3, $4, $5, $6, $7, $8, j) }
}
}

0 comments on commit c9fc25f

Please sign in to comment.