diff --git a/src/patching.spec.ts b/src/patching.spec.ts index e6c12b8..a8940b1 100644 --- a/src/patching.spec.ts +++ b/src/patching.spec.ts @@ -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(() => ({ - "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, }); }); }); \ No newline at end of file diff --git a/src/patching.ts b/src/patching.ts index 9dfdc4e..a64613f 100644 --- a/src/patching.ts +++ b/src/patching.ts @@ -177,52 +177,58 @@ export const patchStore = ( { 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(