Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[enhance] Merge changes to resource instances #10

Merged
merged 6 commits into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/state/__tests__/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ describe('reducer', () => {
expiresAt: 5000500000,
},
};
const partialResultAction: ReceiveAction = {
...action,
payload: { id, title: 'hello' },
};
const iniState = {
entities: {},
results: {},
Expand All @@ -33,6 +37,21 @@ describe('reducer', () => {
expect(nextEntity).not.toBe(prevEntity);
expect(nextEntity).toBeDefined();
})
it('should merge partial entity with existing entity', () => {
const getEntity = (state: any): ArticleResource => state.entities[ArticleResource.getKey()][`${ArticleResource.pk(action.payload)}`]
const prevEntity = getEntity(newState);
expect(prevEntity).toBeDefined();
const nextState = reducer(newState, partialResultAction);
const nextEntity = getEntity(nextState);
expect(nextEntity).not.toBe(prevEntity);
expect(nextEntity).toBeDefined();

expect(nextEntity.title).not.toBe(prevEntity.title);
expect(nextEntity.title).toBe(partialResultAction.payload.title);

expect(nextEntity.content).toBe(prevEntity.content);
expect(nextEntity.content).not.toBe(partialResultAction.payload.content);
})
});
it('mutate should never change results', () => {
const id = 20;
Expand Down
13 changes: 10 additions & 3 deletions src/state/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { normalize } from '../resource';
import { merge } from 'lodash';
import { mergeWith } from 'lodash';
import { Resource } from '../resource';
import { ActionTypes, State } from '../types';

Expand All @@ -13,6 +13,13 @@ type Writable<T> = {
[P in keyof T]: NonNullable<T[P]>;
}

function resourceCustomizer(a: any, b: any): any {
ntucker marked this conversation as resolved.
Show resolved Hide resolved
if (a instanceof Resource && b instanceof Resource) {
ntucker marked this conversation as resolved.
Show resolved Hide resolved
const Static = b.constructor as typeof Resource;
return Static.merge(a, b);
}
}
ntucker marked this conversation as resolved.
Show resolved Hide resolved

export default function reducer(state: State<Resource>, action: ActionTypes) {
switch (action.type) {
case 'receive':
Expand All @@ -31,7 +38,7 @@ export default function reducer(state: State<Resource>, action: ActionTypes) {
}
const normalized = normalize(action.payload, action.meta.schema);
return {
entities: merge({ ...state.entities }, normalized.entities),
entities: mergeWith({ ...state.entities }, normalized.entities, resourceCustomizer),
results: {
...state.results,
[action.meta.url]: normalized.result,
Expand All @@ -49,7 +56,7 @@ export default function reducer(state: State<Resource>, action: ActionTypes) {
let { entities } = normalize(action.payload, action.meta.schema);
return {
...state,
entities: merge({ ...state.entities }, entities),
entities: mergeWith({ ...state.entities }, entities, resourceCustomizer),
};
case 'purge':
if (action.error) return state;
Expand Down