Skip to content

Commit

Permalink
✅ test: When there is no diff, do not patch
Browse files Browse the repository at this point in the history
  • Loading branch information
joebobmiles committed Jul 13, 2021
1 parent 9ab35a5 commit 0e3ecb1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 57 deletions.
26 changes: 11 additions & 15 deletions src/patching.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,28 +652,24 @@ describe("patchStore", () =>
expect(store.getState())
.toEqual({ "owner": { "person": { "age": 1, "name": "Joe", }, }, });
});
});

describe("Combining patchSharedType and patchStore.", () =>
{
it("Does not clobber state when updating shared type.", () =>
it("Does not truncate state if there are no changes.", () =>
{
const ydoc = new Y.Doc();
const ymap = ydoc.getMap("map");

ymap.observeDeep(() =>
type State =
{
patchStore(api, ymap.toJSON());
});
"count": 0,
};

const api = create(() =>
const store = create<State>(() =>
({
"count": 1,
"count": 0,
}));

patchSharedType(ymap, api.getState());
patchStore(
store,
{ "count": 0, }
);

expect(api.getState().count).toBe(1);
expect(ymap.get("count")).toBe(1);
expect(store.getState()).toEqual({ "count": 0, });
});
});
90 changes: 48 additions & 42 deletions src/patching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,52 +177,58 @@ export const patchStore = <S extends State>(
{
const changes = getChangeList(oldState, newState);

const p: any = changes.reduce(
(state, [ type, property, value ]) =>
{
switch (type)
{
case "add":
case "update":
case "none":
{
return {
...state,
[property]: value,
};
}
if (changes.length === 0)
return oldState;

case "pending":
else
{
const p: any = changes.reduce(
(state, [ type, property, value ]) =>
{
return {
...state,
[property]: patch(
oldState[property as string],
newState[property as string]
),
};
}
switch (type)
{
case "add":
case "update":
case "none":
{
return {
...state,
[property]: value,
};
}

case "delete":
default:
return state;
}
},
{}
);

return {
...Object.entries(oldState).reduce(
(o, [ property, value ]) =>
(
value instanceof Function
? { ...o, [property]: value, }
: o
),
case "pending":
{
return {
...state,
[property]: patch(
oldState[property as string],
newState[property as string]
),
};
}

case "delete":
default:
return state;
}
},
{}
),
...p,
};
);

return {
...Object.entries(oldState).reduce(
(o, [ property, value ]) =>
(
value instanceof Function
? { ...o, [property]: value, }
: o
),
{}
),
...p,
};
}
};

store.setState(
Expand Down

0 comments on commit 0e3ecb1

Please sign in to comment.