-
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 binding operator to Signal.Observer #635
Add binding operator to Signal.Observer #635
Conversation
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'm a fan, I've wanted this myself recently 👍
Sources/Observer.swift
Outdated
@@ -124,6 +125,12 @@ extension Signal { | |||
} | |||
} | |||
|
|||
extension Signal.Observer: BindingTargetProvider { | |||
public var bindingTarget: BindingTarget<Value> { | |||
return BindingTarget(lifetime: lifetime, action: send) |
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 is implicitly making a strong reference to self
here. Might need a weak reference to ensure the binding doesn't keep the observer alive?
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 am not quite convinced, because usually there is an owner whose lifetime the binding target can use.
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.
Are you saying another lifetime should be passed in when initialising the observer?
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.
Input Observer
s have to be owned by someone, and IMO it should be that someone who uses its own lifetime to create the binding target for the pipe.
We could have a convenience that takes a lifetime to create a binding target though.
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.
This strongly captures self
by the way, which goes against the contract of <~
.
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 reverted the commit that fixes that in order to un-hide the discussion that was going on. Will change it again if/when we decide how to proceed with this PR.
Makes sense. 👍 Can you add some tests and a changelog entry? |
@mdiep Will do, but there's still an ongoing discussion about which lifetime to use. |
0b72df0
to
75470fe
Compare
The (premature?) optimisation side of me has a little problem with this adding 16B overhead plus (at least) two objects per I would argue a majority of cases can already be satisfied by If this convenience is strongly desired, my proposed alternative would be a special overload for |
Sources/Observer.swift
Outdated
@@ -124,6 +125,12 @@ extension Signal { | |||
} | |||
} | |||
|
|||
extension Signal.Observer: BindingTargetProvider { | |||
public var bindingTarget: BindingTarget<Value> { | |||
return BindingTarget(lifetime: lifetime, action: send) |
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.
This strongly captures self
by the way, which goes against the contract of <~
.
I had two main motivations for this PR:
@andersio Here's another approach: extension Signal.Observer {
public static func <~
<Source: BindingSource>
(observer: Signal<Value, Error>.Observer, source: Source) -> Disposable?
where Source.Value == Value, Source.Error == NoError
{
return source.producer.start { [weak observer] in
switch $0 {
case .value(let val):
observer?.send(value: val)
default: break
}
}
}
} |
@Marcocanc I think the approach in #635 (comment) is fair. :) |
42695c4
to
60ab0ac
Compare
@andersio I have updated the PR with the proposed approach 😊 Sorry it took me so long |
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.
There's a conflict in the CHANGELOG now. Let's get that fixed and then we can merge. 👍
ee5026d
to
f1cb6f3
Compare
Thanks for the PR! Sorry this took so long. 🙈 |
As the title says, I think it would be nice to makeObserver
conform toBindingTargetProvider
.Any thoughts on that?
This PR adds the binding operator to
Signal.Observer
Checklist
Updated CHANGELOG.md.