You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It often happens when you need to inject a different kind of data as payload into signal chaining. Use cases:
1. Pass state on button tap:
letstate=Property("Some payload")letbutton=UIButton()
button.reactive.tap.map{ state.value }.bind(to:...)//Bind it to somewhere we need Signal<Data>
2. Pass object (usually it's self) as weak/unowned on button tap:
unowned
letbutton=UIButton()
button.reactive.tap.observeNext{[unowned self]inself.doSomething() //it's dangerous, as it can already be dead as we don't increase ARC counter :(
self.doSomething()}
weak
letbutton=UIButton()
button.reactive.tap.observeNext{[weak self]in
guard s =selfelse{return} // Using weak - we have to write this "guard" everywhere :(
s.doSomething() //but it's safe at least
s.doSomething()}
3. Pass strong reference to simple object constant
letdata="Some payload"letbutton=UIButton()
button.reactive.tap.map{ data }.bind(to:...) //Bind it to somewhere we need Signal<Data>
4. Pass latest data from signal:
letstate=Property("Some payload").map{ $0.length }letbutton=UIButton()
button.reactive.tap.with(latestFrom: state).bind(to:...)//Bind it to somewhere we need Signal<Data>
For the 1st and the last cases we can use with(latestFrom. The issues are:
2,3 cases are not covered.
It's hard to combine several payloads into one signal with(latestFrom:, combine: ).with(latestFrom:, combine: ).with(latestFrom:, combine:) demands providing more and more complicated function for tuple destructing (combine). And you need to write twice more code.
It has declares .latest, .val, .weak, .strong to be used for defenter use cases.
PS. I really stand for different implementation for .val and .latest as .latest keeps a strong reference to the latest payload, what causes side effects. .val - keeps a reference to Property instead. In general - they're equal, however.
The text was updated successfully, but these errors were encountered:
killev
changed the title
[Proposal]: Expand "with(latestFrom .." with better
[Proposal]: Expand "with(latestFrom .." with better (more convenient) implementation
Dec 7, 2018
It often happens when you need to inject a different kind of data as payload into signal chaining. Use cases:
1. Pass state on button tap:
2. Pass object (usually it's self) as weak/unowned on button tap:
unowned
weak
3. Pass strong reference to simple object constant
4. Pass latest data from signal:
For the 1st and the last cases we can use with(latestFrom. The issues are:
On my own project, I've implemented the function with like in this gist: https://gist.github.com/killev/6ea48dc8fdfd661472d50af42e4f9df2
so that it's possible to use it like below:
It has declares .latest, .val, .weak, .strong to be used for defenter use cases.
PS. I really stand for different implementation for .val and .latest as .latest keeps a strong reference to the latest payload, what causes side effects. .val - keeps a reference to Property instead. In general - they're equal, however.
The text was updated successfully, but these errors were encountered: