Skip to content

Commit

Permalink
✅ test: Yjs doesn't receive functions on patch
Browse files Browse the repository at this point in the history
  • Loading branch information
joebobmiles committed Jul 12, 2021
1 parent f8253b8 commit 0b7ef25
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 27 deletions.
28 changes: 28 additions & 0 deletions src/patching.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,34 @@ describe("patchSharedType", () =>
.get("foo")
.get("baz")).toBeUndefined();
});

it("Ignores when functions are added.", () =>
{
ymap.set("state", objectToYMap({ }));
patchSharedType(
ymap.get("state"),
{
"foo": () =>
null,
}
);

expect(ymap.get("state").get("foo")).toBeUndefined();
});

it("Ignores when values are set to functions.", () =>
{
ymap.set("state", objectToYMap({ "foo": 1, }));
patchSharedType(
ymap.get("state"),
{
"foo": () =>
null,
}
);

expect(ymap.get("state").get("foo")).toBe(1);
});
});

describe("patchStore", () =>
Expand Down
56 changes: 29 additions & 27 deletions src/patching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,38 +97,40 @@ export const patchSharedType = (
{
case "add":
case "update":
if (sharedType instanceof Y.Map)
sharedType.set(property as string, value);

else if (sharedType instanceof Y.Array)
if ((value instanceof Function) === false)
{
const index = property as number;

const left = sharedType.slice(0, index);
const right = sharedType.slice(index+1);

sharedType.delete(0, sharedType.length);
if (sharedType instanceof Y.Map)
sharedType.set(property as string, value);

if (value instanceof Array)
else if (sharedType instanceof Y.Array)
{
sharedType.insert(0, [
...left,
arrayToYArray(value),
...right
]);
const index = property as number;

const left = sharedType.slice(0, index);
const right = sharedType.slice(index+1);

sharedType.delete(0, sharedType.length);

if (value instanceof Array)
{
sharedType.insert(0, [
...left,
arrayToYArray(value),
...right
]);
}
else if (value instanceof Object)
{
sharedType.insert(0, [
...left,
objectToYMap(value),
...right
]);
}
else
sharedType.insert(0, [ ...left, value, ...right ]);
}
else if (value instanceof Object)
{
sharedType.insert(0, [
...left,
objectToYMap(value),
...right
]);
}
else
sharedType.insert(0, [ ...left, value, ...right ]);
}

break;

case "delete":
Expand Down

0 comments on commit 0b7ef25

Please sign in to comment.