@pluv/crdt-loro 0.16.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/.eslintrc.js +4 -0
- package/.turbo/turbo-build.log +18 -0
- package/CHANGELOG.md +8 -0
- package/LICENSE +21 -0
- package/dist/index.d.mts +164 -0
- package/dist/index.d.ts +164 -0
- package/dist/index.js +701 -0
- package/dist/index.mjs +681 -0
- package/package.json +41 -0
- package/src/array/CrdtLoroArray.ts +102 -0
- package/src/array/array.ts +7 -0
- package/src/array/index.ts +2 -0
- package/src/doc/CrdtLoroDoc.ts +239 -0
- package/src/doc/CrdtLoroDocFactory.ts +65 -0
- package/src/doc/doc.ts +10 -0
- package/src/doc/index.ts +3 -0
- package/src/index.ts +1 -0
- package/src/loro.ts +6 -0
- package/src/map/CrdtLoroMap.ts +90 -0
- package/src/map/index.ts +2 -0
- package/src/map/map.ts +7 -0
- package/src/object/CrdtLoroObject.ts +79 -0
- package/src/object/index.ts +2 -0
- package/src/object/object.ts +7 -0
- package/src/shared/cloneType.ts +238 -0
- package/src/shared/getLoroContainerType.ts +22 -0
- package/src/shared/index.ts +5 -0
- package/src/shared/isWrapper.ts +19 -0
- package/src/shared/toLoroValue.ts +5 -0
- package/src/text/CrdtLoroText.ts +68 -0
- package/src/text/index.ts +2 -0
- package/src/text/text.ts +5 -0
- package/src/types.ts +40 -0
- package/tsconfig.json +8 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,701 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
10
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
11
|
+
var __spreadValues = (a, b) => {
|
|
12
|
+
for (var prop in b || (b = {}))
|
|
13
|
+
if (__hasOwnProp.call(b, prop))
|
|
14
|
+
__defNormalProp(a, prop, b[prop]);
|
|
15
|
+
if (__getOwnPropSymbols)
|
|
16
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
17
|
+
if (__propIsEnum.call(b, prop))
|
|
18
|
+
__defNormalProp(a, prop, b[prop]);
|
|
19
|
+
}
|
|
20
|
+
return a;
|
|
21
|
+
};
|
|
22
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
23
|
+
var __export = (target, all) => {
|
|
24
|
+
for (var name in all)
|
|
25
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
26
|
+
};
|
|
27
|
+
var __copyProps = (to, from, except, desc) => {
|
|
28
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
29
|
+
for (let key of __getOwnPropNames(from))
|
|
30
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
31
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
32
|
+
}
|
|
33
|
+
return to;
|
|
34
|
+
};
|
|
35
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
36
|
+
|
|
37
|
+
// src/index.ts
|
|
38
|
+
var src_exports = {};
|
|
39
|
+
__export(src_exports, {
|
|
40
|
+
loro: () => loro_exports
|
|
41
|
+
});
|
|
42
|
+
module.exports = __toCommonJS(src_exports);
|
|
43
|
+
|
|
44
|
+
// src/loro.ts
|
|
45
|
+
var loro_exports = {};
|
|
46
|
+
__export(loro_exports, {
|
|
47
|
+
CrdtLoroArray: () => CrdtLoroArray,
|
|
48
|
+
CrdtLoroDoc: () => CrdtLoroDoc,
|
|
49
|
+
CrdtLoroMap: () => CrdtLoroMap,
|
|
50
|
+
CrdtLoroObject: () => CrdtLoroObject,
|
|
51
|
+
CrdtLoroText: () => CrdtLoroText,
|
|
52
|
+
array: () => array,
|
|
53
|
+
doc: () => doc,
|
|
54
|
+
map: () => map,
|
|
55
|
+
object: () => object,
|
|
56
|
+
text: () => text
|
|
57
|
+
});
|
|
58
|
+
|
|
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(
|
|
76
|
+
([k, v]) => [k, v]
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
set doc(doc2) {
|
|
80
|
+
if (this._doc)
|
|
81
|
+
throw new Error("Cannot overwrite array doc");
|
|
82
|
+
this._doc = doc2;
|
|
83
|
+
}
|
|
84
|
+
get size() {
|
|
85
|
+
return this.value.size;
|
|
86
|
+
}
|
|
87
|
+
get value() {
|
|
88
|
+
return this._value;
|
|
89
|
+
}
|
|
90
|
+
set value(value) {
|
|
91
|
+
if (this._initialized)
|
|
92
|
+
throw new Error("Cannot re-assign map");
|
|
93
|
+
this._initialized = true;
|
|
94
|
+
this._value = value;
|
|
95
|
+
cloneType({ source: this, target: this.value });
|
|
96
|
+
}
|
|
97
|
+
delete(prop) {
|
|
98
|
+
var _a;
|
|
99
|
+
this._guardInitialized();
|
|
100
|
+
this.value.delete(prop);
|
|
101
|
+
(_a = this._doc) == null ? void 0 : _a.value.commit();
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
set(prop, value) {
|
|
105
|
+
var _a, _b;
|
|
106
|
+
this._guardInitialized();
|
|
107
|
+
if (!(value instanceof import_crdt.AbstractCrdtType)) {
|
|
108
|
+
this.value.set(prop, value);
|
|
109
|
+
(_a = this._doc) == null ? void 0 : _a.value.commit();
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
if (!isWrapper(value)) {
|
|
113
|
+
throw new Error("This type is not yet supported");
|
|
114
|
+
}
|
|
115
|
+
const containerType = getLoroContainerType(value);
|
|
116
|
+
const container = this.value.setContainer(prop, containerType);
|
|
117
|
+
cloneType({ source: value, target: container });
|
|
118
|
+
if (this._doc)
|
|
119
|
+
value.doc = this._doc;
|
|
120
|
+
(_b = this._doc) == null ? void 0 : _b.value.commit();
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
toJson() {
|
|
124
|
+
return this.value.toJson();
|
|
125
|
+
}
|
|
126
|
+
_guardInitialized() {
|
|
127
|
+
if (!this._initialized)
|
|
128
|
+
throw new Error("Array is not yet initialized");
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
// src/map/map.ts
|
|
133
|
+
var map = (value = []) => {
|
|
134
|
+
return new CrdtLoroMap(value);
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
// src/object/CrdtLoroObject.ts
|
|
138
|
+
var import_crdt2 = require("@pluv/crdt");
|
|
139
|
+
var import_loro_crdt2 = require("loro-crdt");
|
|
140
|
+
var CrdtLoroObject = class extends import_crdt2.AbstractCrdtType {
|
|
141
|
+
constructor(value) {
|
|
142
|
+
super();
|
|
143
|
+
this._doc = null;
|
|
144
|
+
this._initialized = false;
|
|
145
|
+
this._value = new import_loro_crdt2.LoroMap();
|
|
146
|
+
this.initialValue = Object.entries(value).map(
|
|
147
|
+
([k, v]) => [k, v]
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
set doc(doc2) {
|
|
151
|
+
if (this._doc)
|
|
152
|
+
throw new Error("Cannot overwrite array doc");
|
|
153
|
+
this._doc = doc2;
|
|
154
|
+
}
|
|
155
|
+
get size() {
|
|
156
|
+
return this.value.size;
|
|
157
|
+
}
|
|
158
|
+
get value() {
|
|
159
|
+
return this._value;
|
|
160
|
+
}
|
|
161
|
+
set value(value) {
|
|
162
|
+
if (this._initialized)
|
|
163
|
+
throw new Error("Cannot re-assign map");
|
|
164
|
+
this._initialized = true;
|
|
165
|
+
this._value = value;
|
|
166
|
+
cloneType({ source: this, target: this.value });
|
|
167
|
+
}
|
|
168
|
+
set(prop, value) {
|
|
169
|
+
var _a, _b;
|
|
170
|
+
this._guardInitialized();
|
|
171
|
+
if (!(value instanceof import_crdt2.AbstractCrdtType)) {
|
|
172
|
+
this.value.set(prop, value);
|
|
173
|
+
(_a = this._doc) == null ? void 0 : _a.value.commit();
|
|
174
|
+
return this;
|
|
175
|
+
}
|
|
176
|
+
if (!isWrapper(value)) {
|
|
177
|
+
throw new Error("This type is not yet supported");
|
|
178
|
+
}
|
|
179
|
+
const containerType = getLoroContainerType(value);
|
|
180
|
+
const container = this.value.setContainer(prop, containerType);
|
|
181
|
+
cloneType({ source: value, target: container });
|
|
182
|
+
if (this._doc)
|
|
183
|
+
value.doc = this._doc;
|
|
184
|
+
(_b = this._doc) == null ? void 0 : _b.value.commit();
|
|
185
|
+
return this;
|
|
186
|
+
}
|
|
187
|
+
toJson() {
|
|
188
|
+
return this.value.toJson();
|
|
189
|
+
}
|
|
190
|
+
_guardInitialized() {
|
|
191
|
+
if (!this._initialized)
|
|
192
|
+
throw new Error("Array is not yet initialized");
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
// src/object/object.ts
|
|
197
|
+
var object = (value) => {
|
|
198
|
+
return new CrdtLoroObject(value);
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
// src/text/CrdtLoroText.ts
|
|
202
|
+
var import_crdt3 = require("@pluv/crdt");
|
|
203
|
+
var import_loro_crdt3 = require("loro-crdt");
|
|
204
|
+
var CrdtLoroText = class extends import_crdt3.AbstractCrdtType {
|
|
205
|
+
constructor(value = "") {
|
|
206
|
+
super();
|
|
207
|
+
this._doc = null;
|
|
208
|
+
this._initialized = false;
|
|
209
|
+
this._value = new import_loro_crdt3.LoroText();
|
|
210
|
+
this.initalValue = value;
|
|
211
|
+
this.value = new import_loro_crdt3.LoroText();
|
|
212
|
+
}
|
|
213
|
+
set doc(doc2) {
|
|
214
|
+
if (this._doc)
|
|
215
|
+
throw new Error("Cannot overwrite array doc");
|
|
216
|
+
this._doc = doc2;
|
|
217
|
+
}
|
|
218
|
+
get length() {
|
|
219
|
+
return this.value.length;
|
|
220
|
+
}
|
|
221
|
+
get value() {
|
|
222
|
+
return this._value;
|
|
223
|
+
}
|
|
224
|
+
set value(value) {
|
|
225
|
+
if (this._initialized)
|
|
226
|
+
throw new Error("Cannot re-assign text");
|
|
227
|
+
this._initialized = true;
|
|
228
|
+
this._value = value;
|
|
229
|
+
cloneType({ source: this, target: this.value });
|
|
230
|
+
}
|
|
231
|
+
delete(index, length = 1) {
|
|
232
|
+
var _a;
|
|
233
|
+
this._guardInitialized();
|
|
234
|
+
this.value.delete(index, length);
|
|
235
|
+
(_a = this._doc) == null ? void 0 : _a.value.commit();
|
|
236
|
+
return this;
|
|
237
|
+
}
|
|
238
|
+
insert(index, text2) {
|
|
239
|
+
var _a;
|
|
240
|
+
this._guardInitialized();
|
|
241
|
+
this.value.insert(index, text2);
|
|
242
|
+
(_a = this._doc) == null ? void 0 : _a.value.commit();
|
|
243
|
+
return this;
|
|
244
|
+
}
|
|
245
|
+
toJson() {
|
|
246
|
+
return this.value.toString();
|
|
247
|
+
}
|
|
248
|
+
_guardInitialized() {
|
|
249
|
+
if (!this._initialized)
|
|
250
|
+
throw new Error("Array is not yet initialized");
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
// src/text/text.ts
|
|
255
|
+
var text = (value = "") => {
|
|
256
|
+
return new CrdtLoroText(value);
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
// src/shared/cloneType.ts
|
|
260
|
+
function cloneArray(params) {
|
|
261
|
+
const { source, target } = params;
|
|
262
|
+
const items = source.initialValue;
|
|
263
|
+
items.forEach((item, i) => {
|
|
264
|
+
if (!(item instanceof import_crdt4.AbstractCrdtType)) {
|
|
265
|
+
target.insert(i, item);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
if (item instanceof CrdtLoroArray) {
|
|
269
|
+
const list = target.insertContainer(i, "List");
|
|
270
|
+
item.value = list;
|
|
271
|
+
cloneArray({ source: item, target: list });
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
if (item instanceof CrdtLoroMap) {
|
|
275
|
+
const map2 = target.insertContainer(i, "Map");
|
|
276
|
+
item.value = map2;
|
|
277
|
+
cloneMap({ source: item, target: map2 });
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if (item instanceof CrdtLoroObject) {
|
|
281
|
+
const map2 = target.insertContainer(i, "Map");
|
|
282
|
+
item.value = map2;
|
|
283
|
+
cloneObject({ source: item, target: map2 });
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
if (item instanceof CrdtLoroText) {
|
|
287
|
+
const text2 = target.insertContainer(i, "Text");
|
|
288
|
+
item.value = text2;
|
|
289
|
+
cloneText({ source: item, target: text2 });
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
throw new Error("This type is not yet supported");
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
function cloneMap(params) {
|
|
296
|
+
const { source, target } = params;
|
|
297
|
+
const items = source.initialValue;
|
|
298
|
+
items.forEach(([key, item]) => {
|
|
299
|
+
if (!(item instanceof import_crdt4.AbstractCrdtType)) {
|
|
300
|
+
target.set(key, item);
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
if (item instanceof CrdtLoroArray) {
|
|
304
|
+
const list = target.setContainer(key, "List");
|
|
305
|
+
item.value = list;
|
|
306
|
+
cloneArray({ source: item, target: list });
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
if (item instanceof CrdtLoroMap) {
|
|
310
|
+
const map2 = target.setContainer(key, "Map");
|
|
311
|
+
item.value = map2;
|
|
312
|
+
cloneMap({ source: item, target: map2 });
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
if (item instanceof CrdtLoroObject) {
|
|
316
|
+
const map2 = target.setContainer(key, "Map");
|
|
317
|
+
item.value = map2;
|
|
318
|
+
cloneObject({ source: item, target: map2 });
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
if (item instanceof CrdtLoroText) {
|
|
322
|
+
const text2 = target.setContainer(key, "Text");
|
|
323
|
+
item.value = text2;
|
|
324
|
+
cloneText({ source: item, target: text2 });
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
throw new Error("This type is not yet supported");
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
function cloneObject(params) {
|
|
331
|
+
const { source, target } = params;
|
|
332
|
+
const items = source.initialValue;
|
|
333
|
+
items.forEach(([key, item]) => {
|
|
334
|
+
if (!(item instanceof import_crdt4.AbstractCrdtType)) {
|
|
335
|
+
target.set(key, item);
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
if (item instanceof CrdtLoroArray) {
|
|
339
|
+
const list = target.setContainer(key, "List");
|
|
340
|
+
item.value = list;
|
|
341
|
+
cloneArray({ source: item, target: list });
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
if (item instanceof CrdtLoroMap) {
|
|
345
|
+
const map2 = target.setContainer(key, "Map");
|
|
346
|
+
item.value = map2;
|
|
347
|
+
cloneMap({ source: item, target: map2 });
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
if (item instanceof CrdtLoroObject) {
|
|
351
|
+
const map2 = target.setContainer(key, "Map");
|
|
352
|
+
item.value = map2;
|
|
353
|
+
cloneObject({ source: item, target: map2 });
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
if (item instanceof CrdtLoroText) {
|
|
357
|
+
const text2 = target.setContainer(key, "Text");
|
|
358
|
+
item.value = text2;
|
|
359
|
+
cloneText({ source: item, target: text2 });
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
throw new Error("This type is not yet supported");
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
function cloneText(params) {
|
|
366
|
+
const { source, target } = params;
|
|
367
|
+
source.value = target;
|
|
368
|
+
source.insert(0, source.initalValue);
|
|
369
|
+
}
|
|
370
|
+
var cloneType = (params) => {
|
|
371
|
+
const { source, target } = params;
|
|
372
|
+
if (source instanceof CrdtLoroArray) {
|
|
373
|
+
cloneArray({ source, target });
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
if (source instanceof CrdtLoroMap) {
|
|
377
|
+
cloneMap({ source, target });
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
if (source instanceof CrdtLoroObject) {
|
|
381
|
+
cloneObject({ source, target });
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
if (source instanceof CrdtLoroText) {
|
|
385
|
+
cloneText({ source, target });
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
throw new Error("This type is not yet supported");
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
// src/shared/getLoroContainerType.ts
|
|
392
|
+
var getLoroContainerType = (value) => {
|
|
393
|
+
if (value instanceof CrdtLoroArray)
|
|
394
|
+
return "List";
|
|
395
|
+
if (value instanceof CrdtLoroMap)
|
|
396
|
+
return "Map";
|
|
397
|
+
if (value instanceof CrdtLoroObject)
|
|
398
|
+
return "Map";
|
|
399
|
+
if (value instanceof CrdtLoroText)
|
|
400
|
+
return "Text";
|
|
401
|
+
throw new Error("This type is not yet supported");
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
// src/shared/isWrapper.ts
|
|
405
|
+
var isWrapper = (item) => {
|
|
406
|
+
return item instanceof CrdtLoroArray || item instanceof CrdtLoroMap || item instanceof CrdtLoroObject || item instanceof CrdtLoroText;
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
// src/array/CrdtLoroArray.ts
|
|
410
|
+
var CrdtLoroArray = class extends import_crdt5.AbstractCrdtType {
|
|
411
|
+
constructor(value = []) {
|
|
412
|
+
super();
|
|
413
|
+
this._doc = null;
|
|
414
|
+
this._initialized = false;
|
|
415
|
+
this._value = new import_loro_crdt4.LoroList();
|
|
416
|
+
this.initialValue = value.slice();
|
|
417
|
+
}
|
|
418
|
+
set doc(doc2) {
|
|
419
|
+
if (this._doc)
|
|
420
|
+
throw new Error("Cannot overwrite array doc");
|
|
421
|
+
this._doc = doc2;
|
|
422
|
+
}
|
|
423
|
+
get length() {
|
|
424
|
+
return this.value.length;
|
|
425
|
+
}
|
|
426
|
+
get value() {
|
|
427
|
+
return this._value;
|
|
428
|
+
}
|
|
429
|
+
set value(value) {
|
|
430
|
+
if (this._initialized)
|
|
431
|
+
throw new Error("Cannot re-assign array");
|
|
432
|
+
this._initialized = true;
|
|
433
|
+
this._value = value;
|
|
434
|
+
cloneType({ source: this, target: this.value });
|
|
435
|
+
}
|
|
436
|
+
delete(index, length = 1) {
|
|
437
|
+
var _a;
|
|
438
|
+
this._guardInitialized();
|
|
439
|
+
this.value.delete(index, length);
|
|
440
|
+
(_a = this._doc) == null ? void 0 : _a.value.commit();
|
|
441
|
+
return this;
|
|
442
|
+
}
|
|
443
|
+
insert(index, ...items) {
|
|
444
|
+
var _a;
|
|
445
|
+
this._guardInitialized();
|
|
446
|
+
items.forEach((item, i) => {
|
|
447
|
+
var _a2;
|
|
448
|
+
if (!(item instanceof import_crdt5.AbstractCrdtType)) {
|
|
449
|
+
this.value.insert(index + i, item);
|
|
450
|
+
(_a2 = this._doc) == null ? void 0 : _a2.value.commit();
|
|
451
|
+
return this;
|
|
452
|
+
}
|
|
453
|
+
if (!isWrapper(item)) {
|
|
454
|
+
throw new Error("This type is not yet supported");
|
|
455
|
+
}
|
|
456
|
+
const containerType = getLoroContainerType(item);
|
|
457
|
+
const container = this.value.insertContainer(
|
|
458
|
+
index + i,
|
|
459
|
+
containerType
|
|
460
|
+
);
|
|
461
|
+
cloneType({ source: item, target: container });
|
|
462
|
+
if (this._doc)
|
|
463
|
+
item.doc = this._doc;
|
|
464
|
+
return this;
|
|
465
|
+
});
|
|
466
|
+
(_a = this._doc) == null ? void 0 : _a.value.commit();
|
|
467
|
+
return this;
|
|
468
|
+
}
|
|
469
|
+
push(...items) {
|
|
470
|
+
return this.insert(this.length, ...items);
|
|
471
|
+
}
|
|
472
|
+
unshift(...items) {
|
|
473
|
+
return this.insert(0, ...items);
|
|
474
|
+
}
|
|
475
|
+
toJson() {
|
|
476
|
+
return this.value.toJson();
|
|
477
|
+
}
|
|
478
|
+
_guardInitialized() {
|
|
479
|
+
if (!this._initialized)
|
|
480
|
+
throw new Error("Array is not yet initialized");
|
|
481
|
+
}
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
// src/array/array.ts
|
|
485
|
+
var array = (value = []) => {
|
|
486
|
+
return new CrdtLoroArray(value);
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
// src/doc/CrdtLoroDoc.ts
|
|
490
|
+
var import_crdt6 = require("@pluv/crdt");
|
|
491
|
+
var import_js_base64 = require("js-base64");
|
|
492
|
+
var import_loro_crdt5 = require("loro-crdt");
|
|
493
|
+
var CrdtLoroDoc = class extends import_crdt6.AbstractCrdtDoc {
|
|
494
|
+
constructor(value = {}) {
|
|
495
|
+
super();
|
|
496
|
+
this.value = new import_loro_crdt5.Loro();
|
|
497
|
+
this._storage = Object.entries(value).reduce((acc, [key, node]) => {
|
|
498
|
+
if (node instanceof CrdtLoroArray) {
|
|
499
|
+
const loroList = this.value.getList(key);
|
|
500
|
+
node.value = loroList;
|
|
501
|
+
node.doc = this;
|
|
502
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: node });
|
|
503
|
+
}
|
|
504
|
+
if (node instanceof CrdtLoroMap || node instanceof CrdtLoroObject) {
|
|
505
|
+
const loroMap = this.value.getMap(key);
|
|
506
|
+
node.value = loroMap;
|
|
507
|
+
node.doc = this;
|
|
508
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: node });
|
|
509
|
+
}
|
|
510
|
+
if (node instanceof CrdtLoroText) {
|
|
511
|
+
const loroText = this.value.getText(key);
|
|
512
|
+
node.value = loroText;
|
|
513
|
+
node.doc = this;
|
|
514
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: loroText });
|
|
515
|
+
}
|
|
516
|
+
return acc;
|
|
517
|
+
}, {});
|
|
518
|
+
}
|
|
519
|
+
applyEncodedState(params) {
|
|
520
|
+
const update = typeof params.update === "string" ? (0, import_js_base64.toUint8Array)(params.update) : params.update;
|
|
521
|
+
if (!update)
|
|
522
|
+
return this;
|
|
523
|
+
this.value.import(update);
|
|
524
|
+
return this;
|
|
525
|
+
}
|
|
526
|
+
batchApplyEncodedState(updates) {
|
|
527
|
+
var _a;
|
|
528
|
+
const _updates = updates.reduce((acc, item) => {
|
|
529
|
+
if (!item)
|
|
530
|
+
return acc;
|
|
531
|
+
if (typeof item === "string") {
|
|
532
|
+
acc.push((0, import_js_base64.toUint8Array)(item));
|
|
533
|
+
return acc;
|
|
534
|
+
}
|
|
535
|
+
if (typeof item === "object") {
|
|
536
|
+
const update = typeof item.update === "string" ? (0, import_js_base64.toUint8Array)(item.update) : item.update;
|
|
537
|
+
if (!update)
|
|
538
|
+
return acc;
|
|
539
|
+
acc.push(update);
|
|
540
|
+
return acc;
|
|
541
|
+
}
|
|
542
|
+
return acc;
|
|
543
|
+
}, []);
|
|
544
|
+
if (!_updates.length)
|
|
545
|
+
return this;
|
|
546
|
+
if (_updates.length === 1) {
|
|
547
|
+
const update = (_a = _updates[0]) != null ? _a : null;
|
|
548
|
+
update && this.value.import(update);
|
|
549
|
+
return this;
|
|
550
|
+
}
|
|
551
|
+
this.value.importUpdateBatch(_updates);
|
|
552
|
+
return this;
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* TODO
|
|
556
|
+
* @description This method is not yet supported for loro
|
|
557
|
+
*/
|
|
558
|
+
canRedo() {
|
|
559
|
+
return false;
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* TODO
|
|
563
|
+
* @description This method is not yet supported for loro
|
|
564
|
+
*/
|
|
565
|
+
canUndo() {
|
|
566
|
+
return false;
|
|
567
|
+
}
|
|
568
|
+
destroy() {
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
get(key) {
|
|
572
|
+
if (typeof key === "undefined")
|
|
573
|
+
return this._storage;
|
|
574
|
+
return this._storage[key];
|
|
575
|
+
}
|
|
576
|
+
getEncodedState() {
|
|
577
|
+
return (0, import_js_base64.fromUint8Array)(this.value.exportSnapshot());
|
|
578
|
+
}
|
|
579
|
+
toJson() {
|
|
580
|
+
return Object.entries(this._storage).reduce(
|
|
581
|
+
(acc, [key, value]) => __spreadProps(__spreadValues({}, acc), { [key]: value.toJson() }),
|
|
582
|
+
{}
|
|
583
|
+
);
|
|
584
|
+
}
|
|
585
|
+
isEmpty() {
|
|
586
|
+
const serialized = this.value.toJson();
|
|
587
|
+
return !serialized || !Object.keys(serialized).length;
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* TODO
|
|
591
|
+
* @description This method is not yet supported for loro
|
|
592
|
+
*/
|
|
593
|
+
redo() {
|
|
594
|
+
throw new Error("This is not yet supported");
|
|
595
|
+
}
|
|
596
|
+
subscribe(listener) {
|
|
597
|
+
const fn = (event) => {
|
|
598
|
+
const update = (0, import_js_base64.fromUint8Array)(this.value.exportFrom());
|
|
599
|
+
listener({
|
|
600
|
+
doc: this,
|
|
601
|
+
local: event.local,
|
|
602
|
+
origin: event.origin ? String(event.origin) : null,
|
|
603
|
+
update
|
|
604
|
+
});
|
|
605
|
+
};
|
|
606
|
+
const subscriptionIds = Object.entries(this._storage).reduce(
|
|
607
|
+
(map2, [key, crdtType]) => {
|
|
608
|
+
const container = crdtType.value;
|
|
609
|
+
const subscriptionId = container.subscribe(this.value, fn);
|
|
610
|
+
return map2.set(key, subscriptionId);
|
|
611
|
+
},
|
|
612
|
+
/* @__PURE__ */ new Map()
|
|
613
|
+
);
|
|
614
|
+
return () => {
|
|
615
|
+
Array.from(subscriptionIds.entries()).forEach(
|
|
616
|
+
([key, subscriptionId]) => {
|
|
617
|
+
var _a, _b;
|
|
618
|
+
const container = (_b = (_a = this._storage[key]) == null ? void 0 : _a.value) != null ? _b : null;
|
|
619
|
+
if (!container) {
|
|
620
|
+
throw new Error("Storage could not be found");
|
|
621
|
+
}
|
|
622
|
+
container.unsubscribe(this.value, subscriptionId);
|
|
623
|
+
}
|
|
624
|
+
);
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
/**
|
|
628
|
+
* TODO
|
|
629
|
+
* @description This method doesn't do anything yet.
|
|
630
|
+
*/
|
|
631
|
+
track() {
|
|
632
|
+
return this;
|
|
633
|
+
}
|
|
634
|
+
/**
|
|
635
|
+
* TODO
|
|
636
|
+
* @description This method doesn't do anything yet. The callback will still be executed.
|
|
637
|
+
*/
|
|
638
|
+
transact(fn) {
|
|
639
|
+
fn();
|
|
640
|
+
return this;
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* TODO
|
|
644
|
+
* @description This method is not yet supported for loro
|
|
645
|
+
*/
|
|
646
|
+
undo() {
|
|
647
|
+
throw new Error("This is not yet supported");
|
|
648
|
+
}
|
|
649
|
+
};
|
|
650
|
+
|
|
651
|
+
// src/doc/CrdtLoroDocFactory.ts
|
|
652
|
+
var import_crdt7 = require("@pluv/crdt");
|
|
653
|
+
var CrdtLoroDocFactory = class _CrdtLoroDocFactory extends import_crdt7.AbstractCrdtDocFactory {
|
|
654
|
+
constructor(initialStorage = () => ({})) {
|
|
655
|
+
super();
|
|
656
|
+
this._initialStorage = initialStorage;
|
|
657
|
+
}
|
|
658
|
+
getEmpty() {
|
|
659
|
+
return new CrdtLoroDoc();
|
|
660
|
+
}
|
|
661
|
+
getFactory(initialStorage) {
|
|
662
|
+
return new _CrdtLoroDocFactory(
|
|
663
|
+
initialStorage != null ? initialStorage : this._initialStorage
|
|
664
|
+
);
|
|
665
|
+
}
|
|
666
|
+
getFresh() {
|
|
667
|
+
const storage = this._initialStorage();
|
|
668
|
+
return new CrdtLoroDoc(
|
|
669
|
+
Object.entries(storage).reduce((acc, [key, node]) => {
|
|
670
|
+
if (node instanceof CrdtLoroArray) {
|
|
671
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: new CrdtLoroArray([]) });
|
|
672
|
+
}
|
|
673
|
+
if (node instanceof CrdtLoroMap) {
|
|
674
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: new CrdtLoroMap([]) });
|
|
675
|
+
}
|
|
676
|
+
if (node instanceof CrdtLoroObject) {
|
|
677
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: new CrdtLoroObject({}) });
|
|
678
|
+
}
|
|
679
|
+
if (node instanceof CrdtLoroText) {
|
|
680
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: new CrdtLoroText("") });
|
|
681
|
+
}
|
|
682
|
+
return acc;
|
|
683
|
+
}, {})
|
|
684
|
+
);
|
|
685
|
+
}
|
|
686
|
+
getInitialized(initialStorage) {
|
|
687
|
+
var _a;
|
|
688
|
+
return new CrdtLoroDoc(
|
|
689
|
+
(_a = initialStorage == null ? void 0 : initialStorage()) != null ? _a : this._initialStorage()
|
|
690
|
+
);
|
|
691
|
+
}
|
|
692
|
+
};
|
|
693
|
+
|
|
694
|
+
// src/doc/doc.ts
|
|
695
|
+
var doc = (value = () => ({})) => {
|
|
696
|
+
return new CrdtLoroDocFactory(value);
|
|
697
|
+
};
|
|
698
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
699
|
+
0 && (module.exports = {
|
|
700
|
+
loro
|
|
701
|
+
});
|