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