Skip to content

Commit

Permalink
Fix createSelectorCreator for reselect v4
Browse files Browse the repository at this point in the history
reselect's createSelectorCreator will use the passed memoization function for wrapping the result function, but will also (as of v4.0.0) use it to memoize the actual selector. Unfortunately our specialized mapped memoization functions won't work for general purpose memoization, so this breaks.

At the time of writing (reselect v4.0.0) the memoizeOptions passed to createSelectorCreator are _only_ passed through to the memoize that wraps the result func. We can use this param to determine whether to use our special memoize, or to use reselect's defaultMemoize.

In this way we make createSelectorCreator use our mapped memoize for wrapping the result func and defaultMemoize for wrapping the selector. It sucks that we're relying on an implementation detail inside reselect but I'll be back to fix this code again whenever it breaks :)

Closes #13
  • Loading branch information
heyimalex committed Dec 7, 2018
1 parent 73465fc commit 623d34f
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSelectorCreator } from "reselect";
import { createSelectorCreator, defaultMemoize } from "reselect";
import { memoizeMap, memoizeList } from "./memoize";

export const arrayMemoize = (fn, equalityCheck) =>
Expand Down Expand Up @@ -42,7 +42,32 @@ export const mapMemoize = (fn, equalityCheck) =>
}
});

export const createArraySelector = createSelectorCreator(arrayMemoize);
export const createObjectSelector = createSelectorCreator(objectMemoize);
export const createListSelector = createSelectorCreator(listMemoize);
export const createMapSelector = createSelectorCreator(mapMemoize);
// reselect's createSelectorCreator will use the passed memoization function
// for wrapping the result function, but will also (as of v4.0.0) use it to
// memoize the actual selector. Unfortunately our specialized mapped
// memoization functions won't work for general purpose memoization, so this
// breaks.
//
// At the time of writing (reselect v4.0.0) the memoizeOptions passed to
// createSelectorCreator are _only_ passed through to the memoize that wraps
// the result func. We can use this param to determine whether to use our
// special memoize, or to use reselect's defaultMemoize.
//
// In this way we make createSelectorCreator use our mapped memoize for
// wrapping the result func and defaultMemoize for wrapping the selector. It
// sucks that we're relying on an implementation detail but I'll be back to fix
// this code again whenever it breaks :)
function createMappedSelectorCreator(memoize) {
return createSelectorCreator((fn, mapmem) => {
if (mapmem === true) {
return memoize(fn);
} else {
return defaultMemoize(fn);
}
}, true);
}

export const createArraySelector = createMappedSelectorCreator(arrayMemoize);
export const createObjectSelector = createMappedSelectorCreator(objectMemoize);
export const createListSelector = createMappedSelectorCreator(listMemoize);
export const createMapSelector = createMappedSelectorCreator(mapMemoize);

0 comments on commit 623d34f

Please sign in to comment.