Skip to content

Commit

Permalink
fix: Static props prevents selectors recall (#101)
Browse files Browse the repository at this point in the history
* add failing test for #100

* fix it!!!

* update test desc

* update CHANGELOG
  • Loading branch information
dai-shi authored Jul 11, 2024
1 parent 2bd551c commit 534afa9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Changed

- fix: Static props prevents selectors recall #101

## [3.0.0] - 2024-05-02

### Changed
Expand Down
1 change: 0 additions & 1 deletion src/memoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ const touchAffected = (dst: unknown, src: unknown, affected: Affected) => {
if (!isObject(dst) || !isObject(src)) return;
if (trackMemoOriginalObjSet.has(getUntracked(src) as never)) {
trackMemo(dst);
return;
}
const used = affected.get(getUntracked(src) || src);
if (!used) return;
Expand Down
44 changes: 44 additions & 0 deletions tests/issue_100.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, expect, it } from 'vitest';
import { memoize } from 'proxy-memoize';

describe('Static props prevents selectors recall (#100)', () => {
it('should select correctly with nested selectors', () => {
type State = {
book1: {
staticProp: string;
priceString: string;
};
};

const state1: State = {
book1: {
staticProp: '5',
priceString: '10',
},
};

const state2: State = {
book1: {
staticProp: '5',
priceString: '20',
},
};

const selectAllBooks = memoize((state: State) => Object.values(state));

const selectPriceString = memoize(
(state: State) => state.book1.priceString,
);

const selectAdjustedPriceString = memoize((state: State) => {
const priceString = selectPriceString(state);
state.book1.staticProp; // touch the prop
return priceString;
});

selectAllBooks(state1);

expect(selectAdjustedPriceString(state1)).toBe('10');
expect(selectAdjustedPriceString(state2)).toBe('20');
});
});

0 comments on commit 534afa9

Please sign in to comment.