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 all commits
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ Coming from `react-use`? Check out our

- #### Miscellaneous

- [**`useMemoCache`**](https://react-hookz.github.io/web/?path=/docs/miscellaneous-useMemoCache--example)
— Like useMemo but with cache based on dependency list.
- [**`useSyncedRef`**](https://react-hookz.github.io/web/?path=/docs/miscellaneous-usesyncedref--example)
— Like `useRef`, but it returns an immutable ref that contains the actual value.
- [**`useCustomCompareMemo`**](https://react-hookz.github.io/web/?path=/docs/miscellaneous-useCustomCompareMemo--example)
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export * from './useVibrate';
export * from './useSyncedRef';
export * from './useHookableRef';
export * from './useCustomCompareMemo';
export * from './useMemoCache';

// SideEffect
export * from './useLocalStorageValue';
Expand All @@ -75,6 +76,7 @@ export * from './useWindowSize';
export { isStrictEqual, truthyAndArrayPredicate, truthyOrArrayPredicate } from './util/const';
export { EffectCallback, EffectHook } from './util/misc';
export { resolveHookState } from './util/resolveHookState';
export { is, objectIs, areHookInputsEqual } from './util/areHookInputsEqual';

// Types
export * from './types';
106 changes: 106 additions & 0 deletions src/useMemoCache/__docs__/example.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import React, { useMemo, useRef, useState } from 'react';
import { useMemoCache } from '../..';

export const Example: React.FC = () => {
const { initialFilters: filters, initialUsers: users } = useMemo(() => {
const initialFilters = {
none: 'none',
premiumCustomer: 'premium customer',
customer: 'customer',
guest: 'guest',
};

const initialUsers = [
{
name: 'James',
type: initialFilters.customer,
},
{
name: 'Robert',
type: initialFilters.premiumCustomer,
},
{
name: 'Mary',
type: initialFilters.guest,
},
{
name: 'Elizabeth',
type: initialFilters.customer,
},
{
name: 'Richard',
type: initialFilters.guest,
},
];

return {
initialFilters,
initialUsers,
};
}, []);

const [selectedFilter, setSelectedFilter] = useState(filters.none);

const memoCalls = useRef(0);
const memoCacheCalls = useRef(0);

const memoUsers = useMemo(() => {
memoCalls.current++;

return users.filter((user) => selectedFilter === filters.none || user.type === selectedFilter);
}, [filters.none, selectedFilter, users]);
const memoCacheUsers = useMemoCache(() => {
memoCacheCalls.current++;

return users.filter((user) => selectedFilter === filters.none || user.type === selectedFilter);
}, [filters.none, selectedFilter, users]);

const listing = [
{
name: useMemoCache.name,
calls: memoCacheCalls.current,
listedUsers: memoCacheUsers,
},
{
name: useMemo.name,
calls: memoCalls.current,
listedUsers: memoUsers,
},
];

const labelDomId = 'user-type';

return (
<section>
<h1>Example of useMemoCache</h1>
<div>
{listing.map(({ name, listedUsers, calls }) => (
<section key={name}>
<h2>
{name}: calls {calls}
</h2>
<ul>
{listedUsers.map((user) => (
<li>
<p>
{user.name}: {user.type}
</p>
</li>
))}
</ul>
</section>
))}
</div>
<label htmlFor={labelDomId}>
select filter:{' '}
<select id={labelDomId} onChange={({ target: { value } }) => setSelectedFilter(value)}>
{Object.values(filters).map((filter) => (
<option selected={selectedFilter === filter} value={filter}>
{filter}
</option>
))}
</select>
</label>
</section>
);
};
35 changes: 35 additions & 0 deletions src/useMemoCache/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs/blocks';
import { Example } from './example.stories';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="Miscellaneous/useMemoCache" component={Example} />

# useMemoCache

Copy link
Contributor

Choose a reason for hiding this comment

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

Hook description missing here. You have it in other places and it's good.

Like useMemo but with cache based on dependency list.

#### Example

<Canvas>
<Story story={Example} inline />
</Canvas>

## Reference

```ts
function useMemoCache<State, Deps extends DependencyList>(
factory: () => State,
deps: Deps,
customAreHookInputsEqual?: (nextDeps: DependencyList, prevDeps: DependencyList | null) => boolean
): State;
```

#### Importing

<ImportPath />

#### Arguments

- **factory** _`() => unknown`_ - useMemo factory function
- **deps** _`DependencyList`_ - useMemo dependency list
- **customAreHookInputsEqual** _`(nextDeps: DependencyList, prevDeps: DependencyList | null) => boolean`_ - function which indicates when dependencies are the same
Loading