@pluv/crdt-loro 0.19.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/dist/index.mjs CHANGED
@@ -25,448 +25,41 @@ 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) throw new Error("Cannot overwrite array doc");
60
- this._doc = doc2;
61
- }
62
- get size() {
63
- return this.value.size;
64
- }
65
- get value() {
66
- return this._value;
67
- }
68
- set value(value) {
69
- if (this._initialized) throw new Error("Cannot re-assign map");
70
- this._initialized = true;
71
- this._value = value;
72
- cloneType({ source: this, target: this.value });
73
- }
74
- delete(prop) {
75
- var _a;
76
- this._guardInitialized();
77
- this.value.delete(prop);
78
- (_a = this._doc) == null ? void 0 : _a.value.commit();
79
- return this;
80
- }
81
- set(prop, value) {
82
- var _a, _b;
83
- this._guardInitialized();
84
- if (!(value instanceof AbstractCrdtType)) {
85
- this.value.set(prop, value);
86
- (_a = this._doc) == null ? void 0 : _a.value.commit();
87
- return this;
88
- }
89
- if (!isWrapper(value)) {
90
- throw new Error("This type is not yet supported");
91
- }
92
- const containerType = getLoroContainerType(value);
93
- const container = this.value.setContainer(prop, containerType);
94
- cloneType({ source: value, target: container });
95
- if (this._doc) value.doc = this._doc;
96
- (_b = this._doc) == null ? void 0 : _b.value.commit();
97
- return this;
98
- }
99
- toJson() {
100
- return this.value.toJson();
101
- }
102
- _guardInitialized() {
103
- if (!this._initialized) throw new Error("Array is not yet initialized");
104
- }
105
- };
106
-
107
- // src/map/map.ts
108
- var map = (value = []) => {
109
- return new CrdtLoroMap(value);
110
- };
111
-
112
- // src/object/CrdtLoroObject.ts
113
- import { AbstractCrdtType as AbstractCrdtType2 } from "@pluv/crdt";
114
- import { LoroMap as LoroMap2 } from "loro-crdt";
115
- var CrdtLoroObject = class extends AbstractCrdtType2 {
116
- constructor(value) {
117
- super();
118
- this._doc = null;
119
- this._initialized = false;
120
- this._value = new LoroMap2();
121
- this.initialValue = Object.entries(value).map(([k, v]) => [k, v]);
122
- }
123
- set doc(doc2) {
124
- if (this._doc) throw new Error("Cannot overwrite array doc");
125
- this._doc = doc2;
126
- }
127
- get size() {
128
- return this.value.size;
129
- }
130
- get value() {
131
- return this._value;
132
- }
133
- set value(value) {
134
- if (this._initialized) throw new Error("Cannot re-assign map");
135
- this._initialized = true;
136
- this._value = value;
137
- cloneType({ source: this, target: this.value });
138
- }
139
- set(prop, value) {
140
- var _a, _b;
141
- this._guardInitialized();
142
- if (!(value instanceof AbstractCrdtType2)) {
143
- this.value.set(prop, value);
144
- (_a = this._doc) == null ? void 0 : _a.value.commit();
145
- return this;
146
- }
147
- if (!isWrapper(value)) {
148
- throw new Error("This type is not yet supported");
149
- }
150
- const containerType = getLoroContainerType(value);
151
- const container = this.value.setContainer(prop, containerType);
152
- cloneType({ source: value, target: container });
153
- if (this._doc) value.doc = this._doc;
154
- (_b = this._doc) == null ? void 0 : _b.value.commit();
155
- return this;
156
- }
157
- toJson() {
158
- return this.value.toJson();
159
- }
160
- _guardInitialized() {
161
- if (!this._initialized) throw new Error("Array is not yet initialized");
162
- }
163
- };
164
-
165
- // src/object/object.ts
166
- var object = (value) => {
167
- return new CrdtLoroObject(value);
168
- };
169
-
170
- // src/text/CrdtLoroText.ts
171
- import { AbstractCrdtType as AbstractCrdtType3 } from "@pluv/crdt";
172
- import { LoroText } from "loro-crdt";
173
- var CrdtLoroText = class extends AbstractCrdtType3 {
174
- constructor(value = "") {
175
- super();
176
- this._doc = null;
177
- this._initialized = false;
178
- this._value = new LoroText();
179
- this.initalValue = value;
180
- this.value = new LoroText();
181
- }
182
- set doc(doc2) {
183
- if (this._doc) throw new Error("Cannot overwrite array doc");
184
- this._doc = doc2;
185
- }
186
- get length() {
187
- return this.value.length;
188
- }
189
- get value() {
190
- return this._value;
191
- }
192
- set value(value) {
193
- if (this._initialized) throw new Error("Cannot re-assign text");
194
- this._initialized = true;
195
- this._value = value;
196
- cloneType({ source: this, target: this.value });
197
- }
198
- delete(index, length = 1) {
199
- var _a;
200
- this._guardInitialized();
201
- this.value.delete(index, length);
202
- (_a = this._doc) == null ? void 0 : _a.value.commit();
203
- return this;
204
- }
205
- insert(index, text2) {
206
- var _a;
207
- this._guardInitialized();
208
- this.value.insert(index, text2);
209
- (_a = this._doc) == null ? void 0 : _a.value.commit();
210
- return this;
211
- }
212
- toJson() {
213
- return this.value.toString();
214
- }
215
- _guardInitialized() {
216
- if (!this._initialized) throw new Error("Array is not yet initialized");
217
- }
218
- };
219
-
220
- // src/text/text.ts
221
- var text = (value = "") => {
222
- return new CrdtLoroText(value);
223
- };
224
-
225
- // src/shared/cloneType.ts
226
- function cloneArray(params) {
227
- const { source, target } = params;
228
- const items = source.initialValue;
229
- items.forEach((item, i) => {
230
- if (!(item instanceof AbstractCrdtType4)) {
231
- target.insert(i, item);
232
- return;
233
- }
234
- if (item instanceof CrdtLoroArray) {
235
- const list = target.insertContainer(i, "List");
236
- item.value = list;
237
- cloneArray({ source: item, target: list });
238
- return;
239
- }
240
- if (item instanceof CrdtLoroMap) {
241
- const map2 = target.insertContainer(i, "Map");
242
- item.value = map2;
243
- cloneMap({ source: item, target: map2 });
244
- return;
245
- }
246
- if (item instanceof CrdtLoroObject) {
247
- const map2 = target.insertContainer(i, "Map");
248
- item.value = map2;
249
- cloneObject({ source: item, target: map2 });
250
- return;
251
- }
252
- if (item instanceof CrdtLoroText) {
253
- const text2 = target.insertContainer(i, "Text");
254
- item.value = text2;
255
- cloneText({ source: item, target: text2 });
256
- return;
257
- }
258
- throw new Error("This type is not yet supported");
259
- });
260
- }
261
- function cloneMap(params) {
262
- const { source, target } = params;
263
- const items = source.initialValue;
264
- items.forEach(([key, item]) => {
265
- if (!(item instanceof AbstractCrdtType4)) {
266
- target.set(key, item);
267
- return;
268
- }
269
- if (item instanceof CrdtLoroArray) {
270
- const list = target.setContainer(key, "List");
271
- item.value = list;
272
- cloneArray({ source: item, target: list });
273
- return;
274
- }
275
- if (item instanceof CrdtLoroMap) {
276
- const map2 = target.setContainer(key, "Map");
277
- item.value = map2;
278
- cloneMap({ source: item, target: map2 });
279
- return;
280
- }
281
- if (item instanceof CrdtLoroObject) {
282
- const map2 = target.setContainer(key, "Map");
283
- item.value = map2;
284
- cloneObject({ source: item, target: map2 });
285
- return;
286
- }
287
- if (item instanceof CrdtLoroText) {
288
- const text2 = target.setContainer(key, "Text");
289
- item.value = text2;
290
- cloneText({ source: item, target: text2 });
291
- return;
292
- }
293
- throw new Error("This type is not yet supported");
294
- });
295
- }
296
- function cloneObject(params) {
297
- const { source, target } = params;
298
- const items = source.initialValue;
299
- items.forEach(([key, item]) => {
300
- if (!(item instanceof AbstractCrdtType4)) {
301
- target.set(key, item);
302
- return;
303
- }
304
- if (item instanceof CrdtLoroArray) {
305
- const list = target.setContainer(key, "List");
306
- item.value = list;
307
- cloneArray({ source: item, target: list });
308
- return;
309
- }
310
- if (item instanceof CrdtLoroMap) {
311
- const map2 = target.setContainer(key, "Map");
312
- item.value = map2;
313
- cloneMap({ source: item, target: map2 });
314
- return;
315
- }
316
- if (item instanceof CrdtLoroObject) {
317
- const map2 = target.setContainer(key, "Map");
318
- item.value = map2;
319
- cloneObject({ source: item, target: map2 });
320
- return;
321
- }
322
- if (item instanceof CrdtLoroText) {
323
- const text2 = target.setContainer(key, "Text");
324
- item.value = text2;
325
- cloneText({ source: item, target: text2 });
326
- return;
327
- }
328
- throw new Error("This type is not yet supported");
329
- });
330
- }
331
- function cloneText(params) {
332
- const { source, target } = params;
333
- source.value = target;
334
- source.insert(0, source.initalValue);
335
- }
336
- var cloneType = (params) => {
337
- const { source, target } = params;
338
- if (source instanceof CrdtLoroArray) {
339
- cloneArray({ source, target });
340
- return;
341
- }
342
- if (source instanceof CrdtLoroMap) {
343
- cloneMap({ source, target });
344
- return;
345
- }
346
- if (source instanceof CrdtLoroObject) {
347
- cloneObject({ source, target });
348
- return;
349
- }
350
- if (source instanceof CrdtLoroText) {
351
- cloneText({ source, target });
352
- return;
353
- }
354
- throw new Error("This type is not yet supported");
355
- };
356
-
357
- // src/shared/getLoroContainerType.ts
358
- var getLoroContainerType = (value) => {
359
- if (value instanceof CrdtLoroArray) return "List";
360
- if (value instanceof CrdtLoroMap) return "Map";
361
- if (value instanceof CrdtLoroObject) return "Map";
362
- if (value instanceof CrdtLoroText) return "Text";
363
- throw new Error("This type is not yet supported");
364
- };
365
-
366
- // src/shared/isWrapper.ts
367
- var isWrapper = (item) => {
368
- return item instanceof CrdtLoroArray || item instanceof CrdtLoroMap || item instanceof CrdtLoroObject || item instanceof CrdtLoroText;
369
- };
370
-
371
- // src/array/CrdtLoroArray.ts
372
- var CrdtLoroArray = class extends AbstractCrdtType5 {
373
- constructor(value = []) {
374
- super();
375
- this._doc = null;
376
- this._initialized = false;
377
- this._value = new LoroList();
378
- this.initialValue = value.slice();
379
- }
380
- set doc(doc2) {
381
- if (this._doc) throw new Error("Cannot overwrite array doc");
382
- this._doc = doc2;
383
- }
384
- get length() {
385
- return this.value.length;
386
- }
387
- get value() {
388
- return this._value;
389
- }
390
- set value(value) {
391
- if (this._initialized) throw new Error("Cannot re-assign array");
392
- this._initialized = true;
393
- this._value = value;
394
- cloneType({ source: this, target: this.value });
395
- }
396
- delete(index, length = 1) {
397
- var _a;
398
- this._guardInitialized();
399
- this.value.delete(index, length);
400
- (_a = this._doc) == null ? void 0 : _a.value.commit();
401
- return this;
402
- }
403
- insert(index, ...items) {
404
- var _a;
405
- this._guardInitialized();
406
- items.forEach((item, i) => {
407
- var _a2;
408
- if (!(item instanceof AbstractCrdtType5)) {
409
- this.value.insert(index + i, item);
410
- (_a2 = this._doc) == null ? void 0 : _a2.value.commit();
411
- return this;
412
- }
413
- if (!isWrapper(item)) {
414
- throw new Error("This type is not yet supported");
415
- }
416
- const containerType = getLoroContainerType(item);
417
- const container = this.value.insertContainer(index + i, containerType);
418
- cloneType({ source: item, target: container });
419
- if (this._doc) item.doc = this._doc;
420
- return this;
421
- });
422
- (_a = this._doc) == null ? void 0 : _a.value.commit();
423
- return this;
424
- }
425
- push(...items) {
426
- return this.insert(this.length, ...items);
427
- }
428
- unshift(...items) {
429
- return this.insert(0, ...items);
430
- }
431
- toJson() {
432
- return this.value.toJson();
433
- }
434
- _guardInitialized() {
435
- if (!this._initialized) throw new Error("Array is not yet initialized");
436
- }
437
- };
438
-
439
- // src/array/array.ts
440
- var array = (value = []) => {
441
- return new CrdtLoroArray(value);
442
- };
443
-
444
36
  // src/doc/CrdtLoroDoc.ts
445
37
  import { AbstractCrdtDoc } from "@pluv/crdt";
446
38
  import { fromUint8Array, toUint8Array } from "js-base64";
447
- import { Loro } from "loro-crdt";
39
+ import { Loro, LoroList, LoroMap, LoroText, isContainer } from "loro-crdt";
448
40
  var CrdtLoroDoc = class extends AbstractCrdtDoc {
449
41
  constructor(value = {}) {
450
42
  super();
451
43
  this.value = new Loro();
452
44
  this._storage = Object.entries(value).reduce((acc, [key, node]) => {
453
- if (node instanceof CrdtLoroArray) {
454
- const loroList = this.value.getList(key);
455
- node.value = loroList;
456
- node.doc = this;
457
- return __spreadProps(__spreadValues({}, acc), { [key]: node });
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 });
458
51
  }
459
- if (node instanceof CrdtLoroMap || node instanceof CrdtLoroObject) {
460
- const loroMap = this.value.getMap(key);
461
- node.value = loroMap;
462
- node.doc = this;
463
- return __spreadProps(__spreadValues({}, acc), { [key]: node });
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 });
464
58
  }
465
- if (node instanceof CrdtLoroText) {
466
- const loroText = this.value.getText(key);
467
- node.value = loroText;
468
- node.doc = this;
469
- 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 });
470
63
  }
471
64
  return acc;
472
65
  }, {});
@@ -526,14 +119,20 @@ var CrdtLoroDoc = class extends AbstractCrdtDoc {
526
119
  getEncodedState() {
527
120
  return fromUint8Array(this.value.exportSnapshot());
528
121
  }
529
- 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
+ }
530
127
  return Object.entries(this._storage).reduce(
531
- (acc, [key, value]) => __spreadProps(__spreadValues({}, acc), { [key]: value.toJson() }),
128
+ (acc, [key, value]) => __spreadProps(__spreadValues({}, acc), {
129
+ [key]: value instanceof LoroText ? value.toString() : value.toJSON()
130
+ }),
532
131
  {}
533
132
  );
534
133
  }
535
134
  isEmpty() {
536
- const serialized = this.value.toJson();
135
+ const serialized = this.value.toJSON();
537
136
  return !serialized || !Object.keys(serialized).length;
538
137
  }
539
138
  /**
@@ -548,24 +147,24 @@ var CrdtLoroDoc = class extends AbstractCrdtDoc {
548
147
  const update = fromUint8Array(this.value.exportFrom());
549
148
  listener({
550
149
  doc: this,
551
- local: event.local,
150
+ local: event.by === "local",
552
151
  origin: event.origin ? String(event.origin) : null,
553
152
  update
554
153
  });
555
154
  };
556
155
  const subscriptionIds = Object.entries(this._storage).reduce((map2, [key, crdtType]) => {
557
- const container = crdtType.value;
558
- const subscriptionId = container.subscribe(this.value, fn);
156
+ const container = crdtType;
157
+ const subscriptionId = container.subscribe(fn);
559
158
  return map2.set(key, subscriptionId);
560
159
  }, /* @__PURE__ */ new Map());
561
160
  return () => {
562
161
  Array.from(subscriptionIds.entries()).forEach(([key, subscriptionId]) => {
563
- var _a, _b;
564
- const container = (_b = (_a = this._storage[key]) == null ? void 0 : _a.value) != null ? _b : null;
162
+ var _a;
163
+ const container = (_a = this._storage[key]) != null ? _a : null;
565
164
  if (!container) {
566
165
  throw new Error("Storage could not be found");
567
166
  }
568
- container.unsubscribe(this.value, subscriptionId);
167
+ container.unsubscribe(subscriptionId);
569
168
  });
570
169
  };
571
170
  }
@@ -582,6 +181,7 @@ var CrdtLoroDoc = class extends AbstractCrdtDoc {
582
181
  */
583
182
  transact(fn) {
584
183
  fn();
184
+ this.value.commit();
585
185
  return this;
586
186
  }
587
187
  /**
@@ -595,6 +195,7 @@ var CrdtLoroDoc = class extends AbstractCrdtDoc {
595
195
 
596
196
  // src/doc/CrdtLoroDocFactory.ts
597
197
  import { AbstractCrdtDocFactory } from "@pluv/crdt";
198
+ import { LoroList as LoroList2, LoroMap as LoroMap2, LoroText as LoroText2 } from "loro-crdt";
598
199
  var CrdtLoroDocFactory = class _CrdtLoroDocFactory extends AbstractCrdtDocFactory {
599
200
  constructor(initialStorage = () => ({})) {
600
201
  super();
@@ -610,17 +211,14 @@ var CrdtLoroDocFactory = class _CrdtLoroDocFactory extends AbstractCrdtDocFactor
610
211
  const storage = this._initialStorage();
611
212
  return new CrdtLoroDoc(
612
213
  Object.entries(storage).reduce((acc, [key, node]) => {
613
- if (node instanceof CrdtLoroArray) {
614
- return __spreadProps(__spreadValues({}, acc), { [key]: new CrdtLoroArray([]) });
615
- }
616
- if (node instanceof CrdtLoroMap) {
617
- return __spreadProps(__spreadValues({}, acc), { [key]: new CrdtLoroMap([]) });
214
+ if (node instanceof LoroList2) {
215
+ return __spreadProps(__spreadValues({}, acc), { [key]: new LoroList2() });
618
216
  }
619
- if (node instanceof CrdtLoroObject) {
620
- return __spreadProps(__spreadValues({}, acc), { [key]: new CrdtLoroObject({}) });
217
+ if (node instanceof LoroMap2) {
218
+ return __spreadProps(__spreadValues({}, acc), { [key]: new LoroMap2() });
621
219
  }
622
- if (node instanceof CrdtLoroText) {
623
- return __spreadProps(__spreadValues({}, acc), { [key]: new CrdtLoroText("") });
220
+ if (node instanceof LoroText2) {
221
+ return __spreadProps(__spreadValues({}, acc), { [key]: new LoroText2() });
624
222
  }
625
223
  return acc;
626
224
  }, {})
@@ -636,6 +234,44 @@ var CrdtLoroDocFactory = class _CrdtLoroDocFactory extends AbstractCrdtDocFactor
636
234
  var doc = (value = () => ({})) => {
637
235
  return new CrdtLoroDocFactory(value);
638
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
+ };
639
275
  export {
640
276
  loro_exports as loro
641
277
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pluv/crdt-loro",
3
- "version": "0.19.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.19.0",
22
- "@pluv/types": "^0.19.0"
21
+ "@pluv/crdt": "^0.20.0",
22
+ "@pluv/types": "^0.20.0"
23
23
  },
24
24
  "peerDependencies": {
25
- "loro-crdt": "^0.11.0"
25
+ "loro-crdt": "^0.16.0"
26
26
  },
27
27
  "devDependencies": {
28
28
  "eslint": "^8.57.0",
29
- "loro-crdt": "^0.11.1",
29
+ "loro-crdt": "^0.16.6",
30
30
  "tsup": "^8.1.0",
31
31
  "typescript": "^5.4.5",
32
- "@pluv/tsconfig": "^0.19.0",
33
- "eslint-config-pluv": "^0.19.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",