Skip to content

Commit

Permalink
yaaaay!
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Dec 29, 2023
1 parent 474a5b1 commit 5a2f830
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
],
"license": "MIT",
"dependencies": {
"proxy-compare": "2.5.1"
"proxy-compare": "https://pkg.csb.dev/dai-shi/proxy-compare/commit/67f37fc9/proxy-compare"
},
"devDependencies": {
"@types/jest": "^29.5.1",
Expand Down
26 changes: 20 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion src/memoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ const touchAffected = (dst: unknown, src: unknown, affected: Affected) => {
});
};

const isOriginalEqual = (x: unknown, y: unknown): boolean => {
for (let xx = x; xx; x = xx, xx = getUntracked(xx));
for (let yy = y; yy; y = yy, yy = getUntracked(yy));
return Object.is(x, y);
};

// properties
const OBJ_PROPERTY = 'o';
const RESULT_PROPERTY = 'r';
Expand Down Expand Up @@ -105,7 +111,13 @@ export function memoize<Obj extends object, Result>(
for (let i = 0; i < size; i += 1) {
const memo = memoList[(memoListHead + i) % size];
if (!memo) break;
if (!isChanged(memo[OBJ_PROPERTY], obj, memo[AFFECTED_PROPERTY], new WeakMap())) {
if (!isChanged(
memo[OBJ_PROPERTY],
obj,
memo[AFFECTED_PROPERTY],
new WeakMap(),
isOriginalEqual,
)) {
touchAffected(obj, memo[OBJ_PROPERTY], memo[AFFECTED_PROPERTY]);
resultCache?.set(obj, memo);
return memo[RESULT_PROPERTY];
Expand Down

0 comments on commit 5a2f830

Please sign in to comment.