Skip to content

Commit

Permalink
🐛 fix: Maps didn't add/update new contents as shared types
Browse files Browse the repository at this point in the history
  • Loading branch information
joebobmiles committed Jul 12, 2021
1 parent 01e4eec commit 9696558
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/patching.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,22 @@ describe("patchSharedType", () =>
expect(ymap.get("state").get("foo")).toBe(2);
});

it("Creates a map when value is updated from scalar to object.", () =>
{
ymap.set("state", objectToYMap({ "foo": 1, }));
patchSharedType(ymap.get("state"), { "foo": { "bar": 2, }, });

expect((ymap.get("state").get("foo") as Y.Map<any>).get("bar")).toBe(2);
});

it("Creates an array when value is updated from scalar to array.", () =>
{
ymap.set("state", objectToYMap({ "foo": 1, }));
patchSharedType(ymap.get("state"), { "foo": [ 1, 2 ], });

expect((ymap.get("state").get("foo") as Y.Array<any>).get(0)).toEqual(1);
});

it("Applies deletes to maps.", () =>
{
ymap.set("state", objectToYMap({ "foo": 1, }));
Expand Down
11 changes: 10 additions & 1 deletion src/patching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,16 @@ export const patchSharedType = (
if ((value instanceof Function) === false)
{
if (sharedType instanceof Y.Map)
sharedType.set(property as string, value);
{
if (value instanceof Array)
sharedType.set(property as string, arrayToYArray(value));

else if (value instanceof Object)
sharedType.set(property as string, objectToYMap(value));

else
sharedType.set(property as string, value);
}

else if (sharedType instanceof Y.Array)
{
Expand Down

0 comments on commit 9696558

Please sign in to comment.