Skip to content

Commit 37c2a17

Browse files
authored
fix: checkout should renew txn if not detached (#716)
* fix: checkout should renew txn if not detached * fix: should not renew id if not using detached editing * ci: rm .only test
1 parent 3213751 commit 37c2a17

3 files changed

Lines changed: 79 additions & 48 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"loro-crdt": patch
3+
---
4+
5+
fix: checkout should renew txn if not detached

crates/loro-internal/src/loro.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,8 @@ impl LoroDoc {
12931293
if self.config.detached_editing() {
12941294
self.renew_peer_id();
12951295
self.renew_txn_if_auto_commit(options);
1296+
} else if !self.is_detached() {
1297+
self.renew_txn_if_auto_commit(options);
12961298
}
12971299

12981300
Ok(())
@@ -1329,6 +1331,7 @@ impl LoroDoc {
13291331
} else {
13301332
frontiers.clone()
13311333
};
1334+
13321335
if from_frontiers == frontiers {
13331336
return Ok(());
13341337
}

crates/loro-wasm/tests/basic.test.ts

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,11 @@ it("can set next commit options", () => {
615615
const doc = new LoroDoc();
616616
doc.setPeerId("1");
617617
doc.getText("text").insert(0, "123");
618-
doc.setNextCommitOptions({ message: "test message", origin: "test origin", timestamp: 123 });
618+
doc.setNextCommitOptions({
619+
message: "test message",
620+
origin: "test origin",
621+
timestamp: 123,
622+
});
619623
doc.commit();
620624
const change = doc.getChangeAt({ peer: "1", counter: 0 });
621625
expect(change.message).toBe("test message");
@@ -632,7 +636,7 @@ it("can set next commit origin", async () => {
632636
doc.getText("text").insert(0, "123");
633637
doc.setNextCommitOrigin("test origin");
634638
doc.commit();
635-
await Promise.resolve()
639+
await Promise.resolve();
636640
expect(eventOrigin).toBe("test origin");
637641
});
638642

@@ -650,7 +654,11 @@ it("can clear next commit options", () => {
650654
const doc = new LoroDoc();
651655
doc.setPeerId("1");
652656
doc.getText("text").insert(0, "123");
653-
doc.setNextCommitOptions({ message: "test message", origin: "test origin", timestamp: 123 });
657+
doc.setNextCommitOptions({
658+
message: "test message",
659+
origin: "test origin",
660+
timestamp: 123,
661+
});
654662
doc.clearNextCommitOptions();
655663
doc.commit();
656664
const change = doc.getChangeAt({ peer: "1", counter: 0 });
@@ -1347,13 +1355,13 @@ it("diff two version with serialization", () => {
13471355
text.insert(0, "Hello");
13481356
doc.commit();
13491357
const diff = doc.diff([], doc.frontiers(), true);
1350-
expectTypeOf(diff).toEqualTypeOf<[ContainerID, JsonDiff][]>()
1358+
expectTypeOf(diff).toEqualTypeOf<[ContainerID, JsonDiff][]>();
13511359
const newDiff = JSON.parse(JSON.stringify(diff));
13521360
console.dir(newDiff, { depth: 100 });
13531361
const newDoc = new LoroDoc();
13541362
newDoc.applyDiff(newDiff);
13551363
expect(newDoc.toJSON()).toStrictEqual(doc.toJSON());
1356-
})
1364+
});
13571365

13581366
it("apply diff without for_json should work", () => {
13591367
const doc = new LoroDoc();
@@ -1365,7 +1373,7 @@ it("apply diff without for_json should work", () => {
13651373
const newDoc = new LoroDoc();
13661374
newDoc.applyDiff(diff);
13671375
expect(newDoc.toJSON()).toStrictEqual(doc.toJSON());
1368-
})
1376+
});
13691377

13701378
it("the diff will deduplication", () => {
13711379
const doc = new LoroDoc();
@@ -1495,13 +1503,13 @@ it("should return map for get_path_by_str", () => {
14951503
const child = root.createNode();
14961504
const grandChild = child.createNode();
14971505
grandChild.data.set("type", "grandChild");
1498-
console.log(doc.getByPath("tree/0/0/0"))
1499-
expect(isContainer(doc.getByPath("tree/0/0/0"))).toBe(true)
1506+
console.log(doc.getByPath("tree/0/0/0"));
1507+
expect(isContainer(doc.getByPath("tree/0/0/0"))).toBe(true);
15001508
expect((doc.getByPath("tree/0/0/0") as LoroMap).toJSON()).toStrictEqual({
1501-
"type": "grandChild"
1502-
})
1503-
expect(doc.getByPath("tree/0/0/0/type")).toBe("grandChild")
1504-
})
1509+
type: "grandChild",
1510+
});
1511+
expect(doc.getByPath("tree/0/0/0/type")).toBe("grandChild");
1512+
});
15051513

15061514
it("test container existence", () => {
15071515
const doc = new LoroDoc();
@@ -1517,23 +1525,21 @@ it("test container existence", () => {
15171525
expect(doc2.hasContainer("cid:root-map:Map")).toBe(true);
15181526
expect(doc2.hasContainer("cid:0@1:Text")).toBe(true);
15191527
expect(doc2.hasContainer("cid:1@1:List")).toBe(true);
1520-
})
1521-
1522-
1528+
});
15231529

15241530
it("text mark on LoroText", () => {
15251531
const text = new LoroText();
15261532
text.insert(0, "Hello");
15271533
text.mark({ start: 0, end: 5 }, "bold", true);
1528-
})
1534+
});
15291535

15301536
it("call toDelta on detached text", () => {
15311537
const text = new LoroText();
15321538
text.insert(0, "Hello");
15331539
text.mark({ start: 0, end: 5 }, "bold", true);
15341540
const d = text.toDelta();
15351541
expect(d).toMatchSnapshot();
1536-
})
1542+
});
15371543

15381544
it("can allow default config for text style", () => {
15391545
{
@@ -1542,11 +1548,11 @@ it("can allow default config for text style", () => {
15421548
text.insert(0, "Hello");
15431549
expect(() => {
15441550
text.mark({ start: 0, end: 5 }, "size", true);
1545-
}).toThrow()
1551+
}).toThrow();
15461552
}
15471553
{
15481554
const doc = new LoroDoc();
1549-
doc.configDefaultTextStyle({ expand: "before" })
1555+
doc.configDefaultTextStyle({ expand: "before" });
15501556
const text = doc.getText("text");
15511557
text.insert(0, "Hello");
15521558
text.mark({ start: 0, end: 5 }, "size", true);
@@ -1556,41 +1562,43 @@ it("can allow default config for text style", () => {
15561562
text.insert(0, "Hello");
15571563
text.mark({ start: 0, end: 5 }, "size", true);
15581564
}
1559-
})
1565+
});
15601566

15611567
it("can get pending ops as json", () => {
15621568
const doc = new LoroDoc();
15631569
doc.setPeerId("1");
15641570
expect(doc.getUncommittedOpsAsJson()).toBeUndefined();
15651571
const text = doc.getText("text");
15661572
text.insert(0, "Hello");
1567-
const pendingOps = doc.getUncommittedOpsAsJson()
1573+
const pendingOps = doc.getUncommittedOpsAsJson();
15681574
expect(pendingOps).toBeDefined();
15691575
expect(JSON.stringify(pendingOps)).toContain("insert");
15701576
expect(JSON.stringify(pendingOps)).toContain("Hello");
15711577
expect(pendingOps).toEqual({
1572-
"peers": undefined,
1573-
"schema_version": 1,
1574-
"start_version": {},
1575-
changes: [{
1576-
"id": "0@1",
1577-
"deps": [],
1578-
"msg": undefined,
1579-
"lamport": 0,
1580-
"ops": [
1581-
{
1582-
"container": "cid:root-text:Text",
1583-
"counter": 0,
1584-
"content": {
1585-
"type": "insert",
1586-
"pos": 0,
1587-
"text": "Hello"
1588-
}
1589-
}
1590-
],
1591-
"timestamp": 0,
1592-
}],
1593-
})
1578+
peers: undefined,
1579+
schema_version: 1,
1580+
start_version: {},
1581+
changes: [
1582+
{
1583+
id: "0@1",
1584+
deps: [],
1585+
msg: undefined,
1586+
lamport: 0,
1587+
ops: [
1588+
{
1589+
container: "cid:root-text:Text",
1590+
counter: 0,
1591+
content: {
1592+
type: "insert",
1593+
pos: 0,
1594+
text: "Hello",
1595+
},
1596+
},
1597+
],
1598+
timestamp: 0,
1599+
},
1600+
],
1601+
});
15941602
});
15951603

15961604
it("deleteRootContainers", () => {
@@ -1603,15 +1611,15 @@ it("deleteRootContainers", () => {
16031611
doc.deleteRootContainer("cid:root-text:Text");
16041612

16051613
expect(doc.toJSON()).toStrictEqual({
1606-
m: {}
1614+
m: {},
16071615
});
16081616

16091617
const snapshot = doc.export({ mode: "snapshot" });
16101618
const newDoc = new LoroDoc();
16111619
newDoc.import(snapshot);
16121620

16131621
expect(newDoc.toJSON()).toStrictEqual({
1614-
m: {}
1622+
m: {},
16151623
});
16161624
});
16171625

@@ -1628,9 +1636,24 @@ it("fromShallowSnapshot", () => {
16281636
doc.setPeerId("1");
16291637
doc.getText("text").insert(0, "Hello");
16301638
doc.commit();
1631-
const snapshot = doc.export({ mode: "shallow-snapshot", frontiers: doc.frontiers() });
1639+
const snapshot = doc.export({
1640+
mode: "shallow-snapshot",
1641+
frontiers: doc.frontiers(),
1642+
});
16321643
const newDoc = LoroDoc.fromSnapshot(snapshot);
16331644
expect(newDoc.toJSON()).toStrictEqual({
1634-
text: "Hello"
1645+
text: "Hello",
16351646
});
1636-
})
1647+
});
1648+
1649+
it("update text after switching to a version", () => {
1650+
const doc = new LoroDoc();
1651+
doc.setPeerId(1);
1652+
doc.getText("text").insert(0, "abc");
1653+
const bytes = doc.export({ mode: "snapshot" });
1654+
const newDoc = new LoroDoc();
1655+
newDoc.import(bytes);
1656+
newDoc.checkout([{ peer: "1", counter: 2 }]);
1657+
newDoc.setDetachedEditing(true);
1658+
newDoc.getText("text").update("123");
1659+
});

0 commit comments

Comments
 (0)