@pluv/crdt-loro 0.18.0 → 0.20.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.
- package/.turbo/turbo-build.log +15 -15
- package/CHANGELOG.md +84 -0
- package/dist/index.d.mts +24 -102
- package/dist/index.d.ts +24 -102
- package/dist/index.js +89 -477
- package/dist/index.mjs +84 -472
- package/package.json +8 -8
- package/src/doc/CrdtLoroDoc.ts +43 -39
- package/src/doc/CrdtLoroDocFactory.ts +9 -16
- package/src/doc/doc.ts +2 -2
- package/src/list.ts +13 -0
- package/src/loro.ts +5 -5
- package/src/map.ts +15 -0
- package/src/object.ts +13 -0
- package/src/text.ts +10 -0
- package/src/types.ts +6 -39
- package/src/array/CrdtLoroArray.ts +0 -96
- package/src/array/array.ts +0 -5
- package/src/array/index.ts +0 -2
- package/src/map/CrdtLoroMap.ts +0 -88
- package/src/map/index.ts +0 -2
- package/src/map/map.ts +0 -5
- package/src/object/CrdtLoroObject.ts +0 -75
- package/src/object/index.ts +0 -2
- package/src/object/object.ts +0 -5
- package/src/shared/cloneType.ts +0 -234
- package/src/shared/getLoroContainerType.ts +0 -22
- package/src/shared/index.ts +0 -5
- package/src/shared/isWrapper.ts +0 -15
- package/src/shared/toLoroValue.ts +0 -5
- package/src/text/CrdtLoroText.ts +0 -68
- package/src/text/index.ts +0 -2
- package/src/text/text.ts +0 -5
package/dist/index.mjs
CHANGED
|
@@ -25,498 +25,68 @@ var __export = (target, all) => {
|
|
|
25
25
|
// src/loro.ts
|
|
26
26
|
var loro_exports = {};
|
|
27
27
|
__export(loro_exports, {
|
|
28
|
-
CrdtLoroArray: () => CrdtLoroArray,
|
|
29
28
|
CrdtLoroDoc: () => CrdtLoroDoc,
|
|
30
|
-
CrdtLoroMap: () => CrdtLoroMap,
|
|
31
|
-
CrdtLoroObject: () => CrdtLoroObject,
|
|
32
|
-
CrdtLoroText: () => CrdtLoroText,
|
|
33
|
-
array: () => array,
|
|
34
29
|
doc: () => doc,
|
|
30
|
+
list: () => list,
|
|
35
31
|
map: () => map,
|
|
36
32
|
object: () => object,
|
|
37
33
|
text: () => text
|
|
38
34
|
});
|
|
39
35
|
|
|
40
|
-
// src/array/CrdtLoroArray.ts
|
|
41
|
-
import { AbstractCrdtType as AbstractCrdtType5 } from "@pluv/crdt";
|
|
42
|
-
import { LoroList } from "loro-crdt";
|
|
43
|
-
|
|
44
|
-
// src/shared/cloneType.ts
|
|
45
|
-
import { AbstractCrdtType as AbstractCrdtType4 } from "@pluv/crdt";
|
|
46
|
-
|
|
47
|
-
// src/map/CrdtLoroMap.ts
|
|
48
|
-
import { AbstractCrdtType } from "@pluv/crdt";
|
|
49
|
-
import { LoroMap } from "loro-crdt";
|
|
50
|
-
var CrdtLoroMap = class extends AbstractCrdtType {
|
|
51
|
-
constructor(value = []) {
|
|
52
|
-
super();
|
|
53
|
-
this._doc = null;
|
|
54
|
-
this._initialized = false;
|
|
55
|
-
this._value = new LoroMap();
|
|
56
|
-
this.initialValue = value.map(([k, v]) => [k, v]);
|
|
57
|
-
}
|
|
58
|
-
set doc(doc2) {
|
|
59
|
-
if (this._doc)
|
|
60
|
-
throw new Error("Cannot overwrite array doc");
|
|
61
|
-
this._doc = doc2;
|
|
62
|
-
}
|
|
63
|
-
get size() {
|
|
64
|
-
return this.value.size;
|
|
65
|
-
}
|
|
66
|
-
get value() {
|
|
67
|
-
return this._value;
|
|
68
|
-
}
|
|
69
|
-
set value(value) {
|
|
70
|
-
if (this._initialized)
|
|
71
|
-
throw new Error("Cannot re-assign map");
|
|
72
|
-
this._initialized = true;
|
|
73
|
-
this._value = value;
|
|
74
|
-
cloneType({ source: this, target: this.value });
|
|
75
|
-
}
|
|
76
|
-
delete(prop) {
|
|
77
|
-
var _a;
|
|
78
|
-
this._guardInitialized();
|
|
79
|
-
this.value.delete(prop);
|
|
80
|
-
(_a = this._doc) == null ? void 0 : _a.value.commit();
|
|
81
|
-
return this;
|
|
82
|
-
}
|
|
83
|
-
set(prop, value) {
|
|
84
|
-
var _a, _b;
|
|
85
|
-
this._guardInitialized();
|
|
86
|
-
if (!(value instanceof AbstractCrdtType)) {
|
|
87
|
-
this.value.set(prop, value);
|
|
88
|
-
(_a = this._doc) == null ? void 0 : _a.value.commit();
|
|
89
|
-
return this;
|
|
90
|
-
}
|
|
91
|
-
if (!isWrapper(value)) {
|
|
92
|
-
throw new Error("This type is not yet supported");
|
|
93
|
-
}
|
|
94
|
-
const containerType = getLoroContainerType(value);
|
|
95
|
-
const container = this.value.setContainer(prop, containerType);
|
|
96
|
-
cloneType({ source: value, target: container });
|
|
97
|
-
if (this._doc)
|
|
98
|
-
value.doc = this._doc;
|
|
99
|
-
(_b = this._doc) == null ? void 0 : _b.value.commit();
|
|
100
|
-
return this;
|
|
101
|
-
}
|
|
102
|
-
toJson() {
|
|
103
|
-
return this.value.toJson();
|
|
104
|
-
}
|
|
105
|
-
_guardInitialized() {
|
|
106
|
-
if (!this._initialized)
|
|
107
|
-
throw new Error("Array is not yet initialized");
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
// src/map/map.ts
|
|
112
|
-
var map = (value = []) => {
|
|
113
|
-
return new CrdtLoroMap(value);
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
// src/object/CrdtLoroObject.ts
|
|
117
|
-
import { AbstractCrdtType as AbstractCrdtType2 } from "@pluv/crdt";
|
|
118
|
-
import { LoroMap as LoroMap2 } from "loro-crdt";
|
|
119
|
-
var CrdtLoroObject = class extends AbstractCrdtType2 {
|
|
120
|
-
constructor(value) {
|
|
121
|
-
super();
|
|
122
|
-
this._doc = null;
|
|
123
|
-
this._initialized = false;
|
|
124
|
-
this._value = new LoroMap2();
|
|
125
|
-
this.initialValue = Object.entries(value).map(([k, v]) => [k, v]);
|
|
126
|
-
}
|
|
127
|
-
set doc(doc2) {
|
|
128
|
-
if (this._doc)
|
|
129
|
-
throw new Error("Cannot overwrite array doc");
|
|
130
|
-
this._doc = doc2;
|
|
131
|
-
}
|
|
132
|
-
get size() {
|
|
133
|
-
return this.value.size;
|
|
134
|
-
}
|
|
135
|
-
get value() {
|
|
136
|
-
return this._value;
|
|
137
|
-
}
|
|
138
|
-
set value(value) {
|
|
139
|
-
if (this._initialized)
|
|
140
|
-
throw new Error("Cannot re-assign map");
|
|
141
|
-
this._initialized = true;
|
|
142
|
-
this._value = value;
|
|
143
|
-
cloneType({ source: this, target: this.value });
|
|
144
|
-
}
|
|
145
|
-
set(prop, value) {
|
|
146
|
-
var _a, _b;
|
|
147
|
-
this._guardInitialized();
|
|
148
|
-
if (!(value instanceof AbstractCrdtType2)) {
|
|
149
|
-
this.value.set(prop, value);
|
|
150
|
-
(_a = this._doc) == null ? void 0 : _a.value.commit();
|
|
151
|
-
return this;
|
|
152
|
-
}
|
|
153
|
-
if (!isWrapper(value)) {
|
|
154
|
-
throw new Error("This type is not yet supported");
|
|
155
|
-
}
|
|
156
|
-
const containerType = getLoroContainerType(value);
|
|
157
|
-
const container = this.value.setContainer(prop, containerType);
|
|
158
|
-
cloneType({ source: value, target: container });
|
|
159
|
-
if (this._doc)
|
|
160
|
-
value.doc = this._doc;
|
|
161
|
-
(_b = this._doc) == null ? void 0 : _b.value.commit();
|
|
162
|
-
return this;
|
|
163
|
-
}
|
|
164
|
-
toJson() {
|
|
165
|
-
return this.value.toJson();
|
|
166
|
-
}
|
|
167
|
-
_guardInitialized() {
|
|
168
|
-
if (!this._initialized)
|
|
169
|
-
throw new Error("Array is not yet initialized");
|
|
170
|
-
}
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
// src/object/object.ts
|
|
174
|
-
var object = (value) => {
|
|
175
|
-
return new CrdtLoroObject(value);
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
// src/text/CrdtLoroText.ts
|
|
179
|
-
import { AbstractCrdtType as AbstractCrdtType3 } from "@pluv/crdt";
|
|
180
|
-
import { LoroText } from "loro-crdt";
|
|
181
|
-
var CrdtLoroText = class extends AbstractCrdtType3 {
|
|
182
|
-
constructor(value = "") {
|
|
183
|
-
super();
|
|
184
|
-
this._doc = null;
|
|
185
|
-
this._initialized = false;
|
|
186
|
-
this._value = new LoroText();
|
|
187
|
-
this.initalValue = value;
|
|
188
|
-
this.value = new LoroText();
|
|
189
|
-
}
|
|
190
|
-
set doc(doc2) {
|
|
191
|
-
if (this._doc)
|
|
192
|
-
throw new Error("Cannot overwrite array doc");
|
|
193
|
-
this._doc = doc2;
|
|
194
|
-
}
|
|
195
|
-
get length() {
|
|
196
|
-
return this.value.length;
|
|
197
|
-
}
|
|
198
|
-
get value() {
|
|
199
|
-
return this._value;
|
|
200
|
-
}
|
|
201
|
-
set value(value) {
|
|
202
|
-
if (this._initialized)
|
|
203
|
-
throw new Error("Cannot re-assign text");
|
|
204
|
-
this._initialized = true;
|
|
205
|
-
this._value = value;
|
|
206
|
-
cloneType({ source: this, target: this.value });
|
|
207
|
-
}
|
|
208
|
-
delete(index, length = 1) {
|
|
209
|
-
var _a;
|
|
210
|
-
this._guardInitialized();
|
|
211
|
-
this.value.delete(index, length);
|
|
212
|
-
(_a = this._doc) == null ? void 0 : _a.value.commit();
|
|
213
|
-
return this;
|
|
214
|
-
}
|
|
215
|
-
insert(index, text2) {
|
|
216
|
-
var _a;
|
|
217
|
-
this._guardInitialized();
|
|
218
|
-
this.value.insert(index, text2);
|
|
219
|
-
(_a = this._doc) == null ? void 0 : _a.value.commit();
|
|
220
|
-
return this;
|
|
221
|
-
}
|
|
222
|
-
toJson() {
|
|
223
|
-
return this.value.toString();
|
|
224
|
-
}
|
|
225
|
-
_guardInitialized() {
|
|
226
|
-
if (!this._initialized)
|
|
227
|
-
throw new Error("Array is not yet initialized");
|
|
228
|
-
}
|
|
229
|
-
};
|
|
230
|
-
|
|
231
|
-
// src/text/text.ts
|
|
232
|
-
var text = (value = "") => {
|
|
233
|
-
return new CrdtLoroText(value);
|
|
234
|
-
};
|
|
235
|
-
|
|
236
|
-
// src/shared/cloneType.ts
|
|
237
|
-
function cloneArray(params) {
|
|
238
|
-
const { source, target } = params;
|
|
239
|
-
const items = source.initialValue;
|
|
240
|
-
items.forEach((item, i) => {
|
|
241
|
-
if (!(item instanceof AbstractCrdtType4)) {
|
|
242
|
-
target.insert(i, item);
|
|
243
|
-
return;
|
|
244
|
-
}
|
|
245
|
-
if (item instanceof CrdtLoroArray) {
|
|
246
|
-
const list = target.insertContainer(i, "List");
|
|
247
|
-
item.value = list;
|
|
248
|
-
cloneArray({ source: item, target: list });
|
|
249
|
-
return;
|
|
250
|
-
}
|
|
251
|
-
if (item instanceof CrdtLoroMap) {
|
|
252
|
-
const map2 = target.insertContainer(i, "Map");
|
|
253
|
-
item.value = map2;
|
|
254
|
-
cloneMap({ source: item, target: map2 });
|
|
255
|
-
return;
|
|
256
|
-
}
|
|
257
|
-
if (item instanceof CrdtLoroObject) {
|
|
258
|
-
const map2 = target.insertContainer(i, "Map");
|
|
259
|
-
item.value = map2;
|
|
260
|
-
cloneObject({ source: item, target: map2 });
|
|
261
|
-
return;
|
|
262
|
-
}
|
|
263
|
-
if (item instanceof CrdtLoroText) {
|
|
264
|
-
const text2 = target.insertContainer(i, "Text");
|
|
265
|
-
item.value = text2;
|
|
266
|
-
cloneText({ source: item, target: text2 });
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
throw new Error("This type is not yet supported");
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
function cloneMap(params) {
|
|
273
|
-
const { source, target } = params;
|
|
274
|
-
const items = source.initialValue;
|
|
275
|
-
items.forEach(([key, item]) => {
|
|
276
|
-
if (!(item instanceof AbstractCrdtType4)) {
|
|
277
|
-
target.set(key, item);
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
if (item instanceof CrdtLoroArray) {
|
|
281
|
-
const list = target.setContainer(key, "List");
|
|
282
|
-
item.value = list;
|
|
283
|
-
cloneArray({ source: item, target: list });
|
|
284
|
-
return;
|
|
285
|
-
}
|
|
286
|
-
if (item instanceof CrdtLoroMap) {
|
|
287
|
-
const map2 = target.setContainer(key, "Map");
|
|
288
|
-
item.value = map2;
|
|
289
|
-
cloneMap({ source: item, target: map2 });
|
|
290
|
-
return;
|
|
291
|
-
}
|
|
292
|
-
if (item instanceof CrdtLoroObject) {
|
|
293
|
-
const map2 = target.setContainer(key, "Map");
|
|
294
|
-
item.value = map2;
|
|
295
|
-
cloneObject({ source: item, target: map2 });
|
|
296
|
-
return;
|
|
297
|
-
}
|
|
298
|
-
if (item instanceof CrdtLoroText) {
|
|
299
|
-
const text2 = target.setContainer(key, "Text");
|
|
300
|
-
item.value = text2;
|
|
301
|
-
cloneText({ source: item, target: text2 });
|
|
302
|
-
return;
|
|
303
|
-
}
|
|
304
|
-
throw new Error("This type is not yet supported");
|
|
305
|
-
});
|
|
306
|
-
}
|
|
307
|
-
function cloneObject(params) {
|
|
308
|
-
const { source, target } = params;
|
|
309
|
-
const items = source.initialValue;
|
|
310
|
-
items.forEach(([key, item]) => {
|
|
311
|
-
if (!(item instanceof AbstractCrdtType4)) {
|
|
312
|
-
target.set(key, item);
|
|
313
|
-
return;
|
|
314
|
-
}
|
|
315
|
-
if (item instanceof CrdtLoroArray) {
|
|
316
|
-
const list = target.setContainer(key, "List");
|
|
317
|
-
item.value = list;
|
|
318
|
-
cloneArray({ source: item, target: list });
|
|
319
|
-
return;
|
|
320
|
-
}
|
|
321
|
-
if (item instanceof CrdtLoroMap) {
|
|
322
|
-
const map2 = target.setContainer(key, "Map");
|
|
323
|
-
item.value = map2;
|
|
324
|
-
cloneMap({ source: item, target: map2 });
|
|
325
|
-
return;
|
|
326
|
-
}
|
|
327
|
-
if (item instanceof CrdtLoroObject) {
|
|
328
|
-
const map2 = target.setContainer(key, "Map");
|
|
329
|
-
item.value = map2;
|
|
330
|
-
cloneObject({ source: item, target: map2 });
|
|
331
|
-
return;
|
|
332
|
-
}
|
|
333
|
-
if (item instanceof CrdtLoroText) {
|
|
334
|
-
const text2 = target.setContainer(key, "Text");
|
|
335
|
-
item.value = text2;
|
|
336
|
-
cloneText({ source: item, target: text2 });
|
|
337
|
-
return;
|
|
338
|
-
}
|
|
339
|
-
throw new Error("This type is not yet supported");
|
|
340
|
-
});
|
|
341
|
-
}
|
|
342
|
-
function cloneText(params) {
|
|
343
|
-
const { source, target } = params;
|
|
344
|
-
source.value = target;
|
|
345
|
-
source.insert(0, source.initalValue);
|
|
346
|
-
}
|
|
347
|
-
var cloneType = (params) => {
|
|
348
|
-
const { source, target } = params;
|
|
349
|
-
if (source instanceof CrdtLoroArray) {
|
|
350
|
-
cloneArray({ source, target });
|
|
351
|
-
return;
|
|
352
|
-
}
|
|
353
|
-
if (source instanceof CrdtLoroMap) {
|
|
354
|
-
cloneMap({ source, target });
|
|
355
|
-
return;
|
|
356
|
-
}
|
|
357
|
-
if (source instanceof CrdtLoroObject) {
|
|
358
|
-
cloneObject({ source, target });
|
|
359
|
-
return;
|
|
360
|
-
}
|
|
361
|
-
if (source instanceof CrdtLoroText) {
|
|
362
|
-
cloneText({ source, target });
|
|
363
|
-
return;
|
|
364
|
-
}
|
|
365
|
-
throw new Error("This type is not yet supported");
|
|
366
|
-
};
|
|
367
|
-
|
|
368
|
-
// src/shared/getLoroContainerType.ts
|
|
369
|
-
var getLoroContainerType = (value) => {
|
|
370
|
-
if (value instanceof CrdtLoroArray)
|
|
371
|
-
return "List";
|
|
372
|
-
if (value instanceof CrdtLoroMap)
|
|
373
|
-
return "Map";
|
|
374
|
-
if (value instanceof CrdtLoroObject)
|
|
375
|
-
return "Map";
|
|
376
|
-
if (value instanceof CrdtLoroText)
|
|
377
|
-
return "Text";
|
|
378
|
-
throw new Error("This type is not yet supported");
|
|
379
|
-
};
|
|
380
|
-
|
|
381
|
-
// src/shared/isWrapper.ts
|
|
382
|
-
var isWrapper = (item) => {
|
|
383
|
-
return item instanceof CrdtLoroArray || item instanceof CrdtLoroMap || item instanceof CrdtLoroObject || item instanceof CrdtLoroText;
|
|
384
|
-
};
|
|
385
|
-
|
|
386
|
-
// src/array/CrdtLoroArray.ts
|
|
387
|
-
var CrdtLoroArray = class extends AbstractCrdtType5 {
|
|
388
|
-
constructor(value = []) {
|
|
389
|
-
super();
|
|
390
|
-
this._doc = null;
|
|
391
|
-
this._initialized = false;
|
|
392
|
-
this._value = new LoroList();
|
|
393
|
-
this.initialValue = value.slice();
|
|
394
|
-
}
|
|
395
|
-
set doc(doc2) {
|
|
396
|
-
if (this._doc)
|
|
397
|
-
throw new Error("Cannot overwrite array doc");
|
|
398
|
-
this._doc = doc2;
|
|
399
|
-
}
|
|
400
|
-
get length() {
|
|
401
|
-
return this.value.length;
|
|
402
|
-
}
|
|
403
|
-
get value() {
|
|
404
|
-
return this._value;
|
|
405
|
-
}
|
|
406
|
-
set value(value) {
|
|
407
|
-
if (this._initialized)
|
|
408
|
-
throw new Error("Cannot re-assign array");
|
|
409
|
-
this._initialized = true;
|
|
410
|
-
this._value = value;
|
|
411
|
-
cloneType({ source: this, target: this.value });
|
|
412
|
-
}
|
|
413
|
-
delete(index, length = 1) {
|
|
414
|
-
var _a;
|
|
415
|
-
this._guardInitialized();
|
|
416
|
-
this.value.delete(index, length);
|
|
417
|
-
(_a = this._doc) == null ? void 0 : _a.value.commit();
|
|
418
|
-
return this;
|
|
419
|
-
}
|
|
420
|
-
insert(index, ...items) {
|
|
421
|
-
var _a;
|
|
422
|
-
this._guardInitialized();
|
|
423
|
-
items.forEach((item, i) => {
|
|
424
|
-
var _a2;
|
|
425
|
-
if (!(item instanceof AbstractCrdtType5)) {
|
|
426
|
-
this.value.insert(index + i, item);
|
|
427
|
-
(_a2 = this._doc) == null ? void 0 : _a2.value.commit();
|
|
428
|
-
return this;
|
|
429
|
-
}
|
|
430
|
-
if (!isWrapper(item)) {
|
|
431
|
-
throw new Error("This type is not yet supported");
|
|
432
|
-
}
|
|
433
|
-
const containerType = getLoroContainerType(item);
|
|
434
|
-
const container = this.value.insertContainer(index + i, containerType);
|
|
435
|
-
cloneType({ source: item, target: container });
|
|
436
|
-
if (this._doc)
|
|
437
|
-
item.doc = this._doc;
|
|
438
|
-
return this;
|
|
439
|
-
});
|
|
440
|
-
(_a = this._doc) == null ? void 0 : _a.value.commit();
|
|
441
|
-
return this;
|
|
442
|
-
}
|
|
443
|
-
push(...items) {
|
|
444
|
-
return this.insert(this.length, ...items);
|
|
445
|
-
}
|
|
446
|
-
unshift(...items) {
|
|
447
|
-
return this.insert(0, ...items);
|
|
448
|
-
}
|
|
449
|
-
toJson() {
|
|
450
|
-
return this.value.toJson();
|
|
451
|
-
}
|
|
452
|
-
_guardInitialized() {
|
|
453
|
-
if (!this._initialized)
|
|
454
|
-
throw new Error("Array is not yet initialized");
|
|
455
|
-
}
|
|
456
|
-
};
|
|
457
|
-
|
|
458
|
-
// src/array/array.ts
|
|
459
|
-
var array = (value = []) => {
|
|
460
|
-
return new CrdtLoroArray(value);
|
|
461
|
-
};
|
|
462
|
-
|
|
463
36
|
// src/doc/CrdtLoroDoc.ts
|
|
464
37
|
import { AbstractCrdtDoc } from "@pluv/crdt";
|
|
465
38
|
import { fromUint8Array, toUint8Array } from "js-base64";
|
|
466
|
-
import { Loro } from "loro-crdt";
|
|
39
|
+
import { Loro, LoroList, LoroMap, LoroText, isContainer } from "loro-crdt";
|
|
467
40
|
var CrdtLoroDoc = class extends AbstractCrdtDoc {
|
|
468
41
|
constructor(value = {}) {
|
|
469
42
|
super();
|
|
470
43
|
this.value = new Loro();
|
|
471
44
|
this._storage = Object.entries(value).reduce((acc, [key, node]) => {
|
|
472
|
-
if (node instanceof
|
|
473
|
-
const
|
|
474
|
-
node.
|
|
475
|
-
|
|
476
|
-
|
|
45
|
+
if (node instanceof LoroList) {
|
|
46
|
+
const container = this.value.getList(key);
|
|
47
|
+
node.toArray().forEach((item, i) => {
|
|
48
|
+
isContainer(item) ? container.insertContainer(i, item) : container.insert(i, item);
|
|
49
|
+
});
|
|
50
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: container });
|
|
477
51
|
}
|
|
478
|
-
if (node instanceof
|
|
479
|
-
const
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
52
|
+
if (node instanceof LoroMap) {
|
|
53
|
+
const container = this.value.getMap(key);
|
|
54
|
+
container.entries().forEach(([key2, item]) => {
|
|
55
|
+
isContainer(item) ? container.setContainer(key2, item) : container.set(key2, item);
|
|
56
|
+
});
|
|
57
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: container });
|
|
483
58
|
}
|
|
484
|
-
if (node instanceof
|
|
485
|
-
const
|
|
486
|
-
node.
|
|
487
|
-
|
|
488
|
-
return __spreadProps(__spreadValues({}, acc), { [key]: loroText });
|
|
59
|
+
if (node instanceof LoroText) {
|
|
60
|
+
const container = this.value.getText(key);
|
|
61
|
+
container.insert(0, node.toString());
|
|
62
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: container });
|
|
489
63
|
}
|
|
490
64
|
return acc;
|
|
491
65
|
}, {});
|
|
492
66
|
}
|
|
493
67
|
applyEncodedState(params) {
|
|
494
68
|
const update = typeof params.update === "string" ? toUint8Array(params.update) : params.update;
|
|
495
|
-
if (!update)
|
|
496
|
-
return this;
|
|
69
|
+
if (!update) return this;
|
|
497
70
|
this.value.import(update);
|
|
498
71
|
return this;
|
|
499
72
|
}
|
|
500
73
|
batchApplyEncodedState(updates) {
|
|
501
74
|
var _a;
|
|
502
75
|
const _updates = updates.reduce((acc, item) => {
|
|
503
|
-
if (!item)
|
|
504
|
-
return acc;
|
|
76
|
+
if (!item) return acc;
|
|
505
77
|
if (typeof item === "string") {
|
|
506
78
|
acc.push(toUint8Array(item));
|
|
507
79
|
return acc;
|
|
508
80
|
}
|
|
509
81
|
if (typeof item === "object") {
|
|
510
82
|
const update = typeof item.update === "string" ? toUint8Array(item.update) : item.update;
|
|
511
|
-
if (!update)
|
|
512
|
-
return acc;
|
|
83
|
+
if (!update) return acc;
|
|
513
84
|
acc.push(update);
|
|
514
85
|
return acc;
|
|
515
86
|
}
|
|
516
87
|
return acc;
|
|
517
88
|
}, []);
|
|
518
|
-
if (!_updates.length)
|
|
519
|
-
return this;
|
|
89
|
+
if (!_updates.length) return this;
|
|
520
90
|
if (_updates.length === 1) {
|
|
521
91
|
const update = (_a = _updates[0]) != null ? _a : null;
|
|
522
92
|
update && this.value.import(update);
|
|
@@ -543,21 +113,26 @@ var CrdtLoroDoc = class extends AbstractCrdtDoc {
|
|
|
543
113
|
return;
|
|
544
114
|
}
|
|
545
115
|
get(key) {
|
|
546
|
-
if (typeof key === "undefined")
|
|
547
|
-
return this._storage;
|
|
116
|
+
if (typeof key === "undefined") return this._storage;
|
|
548
117
|
return this._storage[key];
|
|
549
118
|
}
|
|
550
119
|
getEncodedState() {
|
|
551
120
|
return fromUint8Array(this.value.exportSnapshot());
|
|
552
121
|
}
|
|
553
|
-
toJson() {
|
|
122
|
+
toJson(type) {
|
|
123
|
+
if (typeof type === "string") {
|
|
124
|
+
const container = this._storage[type];
|
|
125
|
+
return container instanceof LoroText ? container.toString() : container.toJSON();
|
|
126
|
+
}
|
|
554
127
|
return Object.entries(this._storage).reduce(
|
|
555
|
-
(acc, [key, value]) => __spreadProps(__spreadValues({}, acc), {
|
|
128
|
+
(acc, [key, value]) => __spreadProps(__spreadValues({}, acc), {
|
|
129
|
+
[key]: value instanceof LoroText ? value.toString() : value.toJSON()
|
|
130
|
+
}),
|
|
556
131
|
{}
|
|
557
132
|
);
|
|
558
133
|
}
|
|
559
134
|
isEmpty() {
|
|
560
|
-
const serialized = this.value.
|
|
135
|
+
const serialized = this.value.toJSON();
|
|
561
136
|
return !serialized || !Object.keys(serialized).length;
|
|
562
137
|
}
|
|
563
138
|
/**
|
|
@@ -572,24 +147,24 @@ var CrdtLoroDoc = class extends AbstractCrdtDoc {
|
|
|
572
147
|
const update = fromUint8Array(this.value.exportFrom());
|
|
573
148
|
listener({
|
|
574
149
|
doc: this,
|
|
575
|
-
local: event.local,
|
|
150
|
+
local: event.by === "local",
|
|
576
151
|
origin: event.origin ? String(event.origin) : null,
|
|
577
152
|
update
|
|
578
153
|
});
|
|
579
154
|
};
|
|
580
155
|
const subscriptionIds = Object.entries(this._storage).reduce((map2, [key, crdtType]) => {
|
|
581
|
-
const container = crdtType
|
|
582
|
-
const subscriptionId = container.subscribe(
|
|
156
|
+
const container = crdtType;
|
|
157
|
+
const subscriptionId = container.subscribe(fn);
|
|
583
158
|
return map2.set(key, subscriptionId);
|
|
584
159
|
}, /* @__PURE__ */ new Map());
|
|
585
160
|
return () => {
|
|
586
161
|
Array.from(subscriptionIds.entries()).forEach(([key, subscriptionId]) => {
|
|
587
|
-
var _a
|
|
588
|
-
const container = (
|
|
162
|
+
var _a;
|
|
163
|
+
const container = (_a = this._storage[key]) != null ? _a : null;
|
|
589
164
|
if (!container) {
|
|
590
165
|
throw new Error("Storage could not be found");
|
|
591
166
|
}
|
|
592
|
-
container.unsubscribe(
|
|
167
|
+
container.unsubscribe(subscriptionId);
|
|
593
168
|
});
|
|
594
169
|
};
|
|
595
170
|
}
|
|
@@ -606,6 +181,7 @@ var CrdtLoroDoc = class extends AbstractCrdtDoc {
|
|
|
606
181
|
*/
|
|
607
182
|
transact(fn) {
|
|
608
183
|
fn();
|
|
184
|
+
this.value.commit();
|
|
609
185
|
return this;
|
|
610
186
|
}
|
|
611
187
|
/**
|
|
@@ -619,6 +195,7 @@ var CrdtLoroDoc = class extends AbstractCrdtDoc {
|
|
|
619
195
|
|
|
620
196
|
// src/doc/CrdtLoroDocFactory.ts
|
|
621
197
|
import { AbstractCrdtDocFactory } from "@pluv/crdt";
|
|
198
|
+
import { LoroList as LoroList2, LoroMap as LoroMap2, LoroText as LoroText2 } from "loro-crdt";
|
|
622
199
|
var CrdtLoroDocFactory = class _CrdtLoroDocFactory extends AbstractCrdtDocFactory {
|
|
623
200
|
constructor(initialStorage = () => ({})) {
|
|
624
201
|
super();
|
|
@@ -634,17 +211,14 @@ var CrdtLoroDocFactory = class _CrdtLoroDocFactory extends AbstractCrdtDocFactor
|
|
|
634
211
|
const storage = this._initialStorage();
|
|
635
212
|
return new CrdtLoroDoc(
|
|
636
213
|
Object.entries(storage).reduce((acc, [key, node]) => {
|
|
637
|
-
if (node instanceof
|
|
638
|
-
return __spreadProps(__spreadValues({}, acc), { [key]: new
|
|
639
|
-
}
|
|
640
|
-
if (node instanceof CrdtLoroMap) {
|
|
641
|
-
return __spreadProps(__spreadValues({}, acc), { [key]: new CrdtLoroMap([]) });
|
|
214
|
+
if (node instanceof LoroList2) {
|
|
215
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: new LoroList2() });
|
|
642
216
|
}
|
|
643
|
-
if (node instanceof
|
|
644
|
-
return __spreadProps(__spreadValues({}, acc), { [key]: new
|
|
217
|
+
if (node instanceof LoroMap2) {
|
|
218
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: new LoroMap2() });
|
|
645
219
|
}
|
|
646
|
-
if (node instanceof
|
|
647
|
-
return __spreadProps(__spreadValues({}, acc), { [key]: new
|
|
220
|
+
if (node instanceof LoroText2) {
|
|
221
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: new LoroText2() });
|
|
648
222
|
}
|
|
649
223
|
return acc;
|
|
650
224
|
}, {})
|
|
@@ -660,6 +234,44 @@ var CrdtLoroDocFactory = class _CrdtLoroDocFactory extends AbstractCrdtDocFactor
|
|
|
660
234
|
var doc = (value = () => ({})) => {
|
|
661
235
|
return new CrdtLoroDocFactory(value);
|
|
662
236
|
};
|
|
237
|
+
|
|
238
|
+
// src/list.ts
|
|
239
|
+
import { LoroList as LoroList3, isContainer as isContainer2 } from "loro-crdt";
|
|
240
|
+
var list = (value = []) => {
|
|
241
|
+
const container = new LoroList3();
|
|
242
|
+
value.forEach((item, i) => {
|
|
243
|
+
isContainer2(item) ? container.insertContainer(i, item) : container.insert(i, item);
|
|
244
|
+
});
|
|
245
|
+
return container;
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
// src/map.ts
|
|
249
|
+
import { LoroMap as LoroMap3, isContainer as isContainer3 } from "loro-crdt";
|
|
250
|
+
var map = (value = []) => {
|
|
251
|
+
const container = new LoroMap3();
|
|
252
|
+
value.forEach(([key, item]) => {
|
|
253
|
+
isContainer3(item) ? container.setContainer(key, item) : container.set(key, item);
|
|
254
|
+
});
|
|
255
|
+
return container;
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
// src/object.ts
|
|
259
|
+
import { LoroMap as LoroMap4, isContainer as isContainer4 } from "loro-crdt";
|
|
260
|
+
var object = (value) => {
|
|
261
|
+
const container = new LoroMap4();
|
|
262
|
+
Object.entries(value).forEach(([key, item]) => {
|
|
263
|
+
isContainer4(item) ? container.setContainer(key, item) : container.set(key, item);
|
|
264
|
+
});
|
|
265
|
+
return container;
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
// src/text.ts
|
|
269
|
+
import { LoroText as LoroText3 } from "loro-crdt";
|
|
270
|
+
var text = (value = "") => {
|
|
271
|
+
const container = new LoroText3();
|
|
272
|
+
container.insert(0, value);
|
|
273
|
+
return container;
|
|
274
|
+
};
|
|
663
275
|
export {
|
|
664
276
|
loro_exports as loro
|
|
665
277
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pluv/crdt-loro",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.0",
|
|
4
4
|
"description": "loro for @pluv/io",
|
|
5
5
|
"author": "leedavidcs",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,19 +18,19 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"js-base64": "^3.7.7",
|
|
21
|
-
"@pluv/crdt": "^0.
|
|
22
|
-
"@pluv/types": "^0.
|
|
21
|
+
"@pluv/crdt": "^0.20.0",
|
|
22
|
+
"@pluv/types": "^0.20.0"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
25
|
-
"loro-crdt": "^0.
|
|
25
|
+
"loro-crdt": "^0.16.0"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"eslint": "^8.57.0",
|
|
29
|
-
"loro-crdt": "^0.
|
|
30
|
-
"tsup": "^8.0
|
|
29
|
+
"loro-crdt": "^0.16.6",
|
|
30
|
+
"tsup": "^8.1.0",
|
|
31
31
|
"typescript": "^5.4.5",
|
|
32
|
-
"@pluv/tsconfig": "^0.
|
|
33
|
-
"eslint-config-pluv": "^0.
|
|
32
|
+
"@pluv/tsconfig": "^0.20.0",
|
|
33
|
+
"eslint-config-pluv": "^0.20.0"
|
|
34
34
|
},
|
|
35
35
|
"scripts": {
|
|
36
36
|
"build": "tsup src/index.ts --format esm,cjs --dts",
|