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

Associative lift operators to fix synchronous sampleOn #2571

Merged
merged 4 commits into from
Nov 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 35 additions & 5 deletions ReactiveCocoa/Swift/SignalProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,15 @@ extension SignalProducerType {
/// which may not be adviseable for some operators.
@warn_unused_result(message="Did you forget to call `start` on the producer?")
public func lift<U, F, V, G>(transform: Signal<Value, Error> -> Signal<U, F> -> Signal<V, G>) -> SignalProducer<U, F> -> SignalProducer<V, G> {
return liftRight(transform)
}

/// Right-associative lifting of a binary signal operator over producers. 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.
@warn_unused_result(message="Did you forget to call `start` on the producer?")
private func liftRight<U, F, V, G>(transform: Signal<Value, Error> -> Signal<U, F> -> Signal<V, G>) -> SignalProducer<U, F> -> SignalProducer<V, G> {
return { otherProducer in
return SignalProducer { observer, outerDisposable in
self.startWithSignal { signal, disposable in
Expand All @@ -409,6 +418,27 @@ extension SignalProducerType {
}
}

/// Left-associative lifting of a binary signal operator over producers. That
/// is, the receiver will be started before the argument producer. When both
/// producers are synchronous this order can be important depending on the operator
/// to generate correct results.
@warn_unused_result(message="Did you forget to call `start` on the producer?")
private func liftLeft<U, F, V, G>(transform: Signal<Value, Error> -> Signal<U, F> -> Signal<V, G>) -> SignalProducer<U, F> -> SignalProducer<V, G> {
return { otherProducer in
return SignalProducer { observer, outerDisposable in
otherProducer.startWithSignal { otherSignal, otherDisposable in
outerDisposable.addDisposable(otherDisposable)

self.startWithSignal { signal, disposable in
outerDisposable.addDisposable(disposable)

transform(signal)(otherSignal).observe(observer)
}
}
}
}
}

/// Lifts a binary Signal operator to operate upon a Signal and a SignalProducer instead.
///
/// In other words, this will create a new SignalProducer which will apply
Expand Down Expand Up @@ -473,7 +503,7 @@ extension SignalProducerType {
/// will also be interrupted.
@warn_unused_result(message="Did you forget to call `start` on the producer?")
public func combineLatestWith<U>(otherProducer: SignalProducer<U, Error>) -> SignalProducer<(Value, U), Error> {
return lift(Signal.combineLatestWith)(otherProducer)
return liftRight(Signal.combineLatestWith)(otherProducer)
}

/// Combines the latest value of the receiver with the latest value from
Expand Down Expand Up @@ -527,7 +557,7 @@ extension SignalProducerType {
/// completed, or interrupt if either input producer is interrupted.
@warn_unused_result(message="Did you forget to call `start` on the producer?")
public func sampleOn(sampler: SignalProducer<(), NoError>) -> SignalProducer<Value, Error> {
return lift(Signal.sampleOn)(sampler)
return liftLeft(Signal.sampleOn)(sampler)
}

/// Forwards the latest value from `self` whenever `sampler` sends a Next
Expand All @@ -548,7 +578,7 @@ extension SignalProducerType {
/// event, at which point the returned producer will complete.
@warn_unused_result(message="Did you forget to call `start` on the producer?")
public func takeUntil(trigger: SignalProducer<(), NoError>) -> SignalProducer<Value, Error> {
return lift(Signal.takeUntil)(trigger)
return liftRight(Signal.takeUntil)(trigger)
}

/// Forwards events from `self` until `trigger` sends a Next or Completed
Expand Down Expand Up @@ -606,7 +636,7 @@ extension SignalProducerType {
/// already.
@warn_unused_result(message="Did you forget to call `start` on the producer?")
public func takeUntilReplacement(replacement: SignalProducer<Value, Error>) -> SignalProducer<Value, Error> {
return lift(Signal.takeUntilReplacement)(replacement)
return liftRight(Signal.takeUntilReplacement)(replacement)
}

/// Forwards events from `self` until `replacement` begins sending events.
Expand Down Expand Up @@ -639,7 +669,7 @@ extension SignalProducerType {
/// are the Nth elements of the two input producers.
@warn_unused_result(message="Did you forget to call `start` on the producer?")
public func zipWith<U>(otherProducer: SignalProducer<U, Error>) -> SignalProducer<(Value, U), Error> {
return lift(Signal.zipWith)(otherProducer)
return liftRight(Signal.zipWith)(otherProducer)
}

/// Zips elements of this producer and a signal into pairs. The elements of
Expand Down
12 changes: 12 additions & 0 deletions ReactiveCocoaTests/Swift/SignalProducerLiftingSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,18 @@ class SignalProducerLiftingSpec: QuickSpec {
samplerObserver.sendCompleted()
expect(completed).to(beTruthy())
}

it("should emit an initial value if the sampler is a synchronous SignalProducer") {
let producer = SignalProducer<Int, NoError>(values: [1])
let sampler = SignalProducer<(), NoError>(value: ())

let result = producer.sampleOn(sampler)

var valueReceived: Int?
result.startWithNext { valueReceived = $0 }

expect(valueReceived) == 1
}
}

describe("combineLatestWith") {
Expand Down