-
-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support nested memoize #4
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 3f58857:
|
reduxjs/react-redux#1653 (comment)
@theKashey I hope I understand the problem. proxy tracking itself doesn't help memoization. What I'd like to investigate is whether nested memoize solves some use cases. |
...ok, it wasn't so easy. Let me take some more time. |
"memoization problem" is cache size problem:
I am looking for a solution to this problem, and I don't have a good one yet. Well, atom based mamagers(Recoil, Jotai, Reatom) are. |
OK, I think this is possible. https://codesandbox.io/s/vanilla-typescript-forked-hd72i?file=/src/index.ts import memoize from "proxy-memoize";
const state = {
nodes: [
{ id: 1, text: "a" },
{ id: 2, text: "b" },
{ id: 3, text: "c" }
]
};
type State = typeof state;
const capitalizeTexts = memoize((state: State) => {
console.log("capitalize");
return {
...state,
nodes: state.nodes.map((item) => ({
...item,
text: item.text.toUpperCase()
}))
};
});
const createGetItem = () => {
const getItem = memoize(({ state, id }: { state: State; id: number }) => {
const { nodes } = capitalizeTexts(state);
const found = nodes.find((item) => item.id === id);
return found && { text: found.text };
});
return getItem;
};
const getItemA = createGetItem();
const getItemB = createGetItem(); console.log(getItemA({ state, id: 1 })); // -> { text: "A" }
console.log(getItemB({ state, id: 2 })); // -> { text: "B" }
console.log(getItemA({ state, id: 3 }) === getItemA({ state, id: 3 })); // -> true
console.log(getItemA({ state, id: 3 }) === getItemB({ state, id: 3 })); // -> false capitalize is only called once because state isn't changed. @theKashey proxy-memoize has a WeakMap for result caching (like kashe?). How do you like it? |
Haven't check the code, but probably I don't like it 😎
Ie:
Here is an example - https://codesandbox.io/s/vanilla-typescript-forked-ibz8r?file=/src/index.ts So I was looking for the same "per selector cache" of "size 1". But stored in a box controlled externally. |
Thanks for your comment. I'll take another shot if I can do anything with it without affecting the api. (edit.1) OK, I start to understand the ideal solution is technically impossible. Hm. (edit.2) The problem I'm trying to solve might be a bit different. |
just adds
trackMemo
from proxy-compare.