Skip to content

Commit

Permalink
Add missing removedAt during Primitive deepcopy (#692)
Browse files Browse the repository at this point in the history
When performing a Primitive deepcopy, the absence of removedAt led to
a problem where deleted elements reappeared. This PR addresses the
issue by making sure that removedAt is included in the primitive
deepcopy.
  • Loading branch information
chacha912 authored Nov 23, 2023
1 parent baa2797 commit 5b1425d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/document/crdt/primitive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export class Primitive extends CRDTElement {
public deepcopy(): Primitive {
const primitive = Primitive.of(this.value, this.getCreatedAt());
primitive.setMovedAt(this.getMovedAt());
primitive.setRemovedAt(this.getRemovedAt());
return primitive;
}

Expand Down
7 changes: 6 additions & 1 deletion src/document/json/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@ export class ObjectProxy {
const primitive = Primitive.of(value as PrimitiveValue, ticket);
setAndRegister(primitive);
context.push(
SetOperation.create(key, primitive, target.getCreatedAt(), ticket),
SetOperation.create(
key,
primitive.deepcopy(),
target.getCreatedAt(),
ticket,
),
);
} else if (Array.isArray(value)) {
const array = CRDTArray.create(ticket);
Expand Down
2 changes: 1 addition & 1 deletion test/integration/object_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ describe('Object', function () {
assert.equal(doc.toSortedJSON(), `{}`);
assert.deepEqual(
doc.getRedoStackForTest().at(-1)?.map(toStringHistoryOp),
['0:00:0.SET.shape={"color":"black"}', '1:00:1.SET.color="black"'],
['0:00:0.SET.shape={}', '1:00:1.SET.color="black"'],
);

doc.history.redo();
Expand Down
21 changes: 21 additions & 0 deletions test/unit/document/document_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,27 @@ describe.sequential('Document', function () {
assert.equal(doc.toSortedJSON(), '{"list":[{"id":1}]}');
});

it('splice array with nested object', function () {
const doc = new Document<{
list: Array<{ point: { x?: number; y?: number } }>;
}>('test-doc');

doc.update((root) => {
root.list = [{ point: { x: 0, y: 0 } }, { point: { x: 1, y: 1 } }];
delete root.list[1].point.y;
});
assert.equal(
doc.toSortedJSON(),
'{"list":[{"point":{"x":0,"y":0}},{"point":{"x":1}}]}',
);

doc.update((root) => {
const res = root.list.splice(1, 1);
assert.equal(res.toString(), '{"point":{"x":1}}');
});
assert.equal(doc.toSortedJSON(), '{"list":[{"point":{"x":0,"y":0}}]}');
});

describe('should support standard array read-only operations', () => {
type TestDoc = {
empty: [];
Expand Down

0 comments on commit 5b1425d

Please sign in to comment.