Skip to content

Commit

Permalink
enhance: fetchAction.meta.createdAt -> fetchAction.meta.fetchedAt
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Jul 8, 2024
1 parent 9df0f7c commit 96f7eb0
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 24 deletions.
8 changes: 8 additions & 0 deletions .changeset/cold-melons-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@data-client/core': minor
---

Renamed FETCH action.meta.createdAt to fetchedAt to be consistent with other actions like
SET_RESPONSE.

BREAKING CHANGE: fetchAction.meta.createdAt -> fetchAction.meta.fetchedAt
2 changes: 1 addition & 1 deletion packages/core/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export interface FetchMeta<A extends readonly any[] = readonly any[]> {
resolve: (value?: any | PromiseLike<any>) => void;
reject: (reason?: any) => void;
promise: PromiseLike<any>;
createdAt: number;
fetchedAt: number;
}

export interface FetchAction<E extends EndpointAndUpdate<E> = EndpointDefault> {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/controller/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export default class Controller<
};

/**
* Resolves an inflight fetch. `fetchedAt` should `fetch`'s `createdAt`
* Resolves an inflight fetch.
* @see https://dataclient.io/docs/api/Controller#resolve
*/
resolve = <
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/controller/createFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function createFetch<
resolve,
reject,
promise,
createdAt: Date.now(),
fetchedAt: Date.now(),
};

return {
Expand Down
22 changes: 11 additions & 11 deletions packages/core/src/manager/NetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export default class NetworkManager implements Manager {
*/
protected handleFetch(action: FetchAction) {
const fetch = action.payload;
const { key, resolve, reject, createdAt } = action.meta;
const { key, resolve, reject, fetchedAt } = action.meta;
const throttle = !action.endpoint.sideEffect;

const deferedFetch = () => {
Expand All @@ -178,7 +178,7 @@ export default class NetworkManager implements Manager {
promise = resolvePromise(promise);
}
promise = promise
.then(data => {
.then(response => {
let lastReset = this.getLastReset();

/* istanbul ignore else */
Expand All @@ -190,23 +190,23 @@ export default class NetworkManager implements Manager {
}

// don't update state with promises started before last clear
if (createdAt >= lastReset) {
if (fetchedAt >= lastReset) {
this.controller.resolve(action.endpoint, {
args: action.meta.args,
response: data,
fetchedAt: createdAt,
response,
fetchedAt,
});
}
return data;
return response;
})
.catch(error => {
const lastReset = this.getLastReset();
// don't update state with promises started before last clear
if (createdAt >= lastReset) {
if (fetchedAt >= lastReset) {
this.controller.resolve(action.endpoint, {
args: action.meta.args,
response: error,
fetchedAt: createdAt,
fetchedAt,
error: true,
});
}
Expand All @@ -216,7 +216,7 @@ export default class NetworkManager implements Manager {
};

if (throttle) {
return this.throttle(key, deferedFetch, createdAt)
return this.throttle(key, deferedFetch, fetchedAt)
.then(data => resolve(data))
.catch(error => reject(error));
} else {
Expand Down Expand Up @@ -267,7 +267,7 @@ export default class NetworkManager implements Manager {
protected throttle(
key: string,
fetch: () => Promise<any>,
createdAt: number,
fetchedAt: number,
) {
const lastReset = this.getLastReset();
// we're already fetching so reuse the promise
Expand All @@ -280,7 +280,7 @@ export default class NetworkManager implements Manager {
this.resolvers[key] = resolve;
this.rejectors[key] = reject;
});
this.fetchedAt[key] = createdAt;
this.fetchedAt[key] = fetchedAt;

this.idleCallback(
() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/manager/__tests__/pollingSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,13 @@ describe('PollingSubscription', () => {
jest.advanceTimersByTime(5000);
expect(dispatch.mock.calls.length).toBe(1);
dispatch.mock.calls[0].forEach((element: any) => {
delete element?.meta?.createdAt;
delete element?.meta?.fetchedAt;
});
expect(dispatch.mock.calls[0]).toMatchSnapshot();
jest.advanceTimersByTime(5000);
expect(dispatch.mock.calls.length).toBe(2);
dispatch.mock.calls[1].forEach((element: any) => {
delete element?.meta?.createdAt;
delete element?.meta?.fetchedAt;
});

expect(dispatch.mock.calls[1]).toMatchSnapshot();
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/state/__tests__/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ describe('reducer', () => {
reject: (v: any) => null,
resolve: (v: any) => null,
promise: new Promise((v: any) => null),
createdAt: 0,
fetchedAt: 0,
},
};
const iniState = {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/state/reducer/fetchReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function fetchReducer(state: State<unknown>, action: FetchAction) {
if (action.endpoint.getOptimisticResponse && action.endpoint.sideEffect) {
setAction = createOptimistic(action.endpoint, {
args: action.meta.args,
fetchedAt: action.meta.createdAt,
fetchedAt: action.meta.fetchedAt,
});
} else {
// If 'fetch' action reaches the reducer there are no middlewares installed to handle it
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/__tests__/hooks-endpoint.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async function testDispatchFetch(
expect(dispatch).toHaveBeenCalledTimes(payloads.length);
let i = 0;
for (const call of dispatch.mock.calls) {
delete call[0]?.meta?.createdAt;
delete call[0]?.meta?.fetchedAt;
delete call[0]?.meta?.promise;
expect(call[0]).toMatchSnapshot();
const action = call[0];
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/__tests__/useDLE.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async function testDispatchFetch(
expect(dispatch.mock.calls.length).toBe(payloads.length);
const i = 0;
for (const call of dispatch.mock.calls) {
delete call[0]?.meta?.createdAt;
delete call[0]?.meta?.fetchedAt;
delete call[0]?.meta?.promise;
expect(call[0]).toMatchSnapshot();
// const action = call[0];
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/__tests__/useFetch.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async function testDispatchFetch(
expect(dispatch.mock.calls.length).toBe(payloads.length);
let i = 0;
for (const call of dispatch.mock.calls) {
delete call[0]?.meta?.createdAt;
delete call[0]?.meta?.fetchedAt;
delete call[0]?.meta?.promise;
expect(call[0]).toMatchSnapshot();
const action = call[0];
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/__tests__/useFetch.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async function testDispatchFetch(
expect(dispatch.mock.calls.length).toBe(payloads.length);
let i = 0;
for (const call of dispatch.mock.calls) {
delete call[0]?.meta?.createdAt;
delete call[0]?.meta?.fetchedAt;
delete call[0]?.meta?.promise;
expect(call[0]).toMatchSnapshot();
const action = call[0];
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/__tests__/useSuspense.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async function testDispatchFetch(
let i = 0;
for (const call of dispatch.mock.calls as any) {
expect(call[0].type).toBe(actionTypes.FETCH_TYPE);
delete call[0]?.meta?.createdAt;
delete call[0]?.meta?.fetchedAt;
delete call[0]?.meta?.promise;
expect(call[0]).toMatchSnapshot();
const action: FetchAction = call[0] as any;
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/__tests__/useSuspense.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async function testDispatchFetch(
let i = 0;
for (const call of dispatch.mock.calls as any) {
expect(call[0].type).toBe(actionTypes.FETCH_TYPE);
delete call[0]?.meta?.createdAt;
delete call[0]?.meta?.fetchedAt;
delete call[0]?.meta?.promise;
expect(call[0]).toMatchSnapshot();
const action: FetchAction = call[0] as any;
Expand Down

0 comments on commit 96f7eb0

Please sign in to comment.