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

Pass observer to the callback closure, to avoid retain cycle or [weak self] boilerplate #59

Open
acecilia opened this issue Jun 29, 2018 · 5 comments

Comments

@acecilia
Copy link

acecilia commented Jun 29, 2018

Would it make sense to pass the observer as a parameter in the callback closure? Something like:

public func subscribe(with observer: ObserverType, callback: @escaping (ObserverType, T) -> T) -> Signals.SignalSubscription<ObserverType, T>

This will allow to convert this:

        signal.subscribe(with: self) { [weak self] (newValue) in
            guard let weakSelf = self else { return } // Boilerplate + easy to forget

            weakSelf.property = newValue
        }

Or this:

        signal.subscribe(with: self) { (newValue) in
            self.property = newValue // Retain cycle!!!!
        }

Into this:

        signal.subscribe(with: self) { (observer, newValue) in
            observer.property = newValue
        }

@artman
Copy link
Owner

artman commented Jun 30, 2018

This would work and some might prefer it, but I do like the bare bones closure approach. This would work even better if you could name observer as "self", overriding the instance reference complectly in the closure, so that one would not have any way of accidentally capturing the actual instance.

@acecilia
Copy link
Author

acecilia commented Jun 30, 2018

@artman rebinding of self is coming in Swift 4.2 I believe: swiftlang/swift#15306

Would you accept this functionality I mentioned in the library? If yes, I may invest some time and look into it.

@artman
Copy link
Owner

artman commented Jul 5, 2018

For sure!

@spinach
Copy link

spinach commented Mar 4, 2019

Hey there, first thanks a lot for this library, pretty neat and useful!

I'm wondering if there's any plans to implement passing the observer to the callback closure? Thanks!

@acecilia
Copy link
Author

acecilia commented Mar 4, 2019

It is not in my scope at the moment :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants