Giter Club home page Giter Club logo

Comments (5)

JensRavens avatar JensRavens commented on July 24, 2024 1

If I get it correctly you're implementing something that the library does anyway: Once an observable is dropped from memory, it releases all its observing functions (and other things that are retained by those). Calling unsubscribe is therefore not actually needed in almost all cases (so far I never used it myself, it was just integrated into the library because so many people asked for it - but basically you should never need it).

from interstellar.

JensRavens avatar JensRavens commented on July 24, 2024 1

Yes, this is the usecase that is handled by dispose-bags in other frameworks. I don't want to introduce this complicated concept into the library, that's why I'm looking for other ways.

Maybe it would make sense to change the implementation of ObserverToken. It was implemented in the first place as an opaque thing to store until we come up with a better implementation. Maybe it's time to change it to a struct now and implement the unsubscribe function directly on it.

from interstellar.

Mazyod avatar Mazyod commented on July 24, 2024

Type-erased version:

protocol AnyObservable: class {

    func unsubscribe(_ token: ObserverToken)
}

extension Observable: AnyObservable {

}

final class Subscription {

    private weak var observable: AnyObservable?
    private let token: ObserverToken

    init(observable: AnyObservable, token: ObserverToken) {
        self.observable = observable
        self.token = token
    }

    deinit {
        observable?.unsubscribe(token)
    }
}

extension ObserverToken {

    func subscription<T>(from observable: Observable<T>) -> Subscription {
        return Subscription(observable: observable, token: self)
    }
}

from interstellar.

Mazyod avatar Mazyod commented on July 24, 2024

Thanks for the explanation, @JensRavens. You're right, and I am not worried about the Observable being dropped from memory, nor about the Observer being dropped either (since I can capture weakly in the observation closure).

The problem I'm trying to solve is what if you want to bind, then re-bind objects without them ever being deallocated? Take a UITableViewCell as an example, since it's reused, and never deallocated.

final class Cell: UITableViewCell {

  func bind(observable: Observable<String>) {
    // Uh-oh .. need to unsubscribe from previous observable
    // I need to retain the observables somehow then...
    self.weakObservable.unsubscribe(self.token)
    self.weakObservable = observable
    self.token = observable.subscribe(...)
  }
}

You see how this burden of maintaining two properties, and doing the unsubscribed dance would be required in this case, since both objects are still in-memory?

from interstellar.

Mazyod avatar Mazyod commented on July 24, 2024

@JensRavens I'm happy to raise a PR with that idea in mind if you're busy. I presume the only additional change would be to retain the observable weakly within the token for the unsubscribe to work without the need to pass in any additional parameter.

from interstellar.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.