How to use reselect on a changing list, when the objects are the same?
After all i had an idea which could work:
const list = {
object1: {
id: 'object1',
},
object2: {
id: 'object2',
},
object3: {
id: 'object3',
},
};
const order = ['object1', 'object3'];
const selector = (...args) => args.reduce((prev, curr) => ({...prev, [curr.id]: curr}), {});
createSelector(
state => state.order,
order => createSelector(
...order.map(id => state => state.list[id]),
selector,
)
);
The line ...order.map(id => state => state.list[id]),
will spread the objects as arguments. They will be the same if the order-array will not be changed. So i can generate a new Object with only the Objects listed in the order.
The evaluation function of the first create Selector only gets called if the order array changes. If this happens, a recalculation of the result is necessary anyway. So this is fine. The second one only recalculates if it gets new values. The values are functions that are generated out of the order array. Even if the list object changes (due to more entries or smaller changes on other objects that are not considered in the current list), the pointer to the objects of the order array stays the same. So we always get a the same objects as arguments to our second evaluation function. This prevents an unwanted update.