Skip to content
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

feat(useMemoCache): useMemo with cache #1063

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9d528b2
feat: add useMemoCache hook
michaltarasiuk Jan 2, 2023
c95a94c
feat: add example
michaltarasiuk Jan 2, 2023
5d792bd
fix: example of useMemoCache hook
michaltarasiuk Jan 2, 2023
ad7faa9
fix: performence
michaltarasiuk Jan 3, 2023
0054efa
fix: cached item from Array to Set
michaltarasiuk Jan 3, 2023
ed4d919
feat: add test cases for areHookInputsEqual
michaltarasiuk Jan 3, 2023
a10ecee
feat: set downlevelIteration to true
michaltarasiuk Jan 3, 2023
f5dfafa
feat: add custom areHookInputsEqual
michaltarasiuk Jan 3, 2023
2fc981a
fix: deps of cache memo
michaltarasiuk Jan 3, 2023
078f3a1
fix: reference of customAreHookInputsEqual
michaltarasiuk Jan 3, 2023
f0f898d
fix: remove export of objectIs
michaltarasiuk Jan 3, 2023
b9347e0
feat: resolve conflicts
michaltarasiuk Jan 4, 2023
daa1f6e
feat: cover objectIs helper by test cases
michaltarasiuk Jan 4, 2023
a458b1d
feat: cover is helper by test cases
michaltarasiuk Jan 4, 2023
fc2dd98
fix: unstable refference of customAreHookInputsEqual
michaltarasiuk Jan 4, 2023
e253c3a
fix: docs of useMemoCache
michaltarasiuk Jan 4, 2023
8cf0cd1
feat: resolve conflicts
michaltarasiuk Jan 8, 2023
afe9ffd
feat: remove dist
michaltarasiuk Jan 8, 2023
6e6b7c9
feat: adapt to requirements
michaltarasiuk Jan 8, 2023
09c81e6
feat: add assertion before add to cache
michaltarasiuk Jan 8, 2023
22e5240
fix: naming
michaltarasiuk Jan 8, 2023
82b423f
fix: code review fix
michalt-monogo Jan 17, 2023
dfd6913
feat: useMemo to useCustomCompareMemo
michalt-monogo Jan 17, 2023
216a58a
feat: simplify cache
michalt-monogo Jan 17, 2023
6cda934
feat: add max entries logic
michalt-monogo Jan 18, 2023
2c82fc9
fix: index counter
michalt-monogo Jan 19, 2023
ce8ff2f
feat: simplified cache
michalt-monogo Jan 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/useMemoCache/__tests__/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,33 @@ describe('useMemoCache', () => {
expect(cachedValue2).toBe(value);
expect(memoCache.isNone(cachedValue1)).toBeFalsy();
});

it('cache should have max 64 entries', () => {
const memoCache = createMemoCache();

// eslint-disable-next-line symbol-description
const firstItem = Symbol();

const MAX_ENTRIES = 64;
let indexCounter = 0;

while (indexCounter !== MAX_ENTRIES) {
indexCounter++;

if (indexCounter === 1) {
memoCache.set([indexCounter], firstItem);
} else {
memoCache.set([indexCounter], null);
}
}

const state1 = memoCache.get([1]);
expect(state1).toBe(firstItem);

memoCache.set([indexCounter + 1], null);

const state2 = memoCache.get([1]);
expect(memoCache.isNone(state2)).toBeTruthy();
});
});
});
13 changes: 11 additions & 2 deletions src/useMemoCache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ type CachedItem<State> = { state: State; dependencyList: DependencyList };
export const createMemoCache = <State>(
customAreHookInputsEqual?: typeof nativeAreHookInputsEqual
) => {
const cache = new Set<CachedItem<State>>();
const MAX_ENTRIES = 64;
let indexCounter = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you just check cache.size to determine the current size of the cache? This counter seems unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, indexCounter is used to determine which element should be removed when threshold is hit

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see. Now I wonder if it would be better to just stop accepting new items to the cache after the maximum size is reached since the cache items are basically equally "important" so there is no reason to be deleting one item instead of another when the cache is filled.


const cache = new Map<number, CachedItem<State>>();
const areHookInputsEqual = customAreHookInputsEqual ?? nativeAreHookInputsEqual;

const get = (dependencyList: DependencyList) => {
Expand All @@ -34,8 +37,14 @@ export const createMemoCache = <State>(
areHookInputsEqual(item.dependencyList, dependencyList)
);

indexCounter++;

if (canAddToItem) {
cache.add({ dependencyList, state });
cache.set(indexCounter, { dependencyList, state });
}

if (canAddToItem && indexCounter > MAX_ENTRIES) {
cache.delete(indexCounter - MAX_ENTRIES);
}
};

Expand Down