Git Product home page Git Product logo

Comments (5)

davidmorgan avatar davidmorgan commented on May 22, 2024

Thanks Alex.

I don't have suggestions for a good way to achieve this in Flutter; but what you're describing sounds along the right lines.

When I've done this in my own code--but not Flutter code--I've done it by adding IDs to the data classes, then having a global map from ID to instance. I've used a type Id<T> that does the lookup.

Maybe there is something already in Flutter that gives you an ID to instance mapping you could use.

Or, perhaps you could use the finalizeBuilder hook to maintain such a mapping. It gets calls just before a builder is called. It doesn't give you the final instance, but you can get that by calling build on the builder. (You'll need to guard the hook with a boolean check so that it's not already running, so it doesn't trigger itself).

from built_value.dart.

AlexanderJohr avatar AlexanderJohr commented on May 22, 2024

Thanks for clarifying!

Do you have an example of the Id<T> lookup approach?

from built_value.dart.

davidmorgan avatar davidmorgan commented on May 22, 2024

Nothing I can point to unfortunately. The idea is similar to your example, except:

  • Add type Id<T> which holds a numerical ID and has method T value that looks up the ID in a global map
  • Data classes all have an ID field; for new instances it gets assigned with a globally unique (incrementing) ID number
  • References between types that might introduce circles use an ID instead of containing the data, so in your example you would have an Id<Person> parent field

You might reasonably ask, why go to all the trouble of having immutable types only to introduce something like pointers back into the equation--since what the ID points to can change over time. The answer is, you can control updates to that global map in one place, e.g. accumulating then committing changes to it, rolling back if needed, etc. So you still have plenty of control.

from built_value.dart.

AlexanderJohr avatar AlexanderJohr commented on May 22, 2024

Thanks again! I will try that. It sounds promising, because like that I can create a ViewModel fairly easy by wrapping a data class inside a BehaviorSubject or a regular Stream. The view would only need to have a StreamBuilder, listening to that Stream. Nested ViewModels would have their own simple data class also wrapped inside BehaviorSubject or Stream. Instead of updating the whole tree, because one leaf node changes, I can just modify that one leaf node.

This resolves a lot of difficulties.

All but one:
I would need to garbage collect dead nodes myself, am I correct? A deeply nested built_value Model has at least the advantage, that deleting a node, deletes all child nodes with it.
Deleting a node which holds an ID to another node, does not delete the nested node automatically by looking up the ID in the map. I would need to put such an approach inside a destructor. But Dart does not have destructors. What would you advice?

from built_value.dart.

davidmorgan avatar davidmorgan commented on May 22, 2024

Yes, you will need something to find and remove dead nodes.

If all your nodes implement an interface that returns all the Ids they reference, then you could do that as a global step where you compute all "reachable" nodes then discard the rest. Just like a garbage collector :)

from built_value.dart.

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.