Skip to content

Commit

Permalink
enhance: useDLE() deps list more specific to avoid extraneous re-comp…
Browse files Browse the repository at this point in the history
…utation
  • Loading branch information
ntucker committed Jul 26, 2024
1 parent 3e4e1f9 commit fecb5b5
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-baboons-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@data-client/react': patch
---

useDLE() deps list more specific to avoid extraneous re-computation
4 changes: 2 additions & 2 deletions docs/core/api/DataProvider.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ export interface State<T> {
readonly entities: {
readonly [entityKey: string]: { readonly [pk: string]: T } | undefined;
};
readonly indexes: NormalizedIndex;
readonly endpoints: {
readonly [key: string]: unknown | PK[] | PK | undefined;
};
readonly indexes: NormalizedIndex;
readonly meta: {
readonly [key: string]: {
readonly date: number;
Expand Down Expand Up @@ -116,7 +116,7 @@ const RealApp = (

<img src="/img/client-logo.svg" style={{float:'right',width:'40px'}} />

In development, a small button will appear that gives easy access to browser devtools if
In development, a small button will appear that gives easy access to [browser devtools](../getting-started/debugging.md) if
installed. This option configures where it shows up, or if null will disable it altogether.

`'bottom-right' | 'bottom-left' | 'top-right'| 'top-left' | null` = `'bottom-right'`
Expand Down
7 changes: 4 additions & 3 deletions packages/react/src/hooks/useCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ export default function useCache<
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [expiresAt, controller, key, forceFetch, state.lastReset]);

const wouldSuspend = expiryStatus !== ExpiryStatus.Valid && expired;
// fully "valid" data will not suspend/loading even if it is not fresh
const loading = expiryStatus !== ExpiryStatus.Valid && expired;

return useMemo(() => {
// if useSuspense() would suspend, don't include entities from cache
if (wouldSuspend) {
if (loading) {
if (!endpoint.schema) return undefined;
return controller.getResponse(endpoint, ...args, {
...state,
Expand All @@ -80,6 +81,6 @@ export default function useCache<
// key substitutes args + endpoint
// we only need cacheResults, as entities are not used in this case
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [key, controller, data, wouldSuspend, cacheResults]);
}, [key, controller, data, loading, cacheResults]);
/*********************** end block *****************************/
}
12 changes: 7 additions & 5 deletions packages/react/src/hooks/useDLE.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ export default function useDLE<
const controller = useController();

const key = args[0] !== null ? endpoint.key(...args) : '';
const cacheResults = args[0] !== null && state.endpoints[key];
const cacheResults = key && state.endpoints[key];
const meta = state.meta[key];

// Compute denormalized value
// eslint-disable-next-line prefer-const
Expand All @@ -89,12 +90,10 @@ export default function useDLE<
state.indexes,
state.entities,
state.entityMeta,
state.meta,
meta,
key,
]);

const error = controller.getError(endpoint, ...args, state);

// If we are hard invalid we must fetch regardless of triggering or staleness
const forceFetch = expiryStatus === ExpiryStatus.Invalid;

Expand Down Expand Up @@ -132,8 +131,11 @@ export default function useDLE<
}
return data;
// key substitutes args + endpoint
// we only need cacheResults, as entities are not used in this case
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [key, controller, data, loading, state]);
}, [key, controller, data, loading, cacheResults]);

const error = controller.getError(endpoint, ...args, state);

return {
data,
Expand Down
14 changes: 8 additions & 6 deletions packages/react/src/hooks/useDLE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ExpiryStatus } from '@data-client/core';
import { useMemo } from 'react';

import useCacheState from './useCacheState.js';
import useController from '../hooks/useController.js';
import useController from './useController.js';

type SchemaReturn<S extends Schema | undefined> =
| {
Expand Down Expand Up @@ -75,7 +75,8 @@ export default function useDLE<
const controller = useController();

const key = args[0] !== null ? endpoint.key(...args) : '';
const cacheResults = args[0] !== null && state.endpoints[key];
const cacheResults = key && state.endpoints[key];
const meta = state.meta[key];

// Compute denormalized value
// eslint-disable-next-line prefer-const
Expand All @@ -87,12 +88,10 @@ export default function useDLE<
state.indexes,
state.entities,
state.entityMeta,
state.meta,
meta,
key,
]);

const error = controller.getError(endpoint, ...args, state);

// If we are hard invalid we must fetch regardless of triggering or staleness
const forceFetch = expiryStatus === ExpiryStatus.Invalid;

Expand All @@ -119,8 +118,11 @@ export default function useDLE<
}
return data;
// key substitutes args + endpoint
// we only need cacheResults, as entities are not used in this case
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [key, controller, data, loading, state]);
}, [key, controller, data, loading, cacheResults]);

const error = controller.getError(endpoint, ...args, state);

return {
data,
Expand Down

0 comments on commit fecb5b5

Please sign in to comment.