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

Projects
None yet
3 participants
@acecilia
Copy link

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

This comment has been minimized.

Copy link
Owner

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

This comment has been minimized.

Copy link
Author

commented Jun 30, 2018

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

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

@artman

This comment has been minimized.

Copy link
Owner

commented Jul 5, 2018

For sure!

@spinach

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Author

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
You can’t perform that action at this time.