loro-crdt 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,293 +0,0 @@
1
- import { assertType, describe, expect, it } from "vitest";
2
- import {
3
- Delta,
4
- ListDiff,
5
- Loro,
6
- LoroEvent,
7
- LoroList,
8
- LoroMap,
9
- MapDiff as MapDiff,
10
- PrelimList,
11
- PrelimMap,
12
- PrelimText,
13
- TextDiff,
14
- Transaction,
15
- } from "../src";
16
- import { expectTypeOf } from "vitest";
17
-
18
- function assertEquals(a: any, b: any) {
19
- expect(a).toStrictEqual(b);
20
- }
21
-
22
- describe("transaction", () => {
23
- it("transaction", async () => {
24
- const loro = new Loro();
25
- const text = loro.getText("text");
26
- let count = 0;
27
- const sub = loro.subscribe(() => {
28
- count += 1;
29
- loro.unsubscribe(sub);
30
- });
31
- loro.transact((txn: Transaction) => {
32
- expect(count).toBe(0);
33
- text.insert(txn, 0, "hello world");
34
- expect(count).toBe(0);
35
- text.insert(txn, 0, "hello world");
36
- assertEquals(count, 0);
37
- });
38
- await one_ms();
39
- assertEquals(count, 1);
40
- });
41
-
42
- it("transaction origin", async () => {
43
- const loro = new Loro();
44
- const text = loro.getText("text");
45
- let count = 0;
46
- const sub = loro.subscribe((event: { origin: string }) => {
47
- count += 1;
48
- loro.unsubscribe(sub);
49
- assertEquals(event.origin, "origin");
50
- });
51
- loro.transact((txn: Transaction) => {
52
- assertEquals(count, 0);
53
- text.insert(txn, 0, "hello world");
54
- assertEquals(count, 0);
55
- text.insert(txn, 0, "hello world");
56
- assertEquals(count, 0);
57
- }, "origin");
58
- await one_ms();
59
- assertEquals(count, 1);
60
- });
61
- });
62
-
63
- describe("subscribe", () => {
64
- it("subscribe_lock", async () => {
65
- const loro = new Loro();
66
- const text = loro.getText("text");
67
- const list = loro.getList("list");
68
- let count = 0;
69
- let i = 1;
70
- const sub = loro.subscribe(() => {
71
- if (i > 0) {
72
- list.insert(loro, 0, i);
73
- i--;
74
- }
75
- count += 1;
76
- });
77
- text.insert(loro, 0, "hello world");
78
- await one_ms();
79
- assertEquals(count, 2);
80
- text.insert(loro, 0, "hello world");
81
- await one_ms();
82
- assertEquals(count, 3);
83
- loro.unsubscribe(sub);
84
- text.insert(loro, 0, "hello world");
85
- await one_ms();
86
- assertEquals(count, 3);
87
- });
88
-
89
- it("subscribe_lock2", async () => {
90
- const loro = new Loro();
91
- const text = loro.getText("text");
92
- let count = 0;
93
- const sub = loro.subscribe(() => {
94
- count += 1;
95
- loro.unsubscribe(sub);
96
- });
97
- assertEquals(count, 0);
98
- text.insert(loro, 0, "hello world");
99
- await one_ms();
100
- assertEquals(count, 1);
101
- text.insert(loro, 0, "hello world");
102
- assertEquals(count, 1);
103
- });
104
-
105
- it("subscribe", async () => {
106
- const loro = new Loro();
107
- const text = loro.getText("text");
108
- let count = 0;
109
- const sub = loro.subscribe(() => {
110
- count += 1;
111
- });
112
- text.insert(loro, 0, "hello world");
113
- await one_ms();
114
- assertEquals(count, 1);
115
- text.insert(loro, 0, "hello world");
116
- await one_ms();
117
- assertEquals(count, 2);
118
- loro.unsubscribe(sub);
119
- text.insert(loro, 0, "hello world");
120
- await one_ms();
121
- assertEquals(count, 2);
122
- });
123
- });
124
-
125
- describe("sync", () => {
126
- it("two insert at beginning", async () => {
127
- const a = new Loro();
128
- const b = new Loro();
129
- let a_version: undefined | Uint8Array = undefined;
130
- let b_version: undefined | Uint8Array = undefined;
131
- a.subscribe((e: { local: boolean }) => {
132
- if (e.local) {
133
- const exported = a.exportFrom(a_version);
134
- b.import(exported);
135
- a_version = a.version();
136
- }
137
- });
138
- b.subscribe((e: { local: boolean }) => {
139
- if (e.local) {
140
- const exported = b.exportFrom(b_version);
141
- a.import(exported);
142
- b_version = b.version();
143
- }
144
- });
145
- const aText = a.getText("text");
146
- const bText = b.getText("text");
147
- aText.insert(a, 0, "abc");
148
- await one_ms();
149
- assertEquals(aText.toString(), bText.toString());
150
- });
151
-
152
- it("sync", () => {
153
- const loro = new Loro();
154
- const text = loro.getText("text");
155
- text.insert(loro, 0, "hello world");
156
- const loro_bk = new Loro();
157
- loro_bk.import(loro.exportFrom(undefined));
158
- assertEquals(loro_bk.toJson(), loro.toJson());
159
- const text_bk = loro_bk.getText("text");
160
- assertEquals(text_bk.toString(), "hello world");
161
- text_bk.insert(loro_bk, 0, "a ");
162
- loro.import(loro_bk.exportFrom(undefined));
163
- assertEquals(text.toString(), "a hello world");
164
- const map = loro.getMap("map");
165
- map.set(loro, "key", "value");
166
- });
167
- });
168
-
169
- describe("prelim", () => {
170
- it("test prelim", async (t) => {
171
- const loro = new Loro();
172
- const map = loro.getMap("map");
173
- const list = loro.getList("list");
174
- const prelim_text = new PrelimText(undefined);
175
- const prelim_map = new PrelimMap({ a: 1, b: 2 });
176
- const prelim_list = new PrelimList([1, "2", { a: 4 }]);
177
-
178
- it("prelim text", () => {
179
- prelim_text.insert(0, "hello world");
180
- assertEquals(prelim_text.value, "hello world");
181
- prelim_text.delete(6, 5);
182
- prelim_text.insert(6, "everyone");
183
- assertEquals(prelim_text.value, "hello everyone");
184
- });
185
-
186
- it("prelim map", () => {
187
- prelim_map.set("ab", 123);
188
- assertEquals(prelim_map.value, { a: 1, b: 2, ab: 123 });
189
- prelim_map.delete("b");
190
- assertEquals(prelim_map.value, { a: 1, ab: 123 });
191
- });
192
-
193
- it("prelim list", () => {
194
- prelim_list.insert(0, 0);
195
- assertEquals(prelim_list.value, [0, 1, "2", { a: 4 }]);
196
- prelim_list.delete(1, 2);
197
- assertEquals(prelim_list.value, [0, { a: 4 }]);
198
- });
199
-
200
- it("prelim map integrate", () => {
201
- map.set(loro, "text", prelim_text);
202
- map.set(loro, "map", prelim_map);
203
- map.set(loro, "list", prelim_list);
204
- assertEquals(map.getValueDeep(loro), {
205
- text: "hello everyone",
206
- map: { a: 1, ab: 123 },
207
- list: [0, { a: 4 }],
208
- });
209
- });
210
-
211
- it("prelim list integrate", () => {
212
- const prelim_text = new PrelimText("ttt");
213
- const prelim_map = new PrelimMap({ a: 1, b: 2 });
214
- const prelim_list = new PrelimList([1, "2", { a: 4 }]);
215
- list.insert(loro, 0, prelim_text);
216
- list.insert(loro, 1, prelim_map);
217
- list.insert(loro, 2, prelim_list);
218
- assertEquals(list.getValueDeep(loro), ["ttt", { a: 1, b: 2 }, [1, "2", {
219
- a: 4,
220
- }]]);
221
- });
222
- });
223
- });
224
-
225
- describe("wasm", () => {
226
- const loro = new Loro();
227
- const a = loro.getText("ha");
228
- a.insert(loro, 0, "hello world");
229
- a.delete(loro, 6, 5);
230
- a.insert(loro, 6, "everyone");
231
- const b = loro.getMap("ha");
232
- b.set(loro, "ab", 123);
233
-
234
- const bText = b.insertContainer(loro, "hh", "Text");
235
-
236
- it("map get", () => {
237
- assertEquals(b.get("ab"), 123);
238
- });
239
-
240
- it("getValueDeep", () => {
241
- bText.insert(loro, 0, "hello world Text");
242
- assertEquals(b.getValueDeep(loro), { ab: 123, hh: "hello world Text" });
243
- });
244
-
245
- it("should throw error when using the wrong context", () => {
246
- expect(() => {
247
- const loro2 = new Loro();
248
- bText.insert(loro2, 0, "hello world Text");
249
- }).toThrow();
250
- });
251
-
252
- it("get container by id", () => {
253
- const id = b.id;
254
- const b2 = loro.getContainerById(id) as LoroMap;
255
- assertEquals(b2.value, b.value);
256
- assertEquals(b2.id, id);
257
- b2.set(loro, "0", 12);
258
- assertEquals(b2.value, b.value);
259
- });
260
- });
261
-
262
- describe("type", () => {
263
- it("test map type", () => {
264
- const loro = new Loro<{ map: LoroMap<{ name: "he" }> }>();
265
- const map = loro.getTypedMap("map");
266
- const v = map.getTyped(loro, "name");
267
- expectTypeOf(v).toEqualTypeOf<"he">();
268
- });
269
-
270
- it("test recursive map type", () => {
271
- const loro = new Loro<{ map: LoroMap<{ map: LoroMap<{ name: "he" }> }> }>();
272
- const map = loro.getTypedMap("map");
273
- map.insertContainer(loro, "map", "Map");
274
- const subMap = map.getTyped(loro, "map");
275
- const name = subMap.getTyped(loro, "name");
276
- expectTypeOf(name).toEqualTypeOf<"he">();
277
- });
278
-
279
- it("works for list type", () => {
280
- const loro = new Loro<{ list: LoroList<[string, number]> }>();
281
- const list = loro.getTypedList("list");
282
- list.insertTyped(loro, 0, "123");
283
- list.insertTyped(loro, 1, 123);
284
- const v0 = list.getTyped(loro, 0);
285
- expectTypeOf(v0).toEqualTypeOf<string>();
286
- const v1 = list.getTyped(loro, 1);
287
- expectTypeOf(v1).toEqualTypeOf<number>();
288
- });
289
- });
290
-
291
- function one_ms(): Promise<void> {
292
- return new Promise((resolve) => setTimeout(resolve, 1));
293
- }