Skip to content

Commit

Permalink
enhance: State updates from mutate/read now the same (#326)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Mutation fetches now result in:
- meta entries
- results entries
  • Loading branch information
ntucker committed Apr 26, 2020
1 parent 5a06fe4 commit c789dfb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 23 deletions.
10 changes: 7 additions & 3 deletions packages/rest-hooks/src/state/__tests__/reducer.ts
Expand Up @@ -88,14 +88,15 @@ describe('reducer', () => {
schema: ArticleResource.getEntitySchema(),
key: ArticleResource.listUrl(payload),
date: 0,
expiresAt: 1000000000000,
},
};
const iniState = {
...initialState,
results: { abc: '5' },
results: { abc: '5', [ArticleResource.listUrl(payload)]: `${id}` },
};
const newState = reducer(iniState, action);
expect(newState.results).toBe(iniState.results);
expect(newState.results).toStrictEqual(iniState.results);
});
it('purge should delete entities', () => {
const id = 20;
Expand Down Expand Up @@ -239,6 +240,7 @@ describe('reducer', () => {
key: ArticleResource.createShape().getFetchKey({}),
updaters,
date: 0,
expiresAt: 100000000000,
},
};
}
Expand Down Expand Up @@ -386,12 +388,14 @@ describe('reducer', () => {
schema: ArticleResource.getEntitySchema(),
key: ArticleResource.url({ id }),
date: 0,
expiresAt: 10000000000000000000,
},
error: true,
};
const iniState = initialState;
const newState = reducer(iniState, action);
expect(newState).toEqual(iniState);
// ignore meta for this check
expect({ ...newState, meta: {} }).toEqual(iniState);
});
it('should not delete on error for "purge"', () => {
const id = 20;
Expand Down
21 changes: 1 addition & 20 deletions packages/rest-hooks/src/state/reducer.ts
Expand Up @@ -52,6 +52,7 @@ export default function reducer(
],
};
}
case RECEIVE_MUTATE_TYPE:
case RECEIVE_TYPE: {
if (action.error) {
return {
Expand Down Expand Up @@ -90,26 +91,6 @@ export default function reducer(
optimistic: filterOptimistic(state, action),
};
}
case RECEIVE_MUTATE_TYPE: {
if (action.error)
return { ...state, optimistic: filterOptimistic(state, action) };
const { entities, result, indexes } = normalize(
action.payload,
action.meta.schema,
);
const results = applyUpdatersToResults(
state.results,
result,
action.meta.updaters,
);
return {
...state,
entities: mergeDeepCopy(state.entities, entities),
indexes: mergeDeepCopy(state.indexes, indexes),
results,
optimistic: filterOptimistic(state, action),
};
}
case RECEIVE_DELETE_TYPE: {
if (action.error)
return { ...state, optimistic: filterOptimistic(state, action) };
Expand Down
1 change: 1 addition & 0 deletions packages/rest-hooks/src/types.ts
Expand Up @@ -83,6 +83,7 @@ interface RPCMeta<S extends Schema> {
key: string;
date: number;
updaters?: Record<string, UpdateFunction<S, any>>;
expiresAt: number;
}

export type RPCAction<
Expand Down

0 comments on commit c789dfb

Please sign in to comment.