Skip to content

Commit

Permalink
fix: Correct expiresAt computation when cached + inferred results (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Feb 21, 2023
1 parent 4be7330 commit 32de97f
Show file tree
Hide file tree
Showing 11 changed files with 312 additions and 516 deletions.
28 changes: 12 additions & 16 deletions packages/core/src/controller/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
ResolveType,
DenormalizeNullable,
EntityTable,
Path,
} from '@rest-hooks/normalizr';
import { ExpiryStatus } from '@rest-hooks/normalizr';
import {
Expand Down Expand Up @@ -388,34 +389,29 @@ export default class Controller<

// second argument is false if any entities are missing
// eslint-disable-next-line prefer-const
const [data, found, suspend, resolvedEntities] = denormalize(
const [data, found, suspend, entityPaths] = denormalize(
results,
schema,
state.entities,
this.globalCache.entities,
isActive ? this.globalCache.results[key] : undefined,
) as [
DenormalizeNullable<E['schema']>,
boolean,
boolean,
Record<string, Record<string, any>>,
];
) as [DenormalizeNullable<E['schema']>, boolean, boolean, Path[]];

// fallback to entity expiry time
if (!expiresAt) {
// expiresAt existance is equivalent to cacheResults
if (found) {
const entityMeta = state.entityMeta;
// oldest entity dictates age
expiresAt = Infinity;
// using Object.keys ensures we don't hit `toString` type members
Object.entries(resolvedEntities).forEach(([key, entities]) =>
Object.keys(entities).forEach(pk => {
expiresAt = Math.min(
expiresAt,
state.entityMeta[key]?.[pk]?.expiresAt ?? Infinity,
);
}),
expiresAt = entityPaths.reduce(
(expiresAt: number, { pk, key }) =>
Math.min(expiresAt, entityMeta[key]?.[pk]?.expiresAt ?? Infinity),
Infinity,
);
/*expiresAt = entityPaths
.map(({ pk, key }) => entityMeta[key]?.[pk]?.expiresAt)
.filter(a => a)
.reduce((a, b) => Math.min(a, b), Infinity); Alternative method - is it faster?*/
} else {
expiresAt = 0;
}
Expand Down
14 changes: 13 additions & 1 deletion packages/core/src/controller/__tests__/getResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,25 @@ describe('Controller.getResponse()', () => {
const state = {
...initialState,
entities,
entityMeta: {
Tacos: {
1: { date: 1000000, expiresAt: 1100000, fetchedAt: 1000000 },
2: { date: 2000000, expiresAt: 2100000, fetchedAt: 2000000 },
},
},
};
const { data, expiryStatus } = controller.getResponse(
const { data, expiryStatus, expiresAt } = controller.getResponse(
ep,
{ id: '1' },
state,
);
expect(expiryStatus).toBe(ExpiryStatus.Valid);
expect(data).toMatchSnapshot();
expect(expiresAt).toBe(1100000);
// test caching
const second = controller.getResponse(ep, { id: '1' }, state);
expect(second.data.data).toBe(data.data);
expect(second.expiryStatus).toBe(expiryStatus);
expect(second.expiresAt).toBe(expiresAt);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ exports[`UnionSchema denormalization denormalizes deleted entities as undefined
},
true,
true,
{},
[],
]
`;
Loading

1 comment on commit 32de97f

@vercel
Copy link

@vercel vercel bot commented on 32de97f Feb 21, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.