Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For optimization, test equality of state #13

Open
chandlerprall opened this issue Oct 11, 2017 · 2 comments
Open

For optimization, test equality of state #13

chandlerprall opened this issue Oct 11, 2017 · 2 comments
Milestone

Comments

@chandlerprall
Copy link
Owner

  • when setting state (either source or computed state) first check if the new state equals the existing state, if so exit doing nothing
  • when calling a subscriber, compare the data going into the subscriber with what it was last called with, if data is equal then do nothing

"check state equality" should be done with a shallow compare, and allow middleware to override

@chandlerprall chandlerprall added this to the 3.0 milestone Oct 11, 2017
@michaelmerrill
Copy link
Collaborator

I like this. It will encourage users to use the most direct node in the tree to set state. We will need to make this very explicit in the docs and give examples for how to turn it off. I could see it being confusing for people coming from other state management tools... Basically, we are encouraging users to be very specific with what parts of state update and to use multiple setPartialState()s instead of lazily setting a portion of the state tree once.

The shallow equals will need to handle different types. Do we compare functions? We could stringify them but it could get expensive and is not always accurate.

@chandlerprall
Copy link
Owner Author

I think shallow equality is fairly well understood and accepted? Definitely want to call it out in the docs, but:

objects A and B are unequal if

  • object A has different keys than object B
  • any values in A !== same value in B

this means that if you have a function in state you end up testing if it's the same object in memory on both sides (same applies to objects -- anything that isn't a primitive)

Middleware will be able to add additional checks, for example:

  • insula-immutable - Immutable objects have an equals method on them that effectively does deep comparison
  • deep equality check only on specific values
  • for some object types (or selectors?), check if an object's id field matches on both sides

As a middleware, insula-immutable can essentially do:

if (isImmutable(a) && isImmutable(b) {
  return a.equals(b); // insula-immutable decides fate of the update
} else {
  next(); // rely on other middleware / insula's default shallow compare
}

letting it avoid dealing with any cases where a or b are primitive values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants