@pluv/crdt-yjs 3.2.2 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +21 -16
- package/CHANGELOG.md +27 -0
- package/dist/chunk-15K8U1wQ.mjs +18 -0
- package/dist/index.d.mts +155 -150
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +547 -567
- package/dist/index.mjs.map +1 -0
- package/package.json +21 -17
- package/src/awareness/PluvYjsAwareness.ts +1 -1
- package/tsdown.config.ts +14 -0
- package/dist/index.d.ts +0 -205
- package/dist/index.js +0 -617
package/dist/index.mjs
CHANGED
|
@@ -1,611 +1,591 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var __typeError = (msg) => {
|
|
8
|
-
throw TypeError(msg);
|
|
9
|
-
};
|
|
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 __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
28
|
-
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
29
|
-
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
30
|
-
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
31
|
-
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
32
|
-
|
|
33
|
-
// src/yjs.ts
|
|
34
|
-
var yjs_exports = {};
|
|
35
|
-
__export(yjs_exports, {
|
|
36
|
-
array: () => array,
|
|
37
|
-
awareness: () => awareness,
|
|
38
|
-
doc: () => doc,
|
|
39
|
-
kind: () => kind,
|
|
40
|
-
map: () => map,
|
|
41
|
-
provider: () => provider,
|
|
42
|
-
text: () => text,
|
|
43
|
-
xmlElement: () => xmlElement,
|
|
44
|
-
xmlFragment: () => xmlFragment,
|
|
45
|
-
xmlText: () => xmlText
|
|
46
|
-
});
|
|
1
|
+
import { t as __exportAll } from "./chunk-15K8U1wQ.mjs";
|
|
2
|
+
import { Array, Doc, Map as Map$1, Text, UndoManager, XmlElement, XmlFragment, XmlText, applyUpdate, encodeStateAsUpdate, mergeUpdates } from "yjs";
|
|
3
|
+
import { ObservableV2 } from "lib0/observable";
|
|
4
|
+
import { fromUint8Array, toUint8Array } from "js-base64";
|
|
5
|
+
import { AbstractCrdtDocFactory } from "@pluv/crdt";
|
|
6
|
+
import { StorageState } from "@pluv/types";
|
|
47
7
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
8
|
+
//#region src/array/YjsArray.ts
|
|
9
|
+
var YjsArray = class extends Array {
|
|
10
|
+
initialValue;
|
|
11
|
+
constructor(value = []) {
|
|
12
|
+
super();
|
|
13
|
+
this.insert(0, value);
|
|
14
|
+
this.initialValue = value;
|
|
15
|
+
}
|
|
16
|
+
__pluvType() {
|
|
17
|
+
return {};
|
|
18
|
+
}
|
|
59
19
|
};
|
|
60
20
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/array/array.ts
|
|
23
|
+
const array = (value = []) => {
|
|
24
|
+
return new YjsArray(value);
|
|
64
25
|
};
|
|
65
26
|
|
|
66
|
-
|
|
67
|
-
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region src/constants.ts
|
|
29
|
+
const PLUV_PRESENCE_META_KEY = "__pluv";
|
|
30
|
+
const PLUV_PRESENCE_Y_ID_KEY = "__y_clientid";
|
|
68
31
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
var PLUV_PRESENCE_Y_ID_KEY = "__y_clientid";
|
|
72
|
-
|
|
73
|
-
// src/awareness/PluvYjsAwareness.ts
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/awareness/PluvYjsAwareness.ts
|
|
74
34
|
var PluvYjsAwareness = class extends ObservableV2 {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
35
|
+
doc;
|
|
36
|
+
_field = null;
|
|
37
|
+
_idMap = /* @__PURE__ */ new Map();
|
|
38
|
+
_room;
|
|
39
|
+
_unsubscribe;
|
|
40
|
+
get clientID() {
|
|
41
|
+
return this.doc.clientID;
|
|
42
|
+
}
|
|
43
|
+
get states() {
|
|
44
|
+
return this.getStates();
|
|
45
|
+
}
|
|
46
|
+
constructor(params) {
|
|
47
|
+
super();
|
|
48
|
+
const { presenceField, room } = params;
|
|
49
|
+
this.doc = room.getDoc().value;
|
|
50
|
+
this._room = room;
|
|
51
|
+
if (!!presenceField) this._field = presenceField;
|
|
52
|
+
const myMeta = this._room.getMyPresence()?.[PLUV_PRESENCE_META_KEY] || {};
|
|
53
|
+
this._room.updateMyPresence({ [PLUV_PRESENCE_META_KEY]: {
|
|
54
|
+
...myMeta,
|
|
55
|
+
[PLUV_PRESENCE_Y_ID_KEY]: this.clientID
|
|
56
|
+
} });
|
|
57
|
+
this._unsubscribe = this._room.subscribe.others((others, event) => {
|
|
58
|
+
const updates = this._deriveUpdates(others, event);
|
|
59
|
+
if (!updates) return;
|
|
60
|
+
this.emit("change", [updates, "presence"]);
|
|
61
|
+
this.emit("update", [updates, "presence"]);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
destroy() {
|
|
65
|
+
this.emit("destroy", [this]);
|
|
66
|
+
this._unsubscribe();
|
|
67
|
+
super.destroy();
|
|
68
|
+
}
|
|
69
|
+
getLocalState() {
|
|
70
|
+
return this._adaptPresence(this._room.getMyPresence());
|
|
71
|
+
}
|
|
72
|
+
setLocalState(state) {
|
|
73
|
+
const patch = !!this._field ? { [this._field]: state } : state;
|
|
74
|
+
this._room.updateMyPresence(patch);
|
|
75
|
+
this.emit("update", [{
|
|
76
|
+
added: [],
|
|
77
|
+
updated: [this.clientID],
|
|
78
|
+
removed: []
|
|
79
|
+
}, "local"]);
|
|
80
|
+
}
|
|
81
|
+
setLocalStateField(field, value) {
|
|
82
|
+
/**
|
|
83
|
+
* !HACK
|
|
84
|
+
* @description Cast to unknown first, because field loses its type of TField when set in
|
|
85
|
+
* an object.
|
|
86
|
+
* @date May 14, 2025
|
|
87
|
+
*/
|
|
88
|
+
const patch = !!this._field ? { [this._field]: {
|
|
89
|
+
...this.getLocalState(),
|
|
90
|
+
[field]: value
|
|
91
|
+
} } : { [field]: value };
|
|
92
|
+
this._room.updateMyPresence(patch);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* !HACK
|
|
96
|
+
* @description We're building the awareness presences from the states stored on the room's
|
|
97
|
+
* presense instead of using the map defined in the awareness interface.
|
|
98
|
+
* @see https://github.com/yjs/y-protocols/blob/master/awareness.js
|
|
99
|
+
* @date May 13, 2025
|
|
100
|
+
*/
|
|
101
|
+
getStates() {
|
|
102
|
+
const states = this._room.getOthers().reduce((map$1, other) => {
|
|
103
|
+
const presence = this._adaptPresence(other.presence);
|
|
104
|
+
const clientID = (other.presence?.[PLUV_PRESENCE_META_KEY] || {})[PLUV_PRESENCE_Y_ID_KEY];
|
|
105
|
+
return typeof presence !== "undefined" && typeof clientID !== "undefined" ? map$1.set(clientID, presence) : map$1;
|
|
106
|
+
}, /* @__PURE__ */ new Map());
|
|
107
|
+
const myPresence = this.getLocalState();
|
|
108
|
+
if (typeof myPresence !== "undefined") states.set(this.clientID, myPresence);
|
|
109
|
+
return states;
|
|
110
|
+
}
|
|
111
|
+
_adaptPresence(presence) {
|
|
112
|
+
return !!this._field ? presence[this._field] : presence;
|
|
113
|
+
}
|
|
114
|
+
_deriveUpdates(others, event) {
|
|
115
|
+
switch (event.kind) {
|
|
116
|
+
case "clear":
|
|
117
|
+
this._idMap.clear();
|
|
118
|
+
return null;
|
|
119
|
+
case "enter": {
|
|
120
|
+
const clientID = this._resetIdMap(others).get(event.user.connectionId);
|
|
121
|
+
return {
|
|
122
|
+
added: typeof clientID !== "undefined" ? [clientID] : [],
|
|
123
|
+
updated: [],
|
|
124
|
+
removed: []
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
case "leave": {
|
|
128
|
+
const clientID = this._idMap.get(event.user.connectionId);
|
|
129
|
+
this._resetIdMap(others);
|
|
130
|
+
return {
|
|
131
|
+
added: [],
|
|
132
|
+
updated: [],
|
|
133
|
+
removed: typeof clientID !== "undefined" ? [clientID] : []
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
case "sync":
|
|
137
|
+
this._resetIdMap(others);
|
|
138
|
+
return null;
|
|
139
|
+
case "update": {
|
|
140
|
+
const clientID = this._resetIdMap(others).get(event.user.connectionId);
|
|
141
|
+
return {
|
|
142
|
+
added: [],
|
|
143
|
+
updated: typeof clientID !== "undefined" ? [clientID] : [],
|
|
144
|
+
removed: []
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
default: return null;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
_resetIdMap(others) {
|
|
151
|
+
this._idMap.clear();
|
|
152
|
+
return others.reduce((map$1, other) => {
|
|
153
|
+
const clientID = other.presence[PLUV_PRESENCE_META_KEY]?.[PLUV_PRESENCE_Y_ID_KEY];
|
|
154
|
+
if (typeof clientID === "undefined") return map$1;
|
|
155
|
+
return map$1.set(other.connectionId, clientID);
|
|
156
|
+
}, this._idMap);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* !HACK
|
|
160
|
+
* @description We dgaf about these. We're just mirroring the fields that exist in the
|
|
161
|
+
* og awareness to make sure everything aligns.
|
|
162
|
+
* @see https://github.com/yjs/y-protocols/blob/master/awareness.js
|
|
163
|
+
* @date May 13, 2025
|
|
164
|
+
*/
|
|
165
|
+
meta = /* @__PURE__ */ new Map();
|
|
166
|
+
_checkInterval = 0;
|
|
200
167
|
};
|
|
201
168
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
169
|
+
//#endregion
|
|
170
|
+
//#region src/awareness/awareness.ts
|
|
171
|
+
const awareness = (params) => {
|
|
172
|
+
return new PluvYjsAwareness(params);
|
|
205
173
|
};
|
|
206
174
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
const text2 = doc2.getText(name);
|
|
243
|
-
if (!!value) text2.insert(0, value);
|
|
244
|
-
return text2;
|
|
245
|
-
},
|
|
246
|
-
xmlElement(name, children = []) {
|
|
247
|
-
const xmlElement2 = doc2.getXmlElement(name);
|
|
248
|
-
if (!!children.length) xmlElement2.insert(0, children);
|
|
249
|
-
return xmlElement2;
|
|
250
|
-
},
|
|
251
|
-
xmlFragment(name, children = []) {
|
|
252
|
-
const xmlFragment2 = doc2.getXmlFragment(name);
|
|
253
|
-
if (!!children.length) xmlFragment2.insert(0, children);
|
|
254
|
-
return xmlFragment2;
|
|
255
|
-
},
|
|
256
|
-
xmlText(name) {
|
|
257
|
-
const xmlText2 = doc2.get(name, YXmlText);
|
|
258
|
-
return xmlText2;
|
|
259
|
-
}
|
|
260
|
-
};
|
|
175
|
+
//#endregion
|
|
176
|
+
//#region src/doc/builder.ts
|
|
177
|
+
const builder = (doc$1) => {
|
|
178
|
+
return {
|
|
179
|
+
array(name, value = []) {
|
|
180
|
+
const array$1 = doc$1.getArray(name);
|
|
181
|
+
if (!!value.length) array$1.insert(0, value);
|
|
182
|
+
return array$1;
|
|
183
|
+
},
|
|
184
|
+
map(name, value = []) {
|
|
185
|
+
const map$1 = doc$1.getMap(name);
|
|
186
|
+
value.forEach(([k, v]) => {
|
|
187
|
+
map$1.set(k, v);
|
|
188
|
+
});
|
|
189
|
+
return map$1;
|
|
190
|
+
},
|
|
191
|
+
text(name, value = "") {
|
|
192
|
+
const text$1 = doc$1.getText(name);
|
|
193
|
+
if (!!value) text$1.insert(0, value);
|
|
194
|
+
return text$1;
|
|
195
|
+
},
|
|
196
|
+
xmlElement(name, children = []) {
|
|
197
|
+
const xmlElement$1 = doc$1.getXmlElement(name);
|
|
198
|
+
if (!!children.length) xmlElement$1.insert(0, children);
|
|
199
|
+
return xmlElement$1;
|
|
200
|
+
},
|
|
201
|
+
xmlFragment(name, children = []) {
|
|
202
|
+
const xmlFragment$1 = doc$1.getXmlFragment(name);
|
|
203
|
+
if (!!children.length) xmlFragment$1.insert(0, children);
|
|
204
|
+
return xmlFragment$1;
|
|
205
|
+
},
|
|
206
|
+
xmlText(name) {
|
|
207
|
+
return doc$1.get(name, XmlText);
|
|
208
|
+
}
|
|
209
|
+
};
|
|
261
210
|
};
|
|
262
211
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
212
|
+
//#endregion
|
|
213
|
+
//#region src/doc/CrdtYjsDoc.ts
|
|
214
|
+
const MERGE_INTERVAL_MS = 1e3;
|
|
215
|
+
const PLUV_ID_FIELD = "__$pluv";
|
|
267
216
|
var CrdtYjsDoc = class {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
217
|
+
value = new Doc();
|
|
218
|
+
#_storage;
|
|
219
|
+
#_undoManager = null;
|
|
220
|
+
constructor(params = () => ({})) {
|
|
221
|
+
const storage = params(builder(this.value));
|
|
222
|
+
const keys = this.value.share.keys().reduce((set, key) => set.add(key), /* @__PURE__ */ new Set());
|
|
223
|
+
this.#_storage = Object.entries(storage).reduce((acc, [key, node]) => keys.has(key) ? {
|
|
224
|
+
...acc,
|
|
225
|
+
[key]: node
|
|
226
|
+
} : acc, {});
|
|
227
|
+
if (!!Object.keys(storage).length) this.#_setPluvId();
|
|
228
|
+
}
|
|
229
|
+
applyEncodedState(params) {
|
|
230
|
+
const { origin, update } = params;
|
|
231
|
+
if (update === null || typeof update === "undefined") return this;
|
|
232
|
+
const uint8Update = typeof update === "string" ? toUint8Array(update) : update;
|
|
233
|
+
applyUpdate(this.value, uint8Update, origin);
|
|
234
|
+
return this;
|
|
235
|
+
}
|
|
236
|
+
batchApplyEncodedState(params) {
|
|
237
|
+
const { origin } = params;
|
|
238
|
+
const filtered = (params.updates ?? []).reduce((acc, update) => {
|
|
239
|
+
if (!update) return acc;
|
|
240
|
+
if (typeof update === "string") {
|
|
241
|
+
acc.push(toUint8Array(update));
|
|
242
|
+
return acc;
|
|
243
|
+
}
|
|
244
|
+
if (typeof update === "object") {
|
|
245
|
+
acc.push(update);
|
|
246
|
+
return acc;
|
|
247
|
+
}
|
|
248
|
+
return acc;
|
|
249
|
+
}, []);
|
|
250
|
+
if (!filtered.length) return this;
|
|
251
|
+
const merged = mergeUpdates(filtered);
|
|
252
|
+
return this.applyEncodedState({
|
|
253
|
+
origin,
|
|
254
|
+
update: merged
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
canRedo() {
|
|
258
|
+
return !!this.#_undoManager?.canRedo();
|
|
259
|
+
}
|
|
260
|
+
canUndo() {
|
|
261
|
+
return !!this.#_undoManager?.canUndo();
|
|
262
|
+
}
|
|
263
|
+
destroy() {
|
|
264
|
+
this.#_undoManager?.destroy();
|
|
265
|
+
this.value.destroy();
|
|
266
|
+
}
|
|
267
|
+
get(type) {
|
|
268
|
+
if (typeof type === "undefined") return this.#_storage;
|
|
269
|
+
return this.#_storage[type];
|
|
270
|
+
}
|
|
271
|
+
getEncodedState() {
|
|
272
|
+
return fromUint8Array(encodeStateAsUpdate(this.value));
|
|
273
|
+
}
|
|
274
|
+
isEmpty() {
|
|
275
|
+
return !this.value.share.size;
|
|
276
|
+
}
|
|
277
|
+
rebuildStorage(reference) {
|
|
278
|
+
if (!!Object.keys(this.#_storage).length) {
|
|
279
|
+
console.warn("Attempted to rebuild storage multiple times");
|
|
280
|
+
return this;
|
|
281
|
+
}
|
|
282
|
+
this.#_storage = Object.entries(reference).reduce((acc, [key, node]) => {
|
|
283
|
+
/**
|
|
284
|
+
* @description It is important that the XML shared-types be checked before the others
|
|
285
|
+
* because they extend off the non-xml types (thereby you can mistakenly identify the
|
|
286
|
+
* wrong type if checked in the reverse order)
|
|
287
|
+
* @date May 9 ,2025
|
|
288
|
+
*/
|
|
289
|
+
if (node instanceof XmlElement) return {
|
|
290
|
+
...acc,
|
|
291
|
+
[key]: this.value.getXmlElement(key)
|
|
292
|
+
};
|
|
293
|
+
if (node instanceof XmlFragment) return {
|
|
294
|
+
...acc,
|
|
295
|
+
[key]: this.value.getXmlFragment(key)
|
|
296
|
+
};
|
|
297
|
+
if (node instanceof XmlText) return {
|
|
298
|
+
...acc,
|
|
299
|
+
[key]: this.value.get(key, XmlText)
|
|
300
|
+
};
|
|
301
|
+
if (node instanceof Array) return {
|
|
302
|
+
...acc,
|
|
303
|
+
[key]: this.value.getArray(key)
|
|
304
|
+
};
|
|
305
|
+
if (node instanceof Map$1) return {
|
|
306
|
+
...acc,
|
|
307
|
+
[key]: this.value.getMap(key)
|
|
308
|
+
};
|
|
309
|
+
if (node instanceof Text) return {
|
|
310
|
+
...acc,
|
|
311
|
+
[key]: this.value.getText(key)
|
|
312
|
+
};
|
|
313
|
+
return acc;
|
|
314
|
+
}, {});
|
|
315
|
+
return this.track();
|
|
316
|
+
}
|
|
317
|
+
redo() {
|
|
318
|
+
this.#_undoManager?.redo();
|
|
319
|
+
return this;
|
|
320
|
+
}
|
|
321
|
+
subscribe(listener) {
|
|
322
|
+
const fn = (update, origin, doc$1) => {
|
|
323
|
+
listener({
|
|
324
|
+
doc: this,
|
|
325
|
+
local: origin === null || typeof origin === "undefined",
|
|
326
|
+
origin: origin ? String(origin) : null,
|
|
327
|
+
update: fromUint8Array(update)
|
|
328
|
+
});
|
|
329
|
+
};
|
|
330
|
+
this.value.on("update", fn);
|
|
331
|
+
return () => {
|
|
332
|
+
this.value.off("update", fn);
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
track() {
|
|
336
|
+
if (this.#_undoManager) this.#_undoManager.destroy();
|
|
337
|
+
const sharedTypes = Object.values(this.#_storage).reduce((acc, type) => {
|
|
338
|
+
if (type instanceof Array || type instanceof Map$1 || type instanceof Text || type instanceof XmlElement || type instanceof XmlFragment || type instanceof XmlText) acc.push(type);
|
|
339
|
+
return acc;
|
|
340
|
+
}, []);
|
|
341
|
+
if (!sharedTypes.length) {
|
|
342
|
+
this.#_undoManager?.destroy();
|
|
343
|
+
this.#_undoManager = null;
|
|
344
|
+
return this;
|
|
345
|
+
}
|
|
346
|
+
this.#_undoManager = new UndoManager(sharedTypes, { captureTimeout: MERGE_INTERVAL_MS });
|
|
347
|
+
return this;
|
|
348
|
+
}
|
|
349
|
+
transact(fn) {
|
|
350
|
+
this.value.transact(fn);
|
|
351
|
+
return this;
|
|
352
|
+
}
|
|
353
|
+
toJson(type) {
|
|
354
|
+
if (typeof type === "string") return this.#_storage[type].toJSON();
|
|
355
|
+
return Object.entries(this.#_storage).reduce((acc, [key, value]) => ({
|
|
356
|
+
...acc,
|
|
357
|
+
[key]: value.toJSON()
|
|
358
|
+
}), {});
|
|
359
|
+
}
|
|
360
|
+
undo() {
|
|
361
|
+
this.#_undoManager?.undo();
|
|
362
|
+
return this;
|
|
363
|
+
}
|
|
364
|
+
#_setPluvId() {
|
|
365
|
+
const text$1 = this.value.getText(PLUV_ID_FIELD);
|
|
366
|
+
const id = typeof crypto !== "undefined" ? crypto.randomUUID() : Math.random().toString();
|
|
367
|
+
text$1.insert(0, id);
|
|
368
|
+
}
|
|
415
369
|
};
|
|
416
370
|
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
var CrdtYjsDocFactory = class
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
371
|
+
//#endregion
|
|
372
|
+
//#region src/doc/CrdtYjsDocFactory.ts
|
|
373
|
+
var CrdtYjsDocFactory = class CrdtYjsDocFactory extends AbstractCrdtDocFactory {
|
|
374
|
+
_initialStorage;
|
|
375
|
+
constructor(initialStorage = () => ({})) {
|
|
376
|
+
super(initialStorage);
|
|
377
|
+
this._initialStorage = initialStorage;
|
|
378
|
+
}
|
|
379
|
+
getEmpty() {
|
|
380
|
+
return new CrdtYjsDoc();
|
|
381
|
+
}
|
|
382
|
+
getFactory(initialStorage) {
|
|
383
|
+
return new CrdtYjsDocFactory(initialStorage ?? this._initialStorage);
|
|
384
|
+
}
|
|
385
|
+
getInitialized(initialStorage) {
|
|
386
|
+
return new CrdtYjsDoc(initialStorage ?? this._initialStorage);
|
|
387
|
+
}
|
|
433
388
|
};
|
|
434
389
|
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
390
|
+
//#endregion
|
|
391
|
+
//#region src/doc/doc.ts
|
|
392
|
+
const doc = (value = () => ({})) => {
|
|
393
|
+
return new CrdtYjsDocFactory(value);
|
|
438
394
|
};
|
|
439
395
|
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
var YjsMap = class extends
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
396
|
+
//#endregion
|
|
397
|
+
//#region src/map/YjsMap.ts
|
|
398
|
+
var YjsMap = class extends Map$1 {
|
|
399
|
+
initialValue;
|
|
400
|
+
constructor(value = []) {
|
|
401
|
+
super(value);
|
|
402
|
+
this.initialValue = value;
|
|
403
|
+
}
|
|
404
|
+
__pluvType() {
|
|
405
|
+
return {};
|
|
406
|
+
}
|
|
450
407
|
};
|
|
451
408
|
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
409
|
+
//#endregion
|
|
410
|
+
//#region src/map/map.ts
|
|
411
|
+
const map = (value = []) => {
|
|
412
|
+
return new YjsMap(value);
|
|
455
413
|
};
|
|
456
414
|
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
}
|
|
415
|
+
//#endregion
|
|
416
|
+
//#region src/provider/PluvYjsProvider.ts
|
|
417
|
+
/**
|
|
418
|
+
* !HACK
|
|
419
|
+
* @description This doesn't really do anything but emit states in a Yjs.Provider compatible way.
|
|
420
|
+
* Connections and document syncing are handled by the PluvRoom already.
|
|
421
|
+
* @date May 13, 2025
|
|
422
|
+
*/
|
|
423
|
+
var PluvYjsProvider = class extends ObservableV2 {
|
|
424
|
+
awareness;
|
|
425
|
+
doc;
|
|
426
|
+
_room;
|
|
427
|
+
_unsubscribe;
|
|
428
|
+
_synced = false;
|
|
429
|
+
constructor(params) {
|
|
430
|
+
super();
|
|
431
|
+
const { presenceField, room } = params;
|
|
432
|
+
this.awareness = awareness({
|
|
433
|
+
presenceField,
|
|
434
|
+
room
|
|
435
|
+
});
|
|
436
|
+
this.doc = room.getDoc().value;
|
|
437
|
+
this._room = room;
|
|
438
|
+
this._unsubscribe = [
|
|
439
|
+
this._room.subscribe.connection(({ storage }) => {
|
|
440
|
+
const status = this._toProviderStatus(storage.state);
|
|
441
|
+
if (!status) return;
|
|
442
|
+
this.emit("status", [{ status }]);
|
|
443
|
+
}),
|
|
444
|
+
this._room.addEventListener("close", (event) => {
|
|
445
|
+
this.emit("connection-close", [event, this]);
|
|
446
|
+
}),
|
|
447
|
+
this._room.addEventListener("error", (event) => {
|
|
448
|
+
this.emit("connection-error", [event, this]);
|
|
449
|
+
})
|
|
450
|
+
].reduce((fn, unsubscribe) => () => {
|
|
451
|
+
fn();
|
|
452
|
+
unsubscribe();
|
|
453
|
+
}, () => void 0);
|
|
454
|
+
}
|
|
455
|
+
get synced() {
|
|
456
|
+
return this._synced;
|
|
457
|
+
}
|
|
458
|
+
set synced(state) {
|
|
459
|
+
if (this._synced === state) return;
|
|
460
|
+
this._synced = state;
|
|
461
|
+
this.emit("sync", [state]);
|
|
462
|
+
this.emit("synced", [state]);
|
|
463
|
+
}
|
|
464
|
+
destroy() {
|
|
465
|
+
this.awareness.destroy();
|
|
466
|
+
this._unsubscribe();
|
|
467
|
+
super.destroy();
|
|
468
|
+
}
|
|
469
|
+
_toProviderStatus(state) {
|
|
470
|
+
switch (state) {
|
|
471
|
+
case StorageState.Loading: return "connecting";
|
|
472
|
+
case StorageState.Unavailable: return "disconnected";
|
|
473
|
+
case StorageState.Offline:
|
|
474
|
+
case StorageState.Synced: return "connected";
|
|
475
|
+
default: return null;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* !HACK
|
|
480
|
+
* @description We dgaf about these, since the room manages these already. We're just mirroring
|
|
481
|
+
* the fields that exist in the og provider to make sure everything aligns.
|
|
482
|
+
* @see https://github.com/yjs/y-websocket/blob/master/src/y-websocket.js
|
|
483
|
+
* @date May 13, 2025
|
|
484
|
+
*/
|
|
485
|
+
connect() {}
|
|
486
|
+
disconnect() {}
|
|
530
487
|
};
|
|
531
488
|
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
489
|
+
//#endregion
|
|
490
|
+
//#region src/provider/provider.ts
|
|
491
|
+
const provider = (params) => {
|
|
492
|
+
return new PluvYjsProvider(params);
|
|
535
493
|
};
|
|
536
494
|
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
var YjsText = class extends
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
495
|
+
//#endregion
|
|
496
|
+
//#region src/text/YjsText.ts
|
|
497
|
+
var YjsText = class extends Text {
|
|
498
|
+
initialValue;
|
|
499
|
+
constructor(value = "") {
|
|
500
|
+
super();
|
|
501
|
+
this.initialValue = value;
|
|
502
|
+
}
|
|
503
|
+
__pluvType() {
|
|
504
|
+
return {};
|
|
505
|
+
}
|
|
547
506
|
};
|
|
548
507
|
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
508
|
+
//#endregion
|
|
509
|
+
//#region src/text/text.ts
|
|
510
|
+
const text = (value = "") => {
|
|
511
|
+
return new YjsText(value);
|
|
552
512
|
};
|
|
553
513
|
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
var YjsXmlElement = class extends
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
514
|
+
//#endregion
|
|
515
|
+
//#region src/xmlElement/YjsXmlElement.ts
|
|
516
|
+
var YjsXmlElement = class extends XmlElement {
|
|
517
|
+
initialValue;
|
|
518
|
+
constructor(name, value) {
|
|
519
|
+
super(name);
|
|
520
|
+
if (!!value.length) this.insert(0, value);
|
|
521
|
+
this.initialValue = value;
|
|
522
|
+
}
|
|
523
|
+
__pluvType() {
|
|
524
|
+
return {};
|
|
525
|
+
}
|
|
565
526
|
};
|
|
566
527
|
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
528
|
+
//#endregion
|
|
529
|
+
//#region src/xmlElement/xmlElement.ts
|
|
530
|
+
const xmlElement = (name, children) => {
|
|
531
|
+
return new YjsXmlElement(name, children);
|
|
570
532
|
};
|
|
571
533
|
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
var YjsXmlFragment = class extends
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
534
|
+
//#endregion
|
|
535
|
+
//#region src/xmlFragment/YjsXmlFragment.ts
|
|
536
|
+
var YjsXmlFragment = class extends XmlFragment {
|
|
537
|
+
initialValue;
|
|
538
|
+
constructor(value) {
|
|
539
|
+
super();
|
|
540
|
+
if (!!value.length) this.insert(0, value);
|
|
541
|
+
this.initialValue = value;
|
|
542
|
+
}
|
|
543
|
+
__pluvType() {
|
|
544
|
+
return {};
|
|
545
|
+
}
|
|
583
546
|
};
|
|
584
547
|
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
548
|
+
//#endregion
|
|
549
|
+
//#region src/xmlFragment/xmlFragment.ts
|
|
550
|
+
const xmlFragment = (children) => {
|
|
551
|
+
return new YjsXmlFragment(children);
|
|
588
552
|
};
|
|
589
553
|
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
var YjsXmlText = class extends
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
554
|
+
//#endregion
|
|
555
|
+
//#region src/xmlText/YjsXmlText.ts
|
|
556
|
+
var YjsXmlText = class extends XmlText {
|
|
557
|
+
initialValue;
|
|
558
|
+
constructor(value = "") {
|
|
559
|
+
super();
|
|
560
|
+
this.initialValue = value;
|
|
561
|
+
}
|
|
562
|
+
__pluvType() {
|
|
563
|
+
return {};
|
|
564
|
+
}
|
|
600
565
|
};
|
|
601
566
|
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
567
|
+
//#endregion
|
|
568
|
+
//#region src/xmlText/xmlText.ts
|
|
569
|
+
const xmlText = () => {
|
|
570
|
+
return new YjsXmlText();
|
|
605
571
|
};
|
|
606
572
|
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
573
|
+
//#endregion
|
|
574
|
+
//#region src/yjs.ts
|
|
575
|
+
var yjs_exports = /* @__PURE__ */ __exportAll({
|
|
576
|
+
array: () => array,
|
|
577
|
+
awareness: () => awareness,
|
|
578
|
+
doc: () => doc,
|
|
579
|
+
kind: () => kind,
|
|
580
|
+
map: () => map,
|
|
581
|
+
provider: () => provider,
|
|
582
|
+
text: () => text,
|
|
583
|
+
xmlElement: () => xmlElement,
|
|
584
|
+
xmlFragment: () => xmlFragment,
|
|
585
|
+
xmlText: () => xmlText
|
|
586
|
+
});
|
|
587
|
+
const kind = "yjs";
|
|
588
|
+
|
|
589
|
+
//#endregion
|
|
590
|
+
export { yjs_exports as yjs };
|
|
591
|
+
//# sourceMappingURL=index.mjs.map
|