Skip to content

weakMapMemoize with resultEqualityCheck is provided empty objects for first call. #693

Open
@keegan-lillo

Description

@keegan-lillo

Hi! Thanks for all the great QOL improvements in Reselect 5, however I've found a pretty major issue with the resultEqualityCheck option when using the weakMapMemoize memoization. It calls the resultEqualityCheck even if it is the first time the selector has run. This wouldn't be too bad, however this first call is called with empty objects instead of the actual result of the selector. Please see the example below. Obviously this is a bit of a contrived example using strings, but things get pretty hairy if you expect the result to be a Set or another complex object.

const firstMessageSelector = createSelector(
  [(messages) => messages[0]],
  (message) => `${message}!!!`,
  {
    memoizeOptions: {
      resultEqualityCheck: (a, b) => {
        console.log("resultEqualityCheck:", { a, b });
        return a === b;
      },
    },
  }
);

const one = firstMessageSelector(["one", "two"]);
console.log("one:", one);
const two = firstMessageSelector(["two", "three"]);
console.log("two:", two);

Expected Console Output:

firstMessageSelector: one
one: one!!!
firstMessageSelector: two 
resultEqualityCheck: Object { a: "one!!!", b: "two!!!" }
two: two!!! 

Actual Console Output:

firstMessageSelector: one
resultEqualityCheck: Object { a: {}, b: {} }
one: one!!!
firstMessageSelector: two 
resultEqualityCheck: Object { a: "one!!!", b: "two!!!" }
two: two!!! 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions