@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.js CHANGED
@@ -44,448 +44,41 @@ module.exports = __toCommonJS(src_exports);
44
44
  // src/loro.ts
45
45
  var loro_exports = {};
46
46
  __export(loro_exports, {
47
- CrdtLoroArray: () => CrdtLoroArray,
48
47
  CrdtLoroDoc: () => CrdtLoroDoc,
49
- CrdtLoroMap: () => CrdtLoroMap,
50
- CrdtLoroObject: () => CrdtLoroObject,
51
- CrdtLoroText: () => CrdtLoroText,
52
- array: () => array,
53
48
  doc: () => doc,
49
+ list: () => list,
54
50
  map: () => map,
55
51
  object: () => object,
56
52
  text: () => text
57
53
  });
58
54
 
59
- // src/array/CrdtLoroArray.ts
60
- var import_crdt5 = require("@pluv/crdt");
61
- var import_loro_crdt4 = require("loro-crdt");
62
-
63
- // src/shared/cloneType.ts
64
- var import_crdt4 = require("@pluv/crdt");
65
-
66
- // src/map/CrdtLoroMap.ts
67
- var import_crdt = require("@pluv/crdt");
68
- var import_loro_crdt = require("loro-crdt");
69
- var CrdtLoroMap = class extends import_crdt.AbstractCrdtType {
70
- constructor(value = []) {
71
- super();
72
- this._doc = null;
73
- this._initialized = false;
74
- this._value = new import_loro_crdt.LoroMap();
75
- this.initialValue = value.map(([k, v]) => [k, v]);
76
- }
77
- set doc(doc2) {
78
- if (this._doc) throw new Error("Cannot overwrite array doc");
79
- this._doc = doc2;
80
- }
81
- get size() {
82
- return this.value.size;
83
- }
84
- get value() {
85
- return this._value;
86
- }
87
- set value(value) {
88
- if (this._initialized) throw new Error("Cannot re-assign map");
89
- this._initialized = true;
90
- this._value = value;
91
- cloneType({ source: this, target: this.value });
92
- }
93
- delete(prop) {
94
- var _a;
95
- this._guardInitialized();
96
- this.value.delete(prop);
97
- (_a = this._doc) == null ? void 0 : _a.value.commit();
98
- return this;
99
- }
100
- set(prop, value) {
101
- var _a, _b;
102
- this._guardInitialized();
103
- if (!(value instanceof import_crdt.AbstractCrdtType)) {
104
- this.value.set(prop, value);
105
- (_a = this._doc) == null ? void 0 : _a.value.commit();
106
- return this;
107
- }
108
- if (!isWrapper(value)) {
109
- throw new Error("This type is not yet supported");
110
- }
111
- const containerType = getLoroContainerType(value);
112
- const container = this.value.setContainer(prop, containerType);
113
- cloneType({ source: value, target: container });
114
- if (this._doc) value.doc = this._doc;
115
- (_b = this._doc) == null ? void 0 : _b.value.commit();
116
- return this;
117
- }
118
- toJson() {
119
- return this.value.toJson();
120
- }
121
- _guardInitialized() {
122
- if (!this._initialized) throw new Error("Array is not yet initialized");
123
- }
124
- };
125
-
126
- // src/map/map.ts
127
- var map = (value = []) => {
128
- return new CrdtLoroMap(value);
129
- };
130
-
131
- // src/object/CrdtLoroObject.ts
132
- var import_crdt2 = require("@pluv/crdt");
133
- var import_loro_crdt2 = require("loro-crdt");
134
- var CrdtLoroObject = class extends import_crdt2.AbstractCrdtType {
135
- constructor(value) {
136
- super();
137
- this._doc = null;
138
- this._initialized = false;
139
- this._value = new import_loro_crdt2.LoroMap();
140
- this.initialValue = Object.entries(value).map(([k, v]) => [k, v]);
141
- }
142
- set doc(doc2) {
143
- if (this._doc) throw new Error("Cannot overwrite array doc");
144
- this._doc = doc2;
145
- }
146
- get size() {
147
- return this.value.size;
148
- }
149
- get value() {
150
- return this._value;
151
- }
152
- set value(value) {
153
- if (this._initialized) throw new Error("Cannot re-assign map");
154
- this._initialized = true;
155
- this._value = value;
156
- cloneType({ source: this, target: this.value });
157
- }
158
- set(prop, value) {
159
- var _a, _b;
160
- this._guardInitialized();
161
- if (!(value instanceof import_crdt2.AbstractCrdtType)) {
162
- this.value.set(prop, value);
163
- (_a = this._doc) == null ? void 0 : _a.value.commit();
164
- return this;
165
- }
166
- if (!isWrapper(value)) {
167
- throw new Error("This type is not yet supported");
168
- }
169
- const containerType = getLoroContainerType(value);
170
- const container = this.value.setContainer(prop, containerType);
171
- cloneType({ source: value, target: container });
172
- if (this._doc) value.doc = this._doc;
173
- (_b = this._doc) == null ? void 0 : _b.value.commit();
174
- return this;
175
- }
176
- toJson() {
177
- return this.value.toJson();
178
- }
179
- _guardInitialized() {
180
- if (!this._initialized) throw new Error("Array is not yet initialized");
181
- }
182
- };
183
-
184
- // src/object/object.ts
185
- var object = (value) => {
186
- return new CrdtLoroObject(value);
187
- };
188
-
189
- // src/text/CrdtLoroText.ts
190
- var import_crdt3 = require("@pluv/crdt");
191
- var import_loro_crdt3 = require("loro-crdt");
192
- var CrdtLoroText = class extends import_crdt3.AbstractCrdtType {
193
- constructor(value = "") {
194
- super();
195
- this._doc = null;
196
- this._initialized = false;
197
- this._value = new import_loro_crdt3.LoroText();
198
- this.initalValue = value;
199
- this.value = new import_loro_crdt3.LoroText();
200
- }
201
- set doc(doc2) {
202
- if (this._doc) throw new Error("Cannot overwrite array doc");
203
- this._doc = doc2;
204
- }
205
- get length() {
206
- return this.value.length;
207
- }
208
- get value() {
209
- return this._value;
210
- }
211
- set value(value) {
212
- if (this._initialized) throw new Error("Cannot re-assign text");
213
- this._initialized = true;
214
- this._value = value;
215
- cloneType({ source: this, target: this.value });
216
- }
217
- delete(index, length = 1) {
218
- var _a;
219
- this._guardInitialized();
220
- this.value.delete(index, length);
221
- (_a = this._doc) == null ? void 0 : _a.value.commit();
222
- return this;
223
- }
224
- insert(index, text2) {
225
- var _a;
226
- this._guardInitialized();
227
- this.value.insert(index, text2);
228
- (_a = this._doc) == null ? void 0 : _a.value.commit();
229
- return this;
230
- }
231
- toJson() {
232
- return this.value.toString();
233
- }
234
- _guardInitialized() {
235
- if (!this._initialized) throw new Error("Array is not yet initialized");
236
- }
237
- };
238
-
239
- // src/text/text.ts
240
- var text = (value = "") => {
241
- return new CrdtLoroText(value);
242
- };
243
-
244
- // src/shared/cloneType.ts
245
- function cloneArray(params) {
246
- const { source, target } = params;
247
- const items = source.initialValue;
248
- items.forEach((item, i) => {
249
- if (!(item instanceof import_crdt4.AbstractCrdtType)) {
250
- target.insert(i, item);
251
- return;
252
- }
253
- if (item instanceof CrdtLoroArray) {
254
- const list = target.insertContainer(i, "List");
255
- item.value = list;
256
- cloneArray({ source: item, target: list });
257
- return;
258
- }
259
- if (item instanceof CrdtLoroMap) {
260
- const map2 = target.insertContainer(i, "Map");
261
- item.value = map2;
262
- cloneMap({ source: item, target: map2 });
263
- return;
264
- }
265
- if (item instanceof CrdtLoroObject) {
266
- const map2 = target.insertContainer(i, "Map");
267
- item.value = map2;
268
- cloneObject({ source: item, target: map2 });
269
- return;
270
- }
271
- if (item instanceof CrdtLoroText) {
272
- const text2 = target.insertContainer(i, "Text");
273
- item.value = text2;
274
- cloneText({ source: item, target: text2 });
275
- return;
276
- }
277
- throw new Error("This type is not yet supported");
278
- });
279
- }
280
- function cloneMap(params) {
281
- const { source, target } = params;
282
- const items = source.initialValue;
283
- items.forEach(([key, item]) => {
284
- if (!(item instanceof import_crdt4.AbstractCrdtType)) {
285
- target.set(key, item);
286
- return;
287
- }
288
- if (item instanceof CrdtLoroArray) {
289
- const list = target.setContainer(key, "List");
290
- item.value = list;
291
- cloneArray({ source: item, target: list });
292
- return;
293
- }
294
- if (item instanceof CrdtLoroMap) {
295
- const map2 = target.setContainer(key, "Map");
296
- item.value = map2;
297
- cloneMap({ source: item, target: map2 });
298
- return;
299
- }
300
- if (item instanceof CrdtLoroObject) {
301
- const map2 = target.setContainer(key, "Map");
302
- item.value = map2;
303
- cloneObject({ source: item, target: map2 });
304
- return;
305
- }
306
- if (item instanceof CrdtLoroText) {
307
- const text2 = target.setContainer(key, "Text");
308
- item.value = text2;
309
- cloneText({ source: item, target: text2 });
310
- return;
311
- }
312
- throw new Error("This type is not yet supported");
313
- });
314
- }
315
- function cloneObject(params) {
316
- const { source, target } = params;
317
- const items = source.initialValue;
318
- items.forEach(([key, item]) => {
319
- if (!(item instanceof import_crdt4.AbstractCrdtType)) {
320
- target.set(key, item);
321
- return;
322
- }
323
- if (item instanceof CrdtLoroArray) {
324
- const list = target.setContainer(key, "List");
325
- item.value = list;
326
- cloneArray({ source: item, target: list });
327
- return;
328
- }
329
- if (item instanceof CrdtLoroMap) {
330
- const map2 = target.setContainer(key, "Map");
331
- item.value = map2;
332
- cloneMap({ source: item, target: map2 });
333
- return;
334
- }
335
- if (item instanceof CrdtLoroObject) {
336
- const map2 = target.setContainer(key, "Map");
337
- item.value = map2;
338
- cloneObject({ source: item, target: map2 });
339
- return;
340
- }
341
- if (item instanceof CrdtLoroText) {
342
- const text2 = target.setContainer(key, "Text");
343
- item.value = text2;
344
- cloneText({ source: item, target: text2 });
345
- return;
346
- }
347
- throw new Error("This type is not yet supported");
348
- });
349
- }
350
- function cloneText(params) {
351
- const { source, target } = params;
352
- source.value = target;
353
- source.insert(0, source.initalValue);
354
- }
355
- var cloneType = (params) => {
356
- const { source, target } = params;
357
- if (source instanceof CrdtLoroArray) {
358
- cloneArray({ source, target });
359
- return;
360
- }
361
- if (source instanceof CrdtLoroMap) {
362
- cloneMap({ source, target });
363
- return;
364
- }
365
- if (source instanceof CrdtLoroObject) {
366
- cloneObject({ source, target });
367
- return;
368
- }
369
- if (source instanceof CrdtLoroText) {
370
- cloneText({ source, target });
371
- return;
372
- }
373
- throw new Error("This type is not yet supported");
374
- };
375
-
376
- // src/shared/getLoroContainerType.ts
377
- var getLoroContainerType = (value) => {
378
- if (value instanceof CrdtLoroArray) return "List";
379
- if (value instanceof CrdtLoroMap) return "Map";
380
- if (value instanceof CrdtLoroObject) return "Map";
381
- if (value instanceof CrdtLoroText) return "Text";
382
- throw new Error("This type is not yet supported");
383
- };
384
-
385
- // src/shared/isWrapper.ts
386
- var isWrapper = (item) => {
387
- return item instanceof CrdtLoroArray || item instanceof CrdtLoroMap || item instanceof CrdtLoroObject || item instanceof CrdtLoroText;
388
- };
389
-
390
- // src/array/CrdtLoroArray.ts
391
- var CrdtLoroArray = class extends import_crdt5.AbstractCrdtType {
392
- constructor(value = []) {
393
- super();
394
- this._doc = null;
395
- this._initialized = false;
396
- this._value = new import_loro_crdt4.LoroList();
397
- this.initialValue = value.slice();
398
- }
399
- set doc(doc2) {
400
- if (this._doc) throw new Error("Cannot overwrite array doc");
401
- this._doc = doc2;
402
- }
403
- get length() {
404
- return this.value.length;
405
- }
406
- get value() {
407
- return this._value;
408
- }
409
- set value(value) {
410
- if (this._initialized) throw new Error("Cannot re-assign array");
411
- this._initialized = true;
412
- this._value = value;
413
- cloneType({ source: this, target: this.value });
414
- }
415
- delete(index, length = 1) {
416
- var _a;
417
- this._guardInitialized();
418
- this.value.delete(index, length);
419
- (_a = this._doc) == null ? void 0 : _a.value.commit();
420
- return this;
421
- }
422
- insert(index, ...items) {
423
- var _a;
424
- this._guardInitialized();
425
- items.forEach((item, i) => {
426
- var _a2;
427
- if (!(item instanceof import_crdt5.AbstractCrdtType)) {
428
- this.value.insert(index + i, item);
429
- (_a2 = this._doc) == null ? void 0 : _a2.value.commit();
430
- return this;
431
- }
432
- if (!isWrapper(item)) {
433
- throw new Error("This type is not yet supported");
434
- }
435
- const containerType = getLoroContainerType(item);
436
- const container = this.value.insertContainer(index + i, containerType);
437
- cloneType({ source: item, target: container });
438
- if (this._doc) item.doc = this._doc;
439
- return this;
440
- });
441
- (_a = this._doc) == null ? void 0 : _a.value.commit();
442
- return this;
443
- }
444
- push(...items) {
445
- return this.insert(this.length, ...items);
446
- }
447
- unshift(...items) {
448
- return this.insert(0, ...items);
449
- }
450
- toJson() {
451
- return this.value.toJson();
452
- }
453
- _guardInitialized() {
454
- if (!this._initialized) 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
55
  // src/doc/CrdtLoroDoc.ts
464
- var import_crdt6 = require("@pluv/crdt");
56
+ var import_crdt = require("@pluv/crdt");
465
57
  var import_js_base64 = require("js-base64");
466
- var import_loro_crdt5 = require("loro-crdt");
467
- var CrdtLoroDoc = class extends import_crdt6.AbstractCrdtDoc {
58
+ var import_loro_crdt = require("loro-crdt");
59
+ var CrdtLoroDoc = class extends import_crdt.AbstractCrdtDoc {
468
60
  constructor(value = {}) {
469
61
  super();
470
- this.value = new import_loro_crdt5.Loro();
62
+ this.value = new import_loro_crdt.Loro();
471
63
  this._storage = Object.entries(value).reduce((acc, [key, node]) => {
472
- if (node instanceof CrdtLoroArray) {
473
- const loroList = this.value.getList(key);
474
- node.value = loroList;
475
- node.doc = this;
476
- return __spreadProps(__spreadValues({}, acc), { [key]: node });
64
+ if (node instanceof import_loro_crdt.LoroList) {
65
+ const container = this.value.getList(key);
66
+ node.toArray().forEach((item, i) => {
67
+ (0, import_loro_crdt.isContainer)(item) ? container.insertContainer(i, item) : container.insert(i, item);
68
+ });
69
+ return __spreadProps(__spreadValues({}, acc), { [key]: container });
477
70
  }
478
- if (node instanceof CrdtLoroMap || node instanceof CrdtLoroObject) {
479
- const loroMap = this.value.getMap(key);
480
- node.value = loroMap;
481
- node.doc = this;
482
- return __spreadProps(__spreadValues({}, acc), { [key]: node });
71
+ if (node instanceof import_loro_crdt.LoroMap) {
72
+ const container = this.value.getMap(key);
73
+ container.entries().forEach(([key2, item]) => {
74
+ (0, import_loro_crdt.isContainer)(item) ? container.setContainer(key2, item) : container.set(key2, item);
75
+ });
76
+ return __spreadProps(__spreadValues({}, acc), { [key]: container });
483
77
  }
484
- if (node instanceof CrdtLoroText) {
485
- const loroText = this.value.getText(key);
486
- node.value = loroText;
487
- node.doc = this;
488
- return __spreadProps(__spreadValues({}, acc), { [key]: loroText });
78
+ if (node instanceof import_loro_crdt.LoroText) {
79
+ const container = this.value.getText(key);
80
+ container.insert(0, node.toString());
81
+ return __spreadProps(__spreadValues({}, acc), { [key]: container });
489
82
  }
490
83
  return acc;
491
84
  }, {});
@@ -545,14 +138,20 @@ var CrdtLoroDoc = class extends import_crdt6.AbstractCrdtDoc {
545
138
  getEncodedState() {
546
139
  return (0, import_js_base64.fromUint8Array)(this.value.exportSnapshot());
547
140
  }
548
- toJson() {
141
+ toJson(type) {
142
+ if (typeof type === "string") {
143
+ const container = this._storage[type];
144
+ return container instanceof import_loro_crdt.LoroText ? container.toString() : container.toJSON();
145
+ }
549
146
  return Object.entries(this._storage).reduce(
550
- (acc, [key, value]) => __spreadProps(__spreadValues({}, acc), { [key]: value.toJson() }),
147
+ (acc, [key, value]) => __spreadProps(__spreadValues({}, acc), {
148
+ [key]: value instanceof import_loro_crdt.LoroText ? value.toString() : value.toJSON()
149
+ }),
551
150
  {}
552
151
  );
553
152
  }
554
153
  isEmpty() {
555
- const serialized = this.value.toJson();
154
+ const serialized = this.value.toJSON();
556
155
  return !serialized || !Object.keys(serialized).length;
557
156
  }
558
157
  /**
@@ -567,24 +166,24 @@ var CrdtLoroDoc = class extends import_crdt6.AbstractCrdtDoc {
567
166
  const update = (0, import_js_base64.fromUint8Array)(this.value.exportFrom());
568
167
  listener({
569
168
  doc: this,
570
- local: event.local,
169
+ local: event.by === "local",
571
170
  origin: event.origin ? String(event.origin) : null,
572
171
  update
573
172
  });
574
173
  };
575
174
  const subscriptionIds = Object.entries(this._storage).reduce((map2, [key, crdtType]) => {
576
- const container = crdtType.value;
577
- const subscriptionId = container.subscribe(this.value, fn);
175
+ const container = crdtType;
176
+ const subscriptionId = container.subscribe(fn);
578
177
  return map2.set(key, subscriptionId);
579
178
  }, /* @__PURE__ */ new Map());
580
179
  return () => {
581
180
  Array.from(subscriptionIds.entries()).forEach(([key, subscriptionId]) => {
582
- var _a, _b;
583
- const container = (_b = (_a = this._storage[key]) == null ? void 0 : _a.value) != null ? _b : null;
181
+ var _a;
182
+ const container = (_a = this._storage[key]) != null ? _a : null;
584
183
  if (!container) {
585
184
  throw new Error("Storage could not be found");
586
185
  }
587
- container.unsubscribe(this.value, subscriptionId);
186
+ container.unsubscribe(subscriptionId);
588
187
  });
589
188
  };
590
189
  }
@@ -601,6 +200,7 @@ var CrdtLoroDoc = class extends import_crdt6.AbstractCrdtDoc {
601
200
  */
602
201
  transact(fn) {
603
202
  fn();
203
+ this.value.commit();
604
204
  return this;
605
205
  }
606
206
  /**
@@ -613,8 +213,9 @@ var CrdtLoroDoc = class extends import_crdt6.AbstractCrdtDoc {
613
213
  };
614
214
 
615
215
  // src/doc/CrdtLoroDocFactory.ts
616
- var import_crdt7 = require("@pluv/crdt");
617
- var CrdtLoroDocFactory = class _CrdtLoroDocFactory extends import_crdt7.AbstractCrdtDocFactory {
216
+ var import_crdt2 = require("@pluv/crdt");
217
+ var import_loro_crdt2 = require("loro-crdt");
218
+ var CrdtLoroDocFactory = class _CrdtLoroDocFactory extends import_crdt2.AbstractCrdtDocFactory {
618
219
  constructor(initialStorage = () => ({})) {
619
220
  super();
620
221
  this._initialStorage = initialStorage;
@@ -629,17 +230,14 @@ var CrdtLoroDocFactory = class _CrdtLoroDocFactory extends import_crdt7.Abstract
629
230
  const storage = this._initialStorage();
630
231
  return new CrdtLoroDoc(
631
232
  Object.entries(storage).reduce((acc, [key, node]) => {
632
- if (node instanceof CrdtLoroArray) {
633
- return __spreadProps(__spreadValues({}, acc), { [key]: new CrdtLoroArray([]) });
233
+ if (node instanceof import_loro_crdt2.LoroList) {
234
+ return __spreadProps(__spreadValues({}, acc), { [key]: new import_loro_crdt2.LoroList() });
634
235
  }
635
- if (node instanceof CrdtLoroMap) {
636
- return __spreadProps(__spreadValues({}, acc), { [key]: new CrdtLoroMap([]) });
236
+ if (node instanceof import_loro_crdt2.LoroMap) {
237
+ return __spreadProps(__spreadValues({}, acc), { [key]: new import_loro_crdt2.LoroMap() });
637
238
  }
638
- if (node instanceof CrdtLoroObject) {
639
- return __spreadProps(__spreadValues({}, acc), { [key]: new CrdtLoroObject({}) });
640
- }
641
- if (node instanceof CrdtLoroText) {
642
- return __spreadProps(__spreadValues({}, acc), { [key]: new CrdtLoroText("") });
239
+ if (node instanceof import_loro_crdt2.LoroText) {
240
+ return __spreadProps(__spreadValues({}, acc), { [key]: new import_loro_crdt2.LoroText() });
643
241
  }
644
242
  return acc;
645
243
  }, {})
@@ -655,6 +253,44 @@ var CrdtLoroDocFactory = class _CrdtLoroDocFactory extends import_crdt7.Abstract
655
253
  var doc = (value = () => ({})) => {
656
254
  return new CrdtLoroDocFactory(value);
657
255
  };
256
+
257
+ // src/list.ts
258
+ var import_loro_crdt3 = require("loro-crdt");
259
+ var list = (value = []) => {
260
+ const container = new import_loro_crdt3.LoroList();
261
+ value.forEach((item, i) => {
262
+ (0, import_loro_crdt3.isContainer)(item) ? container.insertContainer(i, item) : container.insert(i, item);
263
+ });
264
+ return container;
265
+ };
266
+
267
+ // src/map.ts
268
+ var import_loro_crdt4 = require("loro-crdt");
269
+ var map = (value = []) => {
270
+ const container = new import_loro_crdt4.LoroMap();
271
+ value.forEach(([key, item]) => {
272
+ (0, import_loro_crdt4.isContainer)(item) ? container.setContainer(key, item) : container.set(key, item);
273
+ });
274
+ return container;
275
+ };
276
+
277
+ // src/object.ts
278
+ var import_loro_crdt5 = require("loro-crdt");
279
+ var object = (value) => {
280
+ const container = new import_loro_crdt5.LoroMap();
281
+ Object.entries(value).forEach(([key, item]) => {
282
+ (0, import_loro_crdt5.isContainer)(item) ? container.setContainer(key, item) : container.set(key, item);
283
+ });
284
+ return container;
285
+ };
286
+
287
+ // src/text.ts
288
+ var import_loro_crdt6 = require("loro-crdt");
289
+ var text = (value = "") => {
290
+ const container = new import_loro_crdt6.LoroText();
291
+ container.insert(0, value);
292
+ return container;
293
+ };
658
294
  // Annotate the CommonJS export names for ESM import in node:
659
295
  0 && (module.exports = {
660
296
  loro