-
Notifications
You must be signed in to change notification settings - Fork 429
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 withLatest(from:) (Rx.withLatestFrom) #128
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1021,6 +1021,77 @@ extension SignalProtocol { | |
.map { $0.0 } | ||
} | ||
|
||
/// Forward the latest value from `samplee` with the value from `self` as a | ||
/// tuple, only when `self` sends a `value` event. | ||
/// This is like a flipped version of `sample(with:)`, but `samplee`'s | ||
/// terminal events are completely ignored. | ||
/// | ||
/// - note: If `self` fires before a value has been observed on `samplee`, | ||
/// nothing happens. | ||
/// | ||
/// - parameters: | ||
/// - samplee: A signal that its latest value is sampled by `self`. | ||
/// | ||
/// - returns: A signal that will send values from `self` and `samplee`, | ||
/// sampled (possibly multiple times) by `self`, then terminate | ||
/// once `self` has terminated. **`samplee`'s terminated events | ||
/// are ignored**. | ||
public func withLatest<U>(from samplee: Signal<U, NoError>) -> Signal<(Value, U), Error> { | ||
return Signal { observer in | ||
let state = Atomic<U?>(nil) | ||
let disposable = CompositeDisposable() | ||
|
||
disposable += samplee.observeValues { value in | ||
state.value = value | ||
} | ||
|
||
disposable += self.observe { event in | ||
switch event { | ||
case let .value(value): | ||
if let value2 = state.value { | ||
observer.send(value: (value, value2)) | ||
} | ||
case .completed: | ||
observer.sendCompleted() | ||
case let .failed(error): | ||
observer.send(error: error) | ||
case .interrupted: | ||
observer.sendInterrupted() | ||
} | ||
} | ||
|
||
return disposable | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be implemented with
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To achieve the simplest
For comparison, in RxSwift, (As a side note, I have restricted |
||
} | ||
|
||
/// Forward the latest value from `samplee` with the value from `self` as a | ||
/// tuple, only when `self` sends a `value` event. | ||
/// This is like a flipped version of `sample(with:)`, but `samplee`'s | ||
/// terminal events are completely ignored. | ||
/// | ||
/// - note: If `self` fires before a value has been observed on `samplee`, | ||
/// nothing happens. | ||
/// | ||
/// - parameters: | ||
/// - samplee: A producer that its latest value is sampled by `self`. | ||
/// | ||
/// - returns: A signal that will send values from `self` and `samplee`, | ||
/// sampled (possibly multiple times) by `self`, then terminate | ||
/// once `self` has terminated. **`samplee`'s terminated events | ||
/// are ignored**. | ||
public func withLatest<U>(from samplee: SignalProducer<U, NoError>) -> Signal<(Value, U), Error> { | ||
return Signal { observer in | ||
let d = CompositeDisposable() | ||
samplee.startWithSignal { signal, disposable in | ||
d += disposable | ||
d += self.withLatest(from: signal).observe(observer) | ||
} | ||
return d | ||
} | ||
} | ||
} | ||
|
||
extension SignalProtocol { | ||
/// Forwards events from `self` until `lifetime` ends, at which point the | ||
/// returned signal will complete. | ||
/// | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would read better as "A signal whose latest value..."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 9580123.