graphistry / falcor

Graphistry forks of the FalcorJS family of projects in a lerna-powered mono-repo.
23 stars 2 forks source link

A way to get an object without metadata? #6

Closed drFabio closed 7 years ago

drFabio commented 7 years ago

I saw that doing a model.get() returns a json object with f_meta as a key. Is there a way to get a pure object like plain netflix falcor ?

Thanks

jameslaneconkling commented 7 years ago

Not sure what your usecase is, but if it's for testing or development or you're fine w/ a not super elegant solution, JSON.parse(JSONGraph.toString()) will return the JSONGraph object w/o metadata. I use this in testing so I can do deepEqual comparisons on a response w/o having to specify the metadata keys in my expected response.

trxcllnt commented 7 years ago

@drFabio @jameslaneconkling is right. You should also be able to just call the toJSON() method, which will deep-clone the JSON output down to the leaf nodes (which won't be cloned).

Alternately, I could put the metadata one level up on the prototype, which would hide it from serialization. This would be slower (2 allocations per branch node instead of 1) per get, but it is the approach I've taken in the sister cache-walk that recycle's the JSON across get requests. Because the recycled JSON cache walk compares versions and hash codes to determine whether it needs to continue the walk, we have quite a bit more breathing room to do things like this.

drFabio commented 7 years ago

Well my need at that time was that I was using the object keys to dynamically display some stuff and do some React comparisons so it was a little more trouble to "Clean" the object before using

trxcllnt commented 7 years ago

@drFabio so that's an interesting case. When we use the new recycleJSON path walk, we compute a hashcode from the cache hits. On the next get request, we compare the hashcodes and versions of the JSON tree we constructed last time against the new request and current cache state, so we can efficiently skip branches that haven't changed. Along with this, we can have fast React updates that compare $__hash and $__version in shouldComponentUpdate.

I intentionally didn't add this to the immutable path walk because it would negatively impact performance, and would only be useful if it's used for React-style sub-tree diff'ing. Generally the trade-off I've made is using Falcor + React, use recycleJSON: true on the root Model, because the output JSON is only read during render. Do you need to use the immutable-JSON form of the path walks, or could you instead switch to the recycleJSON form? If it's the former, would it help if I computed the hashcodes during the immutable path walk as well?

drFabio commented 7 years ago

Well I'm not actively using falcor anymore but I think having a clean plain view of the object is important. without metadata. On my case I was looping through the keys to display a list of some attributes and the metadata was in the way the idea was that you should be able to interact with the incoming data just as a plain JSON. I think that even if internally you use the metaData the default way to receive data should be with a plain object. I'll probably play with falcor on a few months so I can give a better input than now.