jamesplease / redux-resource

3kb resource management for Redux
https://redux-resource.js.org
MIT License
237 stars 28 forks source link

Version tracking for resource types #477

Open pgerlich opened 3 years ago

pgerlich commented 3 years ago

Hey everyone! Just wanted to say that you've done a great job with this library. I've actually built a pretty robust auto fetch framework using redux-resource, which we would like to open source in the near future.

With that said, we handle relatively complex state in our application, and I'm sure many consumers of redux-resource in general do.

Because state diffing can be very expensive and can result in many extra render cycles, I would like to propose a new attribute on the default redux-resource schema. That attribute would be a "version" which is just an atomic incrementing integer that increments each time a Create/Read/Update/Delete is performed on that given resource's state (including PENDING/FAILED requests). With this implementation, two versions could technically result in the same state version, but it is ideal in that two successive versions cannot have the same state. You are basically treating the state like a changeset based database, which it very much is. It also allows consumers to implement a simple areStatePropsEqual function that difs the versions for any resource types that the given component is listening to, resulting in no actual state diffing but an O(1) determination as to whether or not it should pass props on to the underlying component and perform a full render cycle.

Our AutoFetchFramework leverages a custom implementation of this functionality, and it works incredibly well. We are able to manage hundreds to thousands of complex resources with many attributes across dozens of resource types with no extra render cycles using this simple implementation.

If you are open to the above ^ I would be happy to open up a PR!

jamesplease commented 3 years ago

Thanks for taking the time to write out such a thoughtful issue! I agree that this feature is useful, and needs to be supported by Redux Resource, but I disagree that it should be in the core library. My opinion is that there’s enough of a need for a simpler system where resources are not versioned that this feature should be opt in rather than opt out.

Right now, the way to author opt in features are through plugins, which allow you direct access to the reducer.

You can read more about plugins here: https://redux-resource.js.org/other-guides/custom-action-types

If you’re unable to write a plugin for this feature due to API limitations, lmk!