You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'll start with this: I'm NOT suggesting to remove argument comparison, arguments should be compared anyway and comparing them based on === should be sufficient to start the result evaluation.
However the provided isEqual function should be comparing the results (lastResult + new result), when the arguments comparison doesn't pass.
Reasoning:
Comparing arguments is almost useless, especially if there are two or more arguments coming in.
Consider the following:
enumOrderType{EXEC_LEVEL,INTERNAL,PUBLIC,}interfaceOrder{type: OrderType,// <-- this is the enum aboveid: string,totalCost: string,}// Provides Order[] with all existing orders.constgetAllOrders=createSelector(someSelectorWithOrders,(state)=>state.orders);// Provides currently selected OrderType (the one from the enum).constgetSelectedType=createSelector(someSelectorWithTypes,(state)=>state.currentType);// Assume we have the following comparer, which would just LOGs what that it gets to compare.functionisMyCustomComparer(a: any,b: any){console.log('a:',a,'b:',b);returna===b;// this is what is currently isEqualCheck is doing anyway.}// This is how we use the default Memoizer, but are providing custom `isEquals`,// which is a second argument in the defaultMemoizer function.constcustomMemoizer=(aFn)=>defaultMemoize(aFn,isMyCustomComparer);// The actual select of interest. Returns Orders filtered by Current type.constgetOrdersBySelectedType=createSelectorFactory(customMemoizer)(getAllOrders,getSelectedType,(orders: Order[],currentOrderType: OrderType)=>orders.filter(order=>order.type===currentOrderType));
That's because both getAllOrders and getSelectedType go through that isMyCustomComparer
So basically when there are 2 or more "arguments" there could be nothing common for them to compare against.
Comparing the final result will be a lot more beneficial.
E.g. say another order get added, of OrderType.EXEC_LEVEL. That would cause getAllOrders to recompute, and, as a result, getOrdersBySelectedType to recompute and produce another array with the same values (because this added order is does not match my filtered type).
Instead of that, I would like to provide the function like
So that if resulting array has the exact same values then give me the previous result, so that emits are not triggered.
Describe any alternatives/workarounds you're currently using
There are 2 workaround currently:
Wrap the selector with another selector
// Notice, that createSelector is used here, not createSelectorFactoryconstgetOrdersBySelectedTypeIntermediate=createSelector(getAllOrders,getSelectedType,(orders: Order[],currentOrderType: OrderType)=>orders.filter(order=>order.type===currentOrderType));constgetOrdersBySelectedType=createSelectorFactory(customMemoizer)(getOrdersBySelectedTypeIntermediate,filteredOrders=>filteredOrders);
Provide custom memoization. I won't be showing this, but basically it would look similar to defaultMemoizer but more error-prone and has to use the magically appearing arguments within the function as well.
If accepted, I would be willing to submit a PR for this feature
[x] Yes (Assistance is provided if you need help submitting a pull request)
[ ] No
@brandonroberts@MikeRyanDev I would like to change that if you don't mind. It will be a breaking change if anyone is currently providing isEqual to defaultMemoize, but I really doubt that anyone does.
The text was updated successfully, but these errors were encountered:
I'll start with this: I'm NOT suggesting to remove argument comparison, arguments should be compared anyway and comparing them based on === should be sufficient to start the result evaluation.
However the provided
isEqual
function should be comparing the results (lastResult + new result), when the arguments comparison doesn't pass.Reasoning:
Consider the following:
Let's say we have the following global state
Now let's call it twice and see what
console.log('a:', a, 'b:', b);
insideisMyCustomComparer
will produce.The output we get is:
That's because both
getAllOrders
andgetSelectedType
go through thatisMyCustomComparer
So basically when there are 2 or more "arguments" there could be nothing common for them to compare against.
E.g. say another order get added, of
OrderType.EXEC_LEVEL
. That would causegetAllOrders
to recompute, and, as a result,getOrdersBySelectedType
to recompute and produce another array with the same values (because this added order is does not match my filtered type).Instead of that, I would like to provide the function like
So that if resulting array has the exact same values then give me the previous result, so that emits are not triggered.
Describe any alternatives/workarounds you're currently using
There are 2 workaround currently:
defaultMemoizer
but more error-prone and has to use the magically appearingarguments
within the function as well.If accepted, I would be willing to submit a PR for this feature
[x] Yes (Assistance is provided if you need help submitting a pull request)
[ ] No
@brandonroberts @MikeRyanDev I would like to change that if you don't mind. It will be a breaking change if anyone is currently providing
isEqual
todefaultMemoize
, but I really doubt that anyone does.The text was updated successfully, but these errors were encountered: