Open nickretallack opened 4 years ago
@nickretallack your solution should be sufficient. The distance selector is memoized correctly, the handler function objectsByDisntanceSelector
will only be recomputed in the event that the state changes.
However, you are calling objectDistanceSelector
incorrectly, which should actually be invoked like:
objectDistanceSelector(a)(state);
rather than
objectDistanceSelector(state, a);
Do you see the two places where this could be improved though?
objectDistanceSelector
will be recomputed if the state changes in any way, but ideally it would only be recomputed if state.objects[objectId].position
changed.
Also, I'd like to memoize the sorting that occurs in objectsByDistanceSelector
, and only recalculate it if any state.objects[objectId].position
changes. It'd be nice if it was somehow aware of what state objectDistanceSelector
is depending on.
I came across this problem and saw that it wasn't resolved. My solution was to just wrap reselectie
's memoizeAs
with my own that matches the same signature as memoize
, thus allowing arbitrary composing, just like in reselect
.
const myMemoizeAs = (...selectors) => {
const memAs = memoizeAs(...selectors);
return (state, key) => memAs(key)(state);
}
Docs say they're composable and shows that you can use a
memoize
inside amemoizeAs
, but can you use amemoizeAs
inside amemoize
?I want to do something like this:
I'm not sure how to memoize the result correctly. I guess it needs to know that it depends on whatever
objectDistanceSelector
depends on, somehow. Also, ifobjectDistanceSelector
could know not to update for every object when unrelated objects move, that'd be nice.