Skip to content

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

Description

@acecilia

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
        }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions