-
Notifications
You must be signed in to change notification settings - Fork 99
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
Closed
michaltarasiuk
wants to merge
27
commits into
react-hookz:master
from
michaltarasiuk:pr/useMemoCache
Closed
Changes from 26 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
9d528b2
feat: add useMemoCache hook
michaltarasiuk c95a94c
feat: add example
michaltarasiuk 5d792bd
fix: example of useMemoCache hook
michaltarasiuk ad7faa9
fix: performence
michaltarasiuk 0054efa
fix: cached item from Array to Set
michaltarasiuk ed4d919
feat: add test cases for areHookInputsEqual
michaltarasiuk a10ecee
feat: set downlevelIteration to true
michaltarasiuk f5dfafa
feat: add custom areHookInputsEqual
michaltarasiuk 2fc981a
fix: deps of cache memo
michaltarasiuk 078f3a1
fix: reference of customAreHookInputsEqual
michaltarasiuk f0f898d
fix: remove export of objectIs
michaltarasiuk b9347e0
feat: resolve conflicts
michaltarasiuk daa1f6e
feat: cover objectIs helper by test cases
michaltarasiuk a458b1d
feat: cover is helper by test cases
michaltarasiuk fc2dd98
fix: unstable refference of customAreHookInputsEqual
michaltarasiuk e253c3a
fix: docs of useMemoCache
michaltarasiuk 8cf0cd1
feat: resolve conflicts
michaltarasiuk afe9ffd
feat: remove dist
michaltarasiuk 6e6b7c9
feat: adapt to requirements
michaltarasiuk 09c81e6
feat: add assertion before add to cache
michaltarasiuk 22e5240
fix: naming
michaltarasiuk 82b423f
fix: code review fix
michalt-monogo dfd6913
feat: useMemo to useCustomCompareMemo
michalt-monogo 216a58a
feat: simplify cache
michalt-monogo 6cda934
feat: add max entries logic
michalt-monogo 2c82fc9
fix: index counter
michalt-monogo ce8ff2f
feat: simplified cache
michalt-monogo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.