Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance: useCache() and useStatefulResource() respect invalidIfStale (#…
…307) BREAKING CHANGE: When invalidIfStale is true, useCache() and useStatefulResource() will no longer return entities, even if they are in the cache
- Loading branch information
Showing
9 changed files
with
136 additions
and
36 deletions.
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
11 changes: 6 additions & 5 deletions
11
packages/rest-hooks/src/react-integration/__tests__/useResource.tsx
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
19 changes: 17 additions & 2 deletions
19
packages/rest-hooks/src/react-integration/hooks/useCache.ts
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 |
---|---|---|
@@ -1,12 +1,27 @@ | ||
import { StateContext } from 'rest-hooks/react-integration/context'; | ||
import { ReadShape, ParamsFromShape } from 'rest-hooks/resource'; | ||
import { useDenormalized } from 'rest-hooks/state/selectors'; | ||
import { useContext } from 'react'; | ||
|
||
import { useContext, useMemo } from 'react'; | ||
|
||
import useExpiresAt from './useExpiresAt'; | ||
|
||
/** Access a resource if it is available. */ | ||
export default function useCache< | ||
Shape extends Pick<ReadShape<any, any>, 'getFetchKey' | 'schema'> | ||
>(fetchShape: Shape, params: ParamsFromShape<Shape> | null) { | ||
const expiresAt = useExpiresAt(fetchShape, params); | ||
// This computation reflects the behavior of useResource/useRetrive | ||
// It only changes the value when expiry or params change. | ||
// This way, random unrelated re-renders don't cause the concept of expiry | ||
// to change | ||
const expired = useMemo(() => { | ||
if (Date.now() <= expiresAt || !params) return false; | ||
return true; | ||
// we need to check against serialized params, since params can change frequently | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [expiresAt, params && fetchShape.getFetchKey(params)]); | ||
|
||
const state = useContext(StateContext); | ||
return useDenormalized(fetchShape, params, state)[0]; | ||
return useDenormalized(fetchShape, params, state, expired)[0]; | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/rest-hooks/src/react-integration/hooks/useExpiresAt.ts
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,15 @@ | ||
import { ReadShape } from 'rest-hooks/resource'; | ||
|
||
import useMeta from './useMeta'; | ||
|
||
/** Returns whether the data at this url is fresh or stale */ | ||
export default function useExpiresAt<Params extends Readonly<object>>( | ||
fetchShape: Pick<ReadShape<any, Params>, 'getFetchKey'>, | ||
params: Params | null, | ||
): number { | ||
const meta = useMeta(fetchShape, params); | ||
if (!meta) { | ||
return 0; | ||
} | ||
return meta.expiresAt; | ||
} |
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
1 change: 1 addition & 0 deletions
1
packages/rest-hooks/src/react-integration/hooks/useSubscription.ts
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