@coaction/yjs 1.4.1 → 2.0.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/README.md +2 -2
- package/dist/index.d.mts +28 -22
- package/dist/index.d.ts +28 -22
- package/dist/index.js +715 -545
- package/dist/index.mjs +690 -539
- package/package.json +31 -39
package/dist/index.mjs
CHANGED
|
@@ -1,577 +1,728 @@
|
|
|
1
|
-
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
18
|
-
|
|
19
|
-
// index.ts
|
|
20
|
-
var index_exports = {};
|
|
21
|
-
__export(index_exports, {
|
|
22
|
-
__unsafeTestOnly__: () => __unsafeTestOnly__,
|
|
23
|
-
bindYjs: () => bindYjs,
|
|
24
|
-
yjs: () => yjs
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
// src/index.ts
|
|
28
|
-
var src_exports = {};
|
|
29
|
-
__export(src_exports, {
|
|
30
|
-
__unsafeTestOnly__: () => __unsafeTestOnly__,
|
|
31
|
-
bindYjs: () => bindYjs,
|
|
32
|
-
yjs: () => yjs
|
|
33
|
-
});
|
|
34
|
-
__reExport(src_exports, yjs_star);
|
|
1
|
+
import { onStoreReady } from "coaction";
|
|
35
2
|
import * as Y from "yjs";
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
3
|
+
export * from "yjs";
|
|
4
|
+
//#endregion
|
|
5
|
+
//#region packages/coaction-yjs/src/shared.ts
|
|
6
|
+
const scheduleMicrotask = (callback) => {
|
|
7
|
+
if (typeof queueMicrotask === "function") {
|
|
8
|
+
queueMicrotask(callback);
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
Promise.resolve().then(callback);
|
|
12
|
+
};
|
|
13
|
+
const isUnsafeKey = (key) => key === "__proto__" || key === "prototype" || key === "constructor";
|
|
14
|
+
const isUnsafePathSegment = (segment) => typeof segment === "string" && isUnsafeKey(segment);
|
|
15
|
+
const cloneFallback = (value, seen = /* @__PURE__ */ new WeakMap()) => {
|
|
16
|
+
if (typeof value !== "object" || value === null) return value;
|
|
17
|
+
const cached = seen.get(value);
|
|
18
|
+
if (cached) return cached;
|
|
19
|
+
if (Array.isArray(value)) {
|
|
20
|
+
const next = [];
|
|
21
|
+
seen.set(value, next);
|
|
22
|
+
for (let index = 0; index < value.length; index += 1) if (Object.prototype.hasOwnProperty.call(value, index)) next[index] = cloneFallback(value[index], seen);
|
|
23
|
+
return next;
|
|
24
|
+
}
|
|
25
|
+
if (isPlainObject(value)) {
|
|
26
|
+
const next = Object.create(Object.getPrototypeOf(value));
|
|
27
|
+
seen.set(value, next);
|
|
28
|
+
for (const key of Reflect.ownKeys(value)) if (Object.prototype.propertyIsEnumerable.call(value, key)) {
|
|
29
|
+
if (typeof key === "string" && isUnsafeKey(key)) continue;
|
|
30
|
+
next[key] = cloneFallback(value[key], seen);
|
|
31
|
+
}
|
|
32
|
+
return next;
|
|
33
|
+
}
|
|
34
|
+
return JSON.parse(JSON.stringify(value));
|
|
44
35
|
};
|
|
45
36
|
function clone(value) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
37
|
+
if (Array.isArray(value) || isPlainObject(value)) return cloneFallback(value);
|
|
38
|
+
if (typeof structuredClone === "function") try {
|
|
39
|
+
return structuredClone(value);
|
|
40
|
+
} catch {
|
|
41
|
+
return cloneFallback(value);
|
|
42
|
+
}
|
|
43
|
+
return cloneFallback(value);
|
|
50
44
|
}
|
|
51
45
|
function isPlainObject(value) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const prototype = Object.getPrototypeOf(value);
|
|
56
|
-
return prototype === Object.prototype || prototype === null;
|
|
46
|
+
if (typeof value !== "object" || value === null) return false;
|
|
47
|
+
const prototype = Object.getPrototypeOf(value);
|
|
48
|
+
return prototype === Object.prototype || prototype === null;
|
|
57
49
|
}
|
|
50
|
+
function sanitizePlainValue(value, seen = /* @__PURE__ */ new WeakMap()) {
|
|
51
|
+
if (typeof value !== "object" || value === null) return value;
|
|
52
|
+
const cached = seen.get(value);
|
|
53
|
+
if (cached) return cached;
|
|
54
|
+
if (Array.isArray(value)) {
|
|
55
|
+
const next = [];
|
|
56
|
+
seen.set(value, next);
|
|
57
|
+
for (let index = 0; index < value.length; index += 1) if (Object.prototype.hasOwnProperty.call(value, index)) next[index] = sanitizePlainValue(value[index], seen);
|
|
58
|
+
return next;
|
|
59
|
+
}
|
|
60
|
+
if (!isPlainObject(value)) return clone(value);
|
|
61
|
+
const next = {};
|
|
62
|
+
seen.set(value, next);
|
|
63
|
+
for (const key of Object.keys(value)) {
|
|
64
|
+
if (isUnsafeKey(key)) continue;
|
|
65
|
+
next[key] = sanitizePlainValue(value[key], seen);
|
|
66
|
+
}
|
|
67
|
+
return next;
|
|
68
|
+
}
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region packages/coaction-yjs/src/yjsValue.ts
|
|
58
71
|
function toPlainObject(value) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
72
|
+
const next = {};
|
|
73
|
+
value.forEach((item, key) => {
|
|
74
|
+
if (isUnsafeKey(key)) return;
|
|
75
|
+
next[key] = toPlainValue(item);
|
|
76
|
+
});
|
|
77
|
+
return next;
|
|
64
78
|
}
|
|
65
79
|
function toPlainArray(value) {
|
|
66
|
-
|
|
80
|
+
return value.toArray().map((item) => toPlainValue(item));
|
|
67
81
|
}
|
|
68
82
|
function toPlainValue(value) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
return value;
|
|
83
|
+
if (value instanceof Y.Map) return toPlainObject(value);
|
|
84
|
+
if (value instanceof Y.Array) return toPlainArray(value);
|
|
85
|
+
if (value instanceof Y.AbstractType) return toPlainValue(value.toJSON());
|
|
86
|
+
if (Array.isArray(value) || isPlainObject(value)) return sanitizePlainValue(value);
|
|
87
|
+
return value;
|
|
76
88
|
}
|
|
77
89
|
function createYMap(value) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
90
|
+
const next = new Y.Map();
|
|
91
|
+
for (const [key, item] of Object.entries(value)) {
|
|
92
|
+
if (isUnsafeKey(key)) continue;
|
|
93
|
+
next.set(key, toYValue(item));
|
|
94
|
+
}
|
|
95
|
+
return next;
|
|
83
96
|
}
|
|
84
97
|
function createYArray(value) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
0,
|
|
89
|
-
value.map((item) => toYValue(item))
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
return next;
|
|
98
|
+
const next = new Y.Array();
|
|
99
|
+
if (value.length > 0) next.insert(0, value.map((item) => toYValue(item)));
|
|
100
|
+
return next;
|
|
93
101
|
}
|
|
94
102
|
function toYValue(value) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
return createYMap(value);
|
|
100
|
-
}
|
|
101
|
-
if (typeof value === "object" && value !== null) {
|
|
102
|
-
return clone(value);
|
|
103
|
-
}
|
|
104
|
-
return value;
|
|
105
|
-
}
|
|
106
|
-
function isEqualValue(left, right) {
|
|
107
|
-
if (Object.is(left, right)) {
|
|
108
|
-
return true;
|
|
109
|
-
}
|
|
110
|
-
if (Array.isArray(left) && Array.isArray(right)) {
|
|
111
|
-
if (left.length !== right.length) {
|
|
112
|
-
return false;
|
|
113
|
-
}
|
|
114
|
-
for (let index = 0; index < left.length; index += 1) {
|
|
115
|
-
if (!isEqualValue(left[index], right[index])) {
|
|
116
|
-
return false;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
return true;
|
|
120
|
-
}
|
|
121
|
-
if (isPlainObject(left) && isPlainObject(right)) {
|
|
122
|
-
const leftKeys = Object.keys(left);
|
|
123
|
-
const rightKeys = Object.keys(right);
|
|
124
|
-
if (leftKeys.length !== rightKeys.length) {
|
|
125
|
-
return false;
|
|
126
|
-
}
|
|
127
|
-
for (const key of leftKeys) {
|
|
128
|
-
if (!Object.prototype.hasOwnProperty.call(right, key)) {
|
|
129
|
-
return false;
|
|
130
|
-
}
|
|
131
|
-
if (!isEqualValue(left[key], right[key])) {
|
|
132
|
-
return false;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
return true;
|
|
136
|
-
}
|
|
137
|
-
return false;
|
|
138
|
-
}
|
|
139
|
-
function syncObjectToYMap(target, previous, source) {
|
|
140
|
-
const keys = /* @__PURE__ */ new Set([...Object.keys(previous), ...Object.keys(source)]);
|
|
141
|
-
for (const key of keys) {
|
|
142
|
-
const hasPrevious = Object.prototype.hasOwnProperty.call(previous, key);
|
|
143
|
-
const hasNext = Object.prototype.hasOwnProperty.call(source, key);
|
|
144
|
-
if (!hasNext) {
|
|
145
|
-
target.delete(key);
|
|
146
|
-
continue;
|
|
147
|
-
}
|
|
148
|
-
const previousValue = hasPrevious ? previous[key] : void 0;
|
|
149
|
-
const nextValue = source[key];
|
|
150
|
-
if (hasPrevious && isEqualValue(previousValue, nextValue)) {
|
|
151
|
-
continue;
|
|
152
|
-
}
|
|
153
|
-
const current = target.get(key);
|
|
154
|
-
if (Array.isArray(nextValue)) {
|
|
155
|
-
if (Array.isArray(previousValue) && current instanceof Y.Array) {
|
|
156
|
-
syncArrayToYArray(current, previousValue, nextValue);
|
|
157
|
-
} else {
|
|
158
|
-
target.set(key, createYArray(nextValue));
|
|
159
|
-
}
|
|
160
|
-
continue;
|
|
161
|
-
}
|
|
162
|
-
if (isPlainObject(nextValue)) {
|
|
163
|
-
if (isPlainObject(previousValue) && current instanceof Y.Map) {
|
|
164
|
-
syncObjectToYMap(current, previousValue, nextValue);
|
|
165
|
-
} else {
|
|
166
|
-
target.set(key, createYMap(nextValue));
|
|
167
|
-
}
|
|
168
|
-
continue;
|
|
169
|
-
}
|
|
170
|
-
const normalized = typeof nextValue === "object" && nextValue !== null ? clone(nextValue) : nextValue;
|
|
171
|
-
if (!Object.is(current, normalized)) {
|
|
172
|
-
target.set(key, normalized);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
function syncArrayToYArray(target, previous, source) {
|
|
177
|
-
if (target.length > source.length) {
|
|
178
|
-
target.delete(source.length, target.length - source.length);
|
|
179
|
-
}
|
|
180
|
-
const maxLength = Math.max(previous.length, source.length);
|
|
181
|
-
for (let index = 0; index < maxLength; index += 1) {
|
|
182
|
-
const hasPrevious = index < previous.length;
|
|
183
|
-
const hasNext = index < source.length;
|
|
184
|
-
if (!hasNext) {
|
|
185
|
-
continue;
|
|
186
|
-
}
|
|
187
|
-
const previousValue = hasPrevious ? previous[index] : void 0;
|
|
188
|
-
const nextValue = source[index];
|
|
189
|
-
if (hasPrevious && isEqualValue(previousValue, nextValue)) {
|
|
190
|
-
continue;
|
|
191
|
-
}
|
|
192
|
-
if (index >= target.length) {
|
|
193
|
-
target.insert(index, [toYValue(nextValue)]);
|
|
194
|
-
continue;
|
|
195
|
-
}
|
|
196
|
-
const current = target.get(index);
|
|
197
|
-
if (Array.isArray(nextValue)) {
|
|
198
|
-
if (Array.isArray(previousValue) && current instanceof Y.Array) {
|
|
199
|
-
syncArrayToYArray(current, previousValue, nextValue);
|
|
200
|
-
} else {
|
|
201
|
-
target.delete(index, 1);
|
|
202
|
-
target.insert(index, [createYArray(nextValue)]);
|
|
203
|
-
}
|
|
204
|
-
continue;
|
|
205
|
-
}
|
|
206
|
-
if (isPlainObject(nextValue)) {
|
|
207
|
-
if (isPlainObject(previousValue) && current instanceof Y.Map) {
|
|
208
|
-
syncObjectToYMap(current, previousValue, nextValue);
|
|
209
|
-
} else {
|
|
210
|
-
target.delete(index, 1);
|
|
211
|
-
target.insert(index, [createYMap(nextValue)]);
|
|
212
|
-
}
|
|
213
|
-
continue;
|
|
214
|
-
}
|
|
215
|
-
const normalized = typeof nextValue === "object" && nextValue !== null ? clone(nextValue) : nextValue;
|
|
216
|
-
if (!Object.is(current, normalized)) {
|
|
217
|
-
target.delete(index, 1);
|
|
218
|
-
target.insert(index, [normalized]);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
103
|
+
if (Array.isArray(value)) return createYArray(value);
|
|
104
|
+
if (isPlainObject(value)) return createYMap(value);
|
|
105
|
+
if (typeof value === "object" && value !== null) return clone(value);
|
|
106
|
+
return value;
|
|
221
107
|
}
|
|
108
|
+
//#endregion
|
|
109
|
+
//#region packages/coaction-yjs/src/remoteOperations.ts
|
|
222
110
|
function isSetStateReentryError(error) {
|
|
223
|
-
|
|
111
|
+
return error instanceof Error && error.message === "setState cannot be called within the updater";
|
|
224
112
|
}
|
|
225
113
|
function cloneForStore(value) {
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
}
|
|
229
|
-
return value;
|
|
114
|
+
if (typeof value === "object" && value !== null) return sanitizePlainValue(value);
|
|
115
|
+
return value;
|
|
230
116
|
}
|
|
231
117
|
function toPathKey(path) {
|
|
232
|
-
|
|
118
|
+
return path.map((segment) => `${typeof segment}:${String(segment)}`).join("|");
|
|
119
|
+
}
|
|
120
|
+
function toArrayIndex(segment) {
|
|
121
|
+
const index = typeof segment === "number" ? segment : Number.parseInt(segment, 10);
|
|
122
|
+
if (!Number.isInteger(index) || index < 0) return;
|
|
123
|
+
if (typeof segment === "string" && String(index) !== segment) return;
|
|
124
|
+
return index;
|
|
233
125
|
}
|
|
234
126
|
function compactOperations(operations) {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
}
|
|
243
|
-
return Array.from(deduplicated.values()).sort(
|
|
244
|
-
(left, right) => left.path.length - right.path.length
|
|
245
|
-
);
|
|
127
|
+
const deduplicated = /* @__PURE__ */ new Map();
|
|
128
|
+
for (const operation of operations) {
|
|
129
|
+
const key = toPathKey(operation.path);
|
|
130
|
+
if (deduplicated.has(key)) deduplicated.delete(key);
|
|
131
|
+
deduplicated.set(key, operation);
|
|
132
|
+
}
|
|
133
|
+
return Array.from(deduplicated.values()).sort((left, right) => left.path.length - right.path.length);
|
|
246
134
|
}
|
|
247
135
|
function getYValueAtPath(root, path) {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
}
|
|
264
|
-
return current;
|
|
136
|
+
let current = root;
|
|
137
|
+
for (const segment of path) {
|
|
138
|
+
if (current instanceof Y.Map) {
|
|
139
|
+
current = current.get(String(segment));
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (current instanceof Y.Array) {
|
|
143
|
+
const index = toArrayIndex(segment);
|
|
144
|
+
if (typeof index === "undefined") return;
|
|
145
|
+
current = current.get(index);
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
return current;
|
|
265
151
|
}
|
|
266
152
|
function setAtPath(target, path, value) {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
153
|
+
if (path.length === 0) return;
|
|
154
|
+
if (path.some(isUnsafePathSegment)) return;
|
|
155
|
+
let current = target;
|
|
156
|
+
for (let index = 0; index < path.length - 1; index += 1) {
|
|
157
|
+
const segment = path[index];
|
|
158
|
+
const nextSegment = path[index + 1];
|
|
159
|
+
const segmentIndex = Array.isArray(current) ? toArrayIndex(segment) : void 0;
|
|
160
|
+
const targetKey = typeof segmentIndex === "undefined" ? segment : segmentIndex;
|
|
161
|
+
const nextValue = current[targetKey];
|
|
162
|
+
const needsArray = typeof nextSegment === "number" || Array.isArray(nextValue) && typeof toArrayIndex(nextSegment) !== "undefined";
|
|
163
|
+
if (typeof nextValue !== "object" || nextValue === null || (needsArray ? !Array.isArray(nextValue) : Array.isArray(nextValue))) current[targetKey] = typeof nextSegment === "number" ? [] : {};
|
|
164
|
+
current = current[targetKey];
|
|
165
|
+
}
|
|
166
|
+
const leaf = path[path.length - 1];
|
|
167
|
+
const leafIndex = Array.isArray(current) ? toArrayIndex(leaf) : void 0;
|
|
168
|
+
current[typeof leafIndex === "undefined" ? leaf : leafIndex] = cloneForStore(value);
|
|
282
169
|
}
|
|
283
170
|
function deleteAtPath(target, path) {
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
}
|
|
301
|
-
delete current[leaf];
|
|
171
|
+
if (path.length === 0) return;
|
|
172
|
+
if (path.some(isUnsafePathSegment)) return;
|
|
173
|
+
let current = target;
|
|
174
|
+
for (let index = 0; index < path.length - 1; index += 1) {
|
|
175
|
+
const segment = path[index];
|
|
176
|
+
const segmentIndex = Array.isArray(current) ? toArrayIndex(segment) : void 0;
|
|
177
|
+
current = current[typeof segmentIndex === "undefined" ? segment : segmentIndex];
|
|
178
|
+
if (typeof current !== "object" || current === null) return;
|
|
179
|
+
}
|
|
180
|
+
const leaf = path[path.length - 1];
|
|
181
|
+
if (Array.isArray(current)) {
|
|
182
|
+
const leafIndex = toArrayIndex(leaf);
|
|
183
|
+
if (typeof leafIndex !== "undefined" && leafIndex >= 0 && leafIndex < current.length) current.splice(leafIndex, 1);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
delete current[leaf];
|
|
302
187
|
}
|
|
303
188
|
function collectRemoteOperations(events, stateMap) {
|
|
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
|
-
|
|
189
|
+
const operations = [];
|
|
190
|
+
for (const event of events) {
|
|
191
|
+
if (event instanceof Y.YMapEvent) {
|
|
192
|
+
for (const changedKey of event.keysChanged) {
|
|
193
|
+
const path = [...event.path, changedKey];
|
|
194
|
+
if (path.some(isUnsafePathSegment)) continue;
|
|
195
|
+
if (event.changes.keys.get(changedKey)?.action === "delete") {
|
|
196
|
+
operations.push({
|
|
197
|
+
type: "delete",
|
|
198
|
+
path
|
|
199
|
+
});
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
operations.push({
|
|
203
|
+
type: "set",
|
|
204
|
+
path,
|
|
205
|
+
value: toPlainValue(getYValueAtPath(stateMap, path))
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
if (event instanceof Y.YArrayEvent) {
|
|
211
|
+
const path = [...event.path];
|
|
212
|
+
operations.push({
|
|
213
|
+
type: "set",
|
|
214
|
+
path,
|
|
215
|
+
value: toPlainValue(getYValueAtPath(stateMap, path))
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return operations;
|
|
335
220
|
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
221
|
+
//#endregion
|
|
222
|
+
//#region packages/coaction-yjs/src/sync.ts
|
|
223
|
+
function isEqualValue(left, right) {
|
|
224
|
+
if (Object.is(left, right)) return true;
|
|
225
|
+
if (Array.isArray(left) && Array.isArray(right)) {
|
|
226
|
+
if (left.length !== right.length) return false;
|
|
227
|
+
for (let index = 0; index < left.length; index += 1) if (!isEqualValue(left[index], right[index])) return false;
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
if (isPlainObject(left) && isPlainObject(right)) {
|
|
231
|
+
const leftKeys = Object.keys(left);
|
|
232
|
+
const rightKeys = Object.keys(right);
|
|
233
|
+
if (leftKeys.length !== rightKeys.length) return false;
|
|
234
|
+
for (const key of leftKeys) {
|
|
235
|
+
if (!Object.prototype.hasOwnProperty.call(right, key)) return false;
|
|
236
|
+
if (!isEqualValue(left[key], right[key])) return false;
|
|
237
|
+
}
|
|
238
|
+
return true;
|
|
239
|
+
}
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
function syncObjectToYMap(target, previous, source) {
|
|
243
|
+
target.forEach((_value, key) => {
|
|
244
|
+
if (isUnsafeKey(key)) target.delete(key);
|
|
245
|
+
});
|
|
246
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(previous), ...Object.keys(source)]);
|
|
247
|
+
for (const key of keys) {
|
|
248
|
+
if (isUnsafeKey(key)) {
|
|
249
|
+
target.delete(key);
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
const hasPrevious = Object.prototype.hasOwnProperty.call(previous, key);
|
|
253
|
+
if (!Object.prototype.hasOwnProperty.call(source, key)) {
|
|
254
|
+
target.delete(key);
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
const previousValue = hasPrevious ? previous[key] : void 0;
|
|
258
|
+
const nextValue = source[key];
|
|
259
|
+
if (hasPrevious && isEqualValue(previousValue, nextValue)) continue;
|
|
260
|
+
const current = target.get(key);
|
|
261
|
+
if (Array.isArray(nextValue)) {
|
|
262
|
+
if (Array.isArray(previousValue) && current instanceof Y.Array) syncArrayToYArray(current, previousValue, nextValue);
|
|
263
|
+
else target.set(key, createYArray(nextValue));
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
if (isPlainObject(nextValue)) {
|
|
267
|
+
if (isPlainObject(previousValue) && current instanceof Y.Map) syncObjectToYMap(current, previousValue, nextValue);
|
|
268
|
+
else target.set(key, createYMap(nextValue));
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
const normalized = typeof nextValue === "object" && nextValue !== null ? clone(nextValue) : nextValue;
|
|
272
|
+
if (!Object.is(current, normalized)) target.set(key, normalized);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
function syncArrayToYArray(target, previous, source) {
|
|
276
|
+
if (target.length > source.length) target.delete(source.length, target.length - source.length);
|
|
277
|
+
const maxLength = Math.max(previous.length, source.length);
|
|
278
|
+
for (let index = 0; index < maxLength; index += 1) {
|
|
279
|
+
const hasPrevious = index < previous.length;
|
|
280
|
+
if (!(index < source.length)) continue;
|
|
281
|
+
const previousValue = hasPrevious ? previous[index] : void 0;
|
|
282
|
+
const nextValue = source[index];
|
|
283
|
+
if (hasPrevious && isEqualValue(previousValue, nextValue)) continue;
|
|
284
|
+
if (index >= target.length) {
|
|
285
|
+
target.insert(index, [toYValue(nextValue)]);
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
const current = target.get(index);
|
|
289
|
+
if (Array.isArray(nextValue)) {
|
|
290
|
+
if (Array.isArray(previousValue) && current instanceof Y.Array) syncArrayToYArray(current, previousValue, nextValue);
|
|
291
|
+
else {
|
|
292
|
+
target.delete(index, 1);
|
|
293
|
+
target.insert(index, [createYArray(nextValue)]);
|
|
294
|
+
}
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
if (isPlainObject(nextValue)) {
|
|
298
|
+
if (isPlainObject(previousValue) && current instanceof Y.Map) syncObjectToYMap(current, previousValue, nextValue);
|
|
299
|
+
else {
|
|
300
|
+
target.delete(index, 1);
|
|
301
|
+
target.insert(index, [createYMap(nextValue)]);
|
|
302
|
+
}
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
const normalized = typeof nextValue === "object" && nextValue !== null ? clone(nextValue) : nextValue;
|
|
306
|
+
if (!Object.is(current, normalized)) {
|
|
307
|
+
target.delete(index, 1);
|
|
308
|
+
target.insert(index, [normalized]);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
//#endregion
|
|
313
|
+
//#region packages/coaction-yjs/src/index.ts
|
|
314
|
+
const STATE_KEY = "state";
|
|
315
|
+
const historySuppressionSymbol = Symbol.for("coaction.history.suppress");
|
|
316
|
+
const runWithoutHistoryRecording = (store, callback) => {
|
|
317
|
+
const runner = store[historySuppressionSymbol];
|
|
318
|
+
return typeof runner === "function" ? runner(callback) : callback();
|
|
319
|
+
};
|
|
320
|
+
const getOwnEnumerableKeys = (value) => Reflect.ownKeys(value).filter((key) => Object.prototype.propertyIsEnumerable.call(value, key) && !(typeof key === "string" && isUnsafeKey(key)));
|
|
321
|
+
const formatPropertyPath = (path) => path.length ? path.map((key) => String(key)).join(".") : "<root>";
|
|
322
|
+
var YjsSerializableStateError = class extends Error {};
|
|
323
|
+
const isYjsSerializableStateError = (error) => error instanceof YjsSerializableStateError;
|
|
324
|
+
const isArrayIndexKey = (key, length) => {
|
|
325
|
+
if (key === "") return false;
|
|
326
|
+
const index = Number(key);
|
|
327
|
+
return Number.isInteger(index) && index >= 0 && index < length && String(index) === key;
|
|
328
|
+
};
|
|
329
|
+
const findYjsStateViolation = (value, path = [], ancestors = /* @__PURE__ */ new WeakSet()) => {
|
|
330
|
+
switch (typeof value) {
|
|
331
|
+
case "symbol": return {
|
|
332
|
+
type: "symbol-value",
|
|
333
|
+
path
|
|
334
|
+
};
|
|
335
|
+
case "function": return {
|
|
336
|
+
type: "function",
|
|
337
|
+
path
|
|
338
|
+
};
|
|
339
|
+
default: break;
|
|
340
|
+
}
|
|
341
|
+
if (typeof value !== "object" || value === null) return;
|
|
342
|
+
if (ancestors.has(value)) return {
|
|
343
|
+
type: "circular-reference",
|
|
344
|
+
path
|
|
345
|
+
};
|
|
346
|
+
if (Array.isArray(value)) {
|
|
347
|
+
ancestors.add(value);
|
|
348
|
+
for (let index = 0; index < value.length; index += 1) if (!Object.prototype.hasOwnProperty.call(value, index)) return {
|
|
349
|
+
type: "array-hole",
|
|
350
|
+
path: [...path, index]
|
|
351
|
+
};
|
|
352
|
+
for (const key of getOwnEnumerableKeys(value)) {
|
|
353
|
+
const nextPath = [...path, key];
|
|
354
|
+
if (typeof key === "symbol") return {
|
|
355
|
+
type: "symbol-key",
|
|
356
|
+
path: nextPath
|
|
357
|
+
};
|
|
358
|
+
if (!isArrayIndexKey(key, value.length)) return {
|
|
359
|
+
type: "array-property",
|
|
360
|
+
path: nextPath
|
|
361
|
+
};
|
|
362
|
+
const violation = findYjsStateViolation(value[Number(key)], nextPath, ancestors);
|
|
363
|
+
if (violation) return violation;
|
|
364
|
+
}
|
|
365
|
+
ancestors.delete(value);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
if (!isPlainObject(value)) return {
|
|
369
|
+
type: "non-plain-object",
|
|
370
|
+
path
|
|
371
|
+
};
|
|
372
|
+
ancestors.add(value);
|
|
373
|
+
for (const key of getOwnEnumerableKeys(value)) {
|
|
374
|
+
const nextPath = [...path, key];
|
|
375
|
+
if (typeof key === "symbol") return {
|
|
376
|
+
type: "symbol-key",
|
|
377
|
+
path: nextPath
|
|
378
|
+
};
|
|
379
|
+
const child = value[key];
|
|
380
|
+
const violation = findYjsStateViolation(child, nextPath, ancestors);
|
|
381
|
+
if (violation) return violation;
|
|
382
|
+
}
|
|
383
|
+
ancestors.delete(value);
|
|
384
|
+
};
|
|
385
|
+
const assertYjsSerializableState = (state, path = []) => {
|
|
386
|
+
const violation = findYjsStateViolation(state, path);
|
|
387
|
+
if (!violation) return;
|
|
388
|
+
if (violation.type === "symbol-key") throw new YjsSerializableStateError(`Yjs binding does not support symbol-keyed state because Y.Map keys are strings. Found symbol key at ${formatPropertyPath(violation.path)}.`);
|
|
389
|
+
if (violation.type === "symbol-value") throw new YjsSerializableStateError(`Yjs binding does not support symbol-valued state because symbols cannot be cloned into Yjs documents. Found symbol value at ${formatPropertyPath(violation.path)}.`);
|
|
390
|
+
throw new YjsSerializableStateError(`Yjs binding does not support ${violation.type} state because only plain objects, arrays, and primitive values round-trip through Yjs updates. Found unsupported value at ${formatPropertyPath(violation.path)}.`);
|
|
391
|
+
};
|
|
392
|
+
const assertRemoteOperationsSerializable = (operations) => {
|
|
393
|
+
for (const operation of operations) if (operation.type === "set") assertYjsSerializableState(operation.value, operation.path);
|
|
394
|
+
};
|
|
395
|
+
const createRootReplacementPatches = (currentState, nextState) => {
|
|
396
|
+
const patches = [];
|
|
397
|
+
const inversePatches = [];
|
|
398
|
+
const nextKeys = new Set(getOwnEnumerableKeys(nextState));
|
|
399
|
+
for (const key of getOwnEnumerableKeys(currentState)) {
|
|
400
|
+
if (typeof key === "symbol" || nextKeys.has(key)) continue;
|
|
401
|
+
patches.push({
|
|
402
|
+
op: "remove",
|
|
403
|
+
path: [key]
|
|
404
|
+
});
|
|
405
|
+
inversePatches.push({
|
|
406
|
+
op: "add",
|
|
407
|
+
path: [key],
|
|
408
|
+
value: currentState[key]
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
for (const key of nextKeys) {
|
|
412
|
+
if (typeof key === "symbol") continue;
|
|
413
|
+
if (!Object.prototype.hasOwnProperty.call(currentState, key)) {
|
|
414
|
+
patches.push({
|
|
415
|
+
op: "add",
|
|
416
|
+
path: [key],
|
|
417
|
+
value: nextState[key]
|
|
418
|
+
});
|
|
419
|
+
inversePatches.push({
|
|
420
|
+
op: "remove",
|
|
421
|
+
path: [key]
|
|
422
|
+
});
|
|
423
|
+
continue;
|
|
424
|
+
}
|
|
425
|
+
if (Object.is(currentState[key], nextState[key])) continue;
|
|
426
|
+
patches.push({
|
|
427
|
+
op: "replace",
|
|
428
|
+
path: [key],
|
|
429
|
+
value: nextState[key]
|
|
430
|
+
});
|
|
431
|
+
inversePatches.push({
|
|
432
|
+
op: "replace",
|
|
433
|
+
path: [key],
|
|
434
|
+
value: currentState[key]
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
return {
|
|
438
|
+
patches,
|
|
439
|
+
inversePatches
|
|
440
|
+
};
|
|
344
441
|
};
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
let destroyed = false;
|
|
354
|
-
let syncingFromYjs = false;
|
|
355
|
-
let lastSyncedState = (() => {
|
|
356
|
-
const pureState = clone(store.getPureState());
|
|
357
|
-
return isPlainObject(pureState) ? pureState : {};
|
|
358
|
-
})();
|
|
359
|
-
let flushScheduled = false;
|
|
360
|
-
let pendingSnapshot = null;
|
|
361
|
-
let pendingOperations = [];
|
|
362
|
-
const applyRemoteState = (state) => {
|
|
363
|
-
const next = clone(state);
|
|
364
|
-
syncingFromYjs = true;
|
|
365
|
-
try {
|
|
366
|
-
store.setState(next);
|
|
367
|
-
const pureState = clone(store.getPureState());
|
|
368
|
-
lastSyncedState = isPlainObject(pureState) ? pureState : {};
|
|
369
|
-
} finally {
|
|
370
|
-
syncingFromYjs = false;
|
|
371
|
-
}
|
|
372
|
-
};
|
|
373
|
-
const applyRemoteOperations = (operations) => {
|
|
374
|
-
if (operations.length === 0) {
|
|
375
|
-
return;
|
|
376
|
-
}
|
|
377
|
-
syncingFromYjs = true;
|
|
378
|
-
try {
|
|
379
|
-
store.setState((draft) => {
|
|
380
|
-
const mutableDraft = draft;
|
|
381
|
-
for (const operation of operations) {
|
|
382
|
-
if (operation.type === "set") {
|
|
383
|
-
setAtPath(mutableDraft, operation.path, operation.value);
|
|
384
|
-
} else {
|
|
385
|
-
deleteAtPath(mutableDraft, operation.path);
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
});
|
|
389
|
-
const pureState = clone(store.getPureState());
|
|
390
|
-
lastSyncedState = isPlainObject(pureState) ? pureState : {};
|
|
391
|
-
} finally {
|
|
392
|
-
syncingFromYjs = false;
|
|
393
|
-
}
|
|
394
|
-
};
|
|
395
|
-
const getStateMap = () => {
|
|
396
|
-
const state = map.get(STATE_KEY);
|
|
397
|
-
if (state instanceof Y.Map) {
|
|
398
|
-
return state;
|
|
399
|
-
}
|
|
400
|
-
return null;
|
|
401
|
-
};
|
|
402
|
-
const scheduleFlushFromYjs = () => {
|
|
403
|
-
if (destroyed || flushScheduled) {
|
|
404
|
-
return;
|
|
405
|
-
}
|
|
406
|
-
flushScheduled = true;
|
|
407
|
-
scheduleMicrotask(flushFromYjs);
|
|
408
|
-
};
|
|
409
|
-
const flushFromYjs = () => {
|
|
410
|
-
flushScheduled = false;
|
|
411
|
-
if (destroyed) {
|
|
412
|
-
return;
|
|
413
|
-
}
|
|
414
|
-
if (pendingSnapshot) {
|
|
415
|
-
const snapshot = pendingSnapshot;
|
|
416
|
-
pendingSnapshot = null;
|
|
417
|
-
pendingOperations = [];
|
|
418
|
-
try {
|
|
419
|
-
applyRemoteState(snapshot);
|
|
420
|
-
} catch (error) {
|
|
421
|
-
if (isSetStateReentryError(error)) {
|
|
422
|
-
pendingSnapshot = snapshot;
|
|
423
|
-
setTimeout(scheduleFlushFromYjs, 0);
|
|
424
|
-
return;
|
|
425
|
-
}
|
|
426
|
-
throw error;
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
if (pendingOperations.length === 0) {
|
|
430
|
-
return;
|
|
431
|
-
}
|
|
432
|
-
const operations = compactOperations(pendingOperations);
|
|
433
|
-
pendingOperations = [];
|
|
434
|
-
try {
|
|
435
|
-
applyRemoteOperations(operations);
|
|
436
|
-
} catch (error) {
|
|
437
|
-
if (isSetStateReentryError(error)) {
|
|
438
|
-
pendingOperations = [...operations, ...pendingOperations];
|
|
439
|
-
setTimeout(scheduleFlushFromYjs, 0);
|
|
440
|
-
return;
|
|
441
|
-
}
|
|
442
|
-
throw error;
|
|
443
|
-
}
|
|
444
|
-
};
|
|
445
|
-
const enqueueSnapshot = (snapshot) => {
|
|
446
|
-
pendingSnapshot = snapshot;
|
|
447
|
-
pendingOperations = [];
|
|
448
|
-
scheduleFlushFromYjs();
|
|
449
|
-
};
|
|
450
|
-
const enqueueOperations = (operations) => {
|
|
451
|
-
if (operations.length === 0) {
|
|
452
|
-
return;
|
|
453
|
-
}
|
|
454
|
-
if (!pendingSnapshot) {
|
|
455
|
-
pendingOperations.push(...operations);
|
|
456
|
-
}
|
|
457
|
-
scheduleFlushFromYjs();
|
|
458
|
-
};
|
|
459
|
-
const syncNow = () => {
|
|
460
|
-
if (destroyed || syncingFromYjs) {
|
|
461
|
-
return;
|
|
462
|
-
}
|
|
463
|
-
const pureState = clone(store.getPureState());
|
|
464
|
-
if (!isPlainObject(pureState)) {
|
|
465
|
-
return;
|
|
466
|
-
}
|
|
467
|
-
doc.transact(() => {
|
|
468
|
-
syncObjectToYMap(stateMap, lastSyncedState, pureState);
|
|
469
|
-
}, localOrigin);
|
|
470
|
-
lastSyncedState = pureState;
|
|
471
|
-
};
|
|
472
|
-
const stateObserver = (events, transaction) => {
|
|
473
|
-
if (transaction.origin === localOrigin) {
|
|
474
|
-
return;
|
|
475
|
-
}
|
|
476
|
-
enqueueOperations(collectRemoteOperations(events, stateMap));
|
|
477
|
-
};
|
|
478
|
-
let stateMap;
|
|
479
|
-
const existingStateMap = getStateMap();
|
|
480
|
-
if (existingStateMap) {
|
|
481
|
-
stateMap = existingStateMap;
|
|
482
|
-
applyRemoteState(toPlainObject(stateMap));
|
|
483
|
-
} else {
|
|
484
|
-
const currentState = map.get(STATE_KEY);
|
|
485
|
-
if (isPlainObject(currentState)) {
|
|
486
|
-
stateMap = createYMap(currentState);
|
|
487
|
-
doc.transact(() => {
|
|
488
|
-
map.set(STATE_KEY, stateMap);
|
|
489
|
-
}, localOrigin);
|
|
490
|
-
applyRemoteState(currentState);
|
|
491
|
-
} else {
|
|
492
|
-
const pureState = clone(store.getPureState());
|
|
493
|
-
stateMap = createYMap(isPlainObject(pureState) ? pureState : {});
|
|
494
|
-
doc.transact(() => {
|
|
495
|
-
map.set(STATE_KEY, stateMap);
|
|
496
|
-
}, localOrigin);
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
stateMap.observeDeep(stateObserver);
|
|
500
|
-
const observer = (event) => {
|
|
501
|
-
if (event.transaction.origin === localOrigin) {
|
|
502
|
-
return;
|
|
503
|
-
}
|
|
504
|
-
if (!event.keysChanged.has(STATE_KEY)) {
|
|
505
|
-
return;
|
|
506
|
-
}
|
|
507
|
-
const nextStateMap = getStateMap();
|
|
508
|
-
if (nextStateMap) {
|
|
509
|
-
if (stateMap !== nextStateMap) {
|
|
510
|
-
stateMap.unobserveDeep(stateObserver);
|
|
511
|
-
stateMap = nextStateMap;
|
|
512
|
-
stateMap.observeDeep(stateObserver);
|
|
513
|
-
}
|
|
514
|
-
enqueueSnapshot(toPlainObject(nextStateMap));
|
|
515
|
-
return;
|
|
516
|
-
}
|
|
517
|
-
const currentState = map.get(STATE_KEY);
|
|
518
|
-
if (isPlainObject(currentState)) {
|
|
519
|
-
const migrated = createYMap(currentState);
|
|
520
|
-
doc.transact(() => {
|
|
521
|
-
map.set(STATE_KEY, migrated);
|
|
522
|
-
}, localOrigin);
|
|
523
|
-
if (stateMap !== migrated) {
|
|
524
|
-
stateMap.unobserveDeep(stateObserver);
|
|
525
|
-
stateMap = migrated;
|
|
526
|
-
stateMap.observeDeep(stateObserver);
|
|
527
|
-
}
|
|
528
|
-
enqueueSnapshot(currentState);
|
|
529
|
-
}
|
|
530
|
-
};
|
|
531
|
-
map.observe(observer);
|
|
532
|
-
const unsubscribe = store.subscribe(() => {
|
|
533
|
-
syncNow();
|
|
534
|
-
});
|
|
535
|
-
const binding = {
|
|
536
|
-
doc,
|
|
537
|
-
map,
|
|
538
|
-
syncNow,
|
|
539
|
-
destroy: () => {
|
|
540
|
-
if (destroyed) {
|
|
541
|
-
return;
|
|
542
|
-
}
|
|
543
|
-
destroyed = true;
|
|
544
|
-
unsubscribe();
|
|
545
|
-
map.unobserve(observer);
|
|
546
|
-
stateMap.unobserveDeep(stateObserver);
|
|
547
|
-
if (!options.doc) {
|
|
548
|
-
doc.destroy();
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
};
|
|
552
|
-
if (process.env.NODE_ENV === "test") {
|
|
553
|
-
binding.__unsafeTestOnly__ = {
|
|
554
|
-
applyRemoteOperations: (operations) => {
|
|
555
|
-
applyRemoteOperations(operations);
|
|
556
|
-
}
|
|
557
|
-
};
|
|
558
|
-
}
|
|
559
|
-
return binding;
|
|
442
|
+
const __unsafeTestOnly__ = {
|
|
443
|
+
getYValueAtPath: (root, path) => getYValueAtPath(root, path),
|
|
444
|
+
setAtPath: (target, path, value) => {
|
|
445
|
+
setAtPath(target, path, value);
|
|
446
|
+
},
|
|
447
|
+
deleteAtPath: (target, path) => {
|
|
448
|
+
deleteAtPath(target, path);
|
|
449
|
+
}
|
|
560
450
|
};
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
451
|
+
const bindYjs = (store, options = {}) => {
|
|
452
|
+
if (store.share === "client") throw new Error("Yjs binding is not supported in client store mode.");
|
|
453
|
+
const doc = options.doc ?? new Y.Doc();
|
|
454
|
+
const key = options.key ?? `coaction:${store.name}`;
|
|
455
|
+
const map = doc.getMap(key);
|
|
456
|
+
const localOrigin = Symbol(`coaction-yjs:${store.name}`);
|
|
457
|
+
let destroyed = false;
|
|
458
|
+
let syncingFromYjs = false;
|
|
459
|
+
assertYjsSerializableState(store.getPureState());
|
|
460
|
+
let lastSyncedState = (() => {
|
|
461
|
+
const pureState = clone(store.getPureState());
|
|
462
|
+
return isPlainObject(pureState) ? pureState : {};
|
|
463
|
+
})();
|
|
464
|
+
let flushScheduled = false;
|
|
465
|
+
let pendingSnapshot = null;
|
|
466
|
+
let pendingOperations = [];
|
|
467
|
+
const restoreLastSyncedState = () => {
|
|
468
|
+
syncingFromYjs = true;
|
|
469
|
+
try {
|
|
470
|
+
store.apply(lastSyncedState);
|
|
471
|
+
} finally {
|
|
472
|
+
syncingFromYjs = false;
|
|
473
|
+
}
|
|
474
|
+
};
|
|
475
|
+
const applyRemoteState = (state) => {
|
|
476
|
+
assertYjsSerializableState(state);
|
|
477
|
+
const next = sanitizePlainValue(state);
|
|
478
|
+
syncingFromYjs = true;
|
|
479
|
+
try {
|
|
480
|
+
if (store.share === "main") store.setState(next, () => {
|
|
481
|
+
const { patches, inversePatches } = createRootReplacementPatches(store.getPureState(), next);
|
|
482
|
+
const finalPatches = store.patch ? store.patch({
|
|
483
|
+
patches,
|
|
484
|
+
inversePatches
|
|
485
|
+
}) : {
|
|
486
|
+
patches,
|
|
487
|
+
inversePatches
|
|
488
|
+
};
|
|
489
|
+
if (finalPatches.patches.length) store.apply(store.getPureState(), finalPatches.patches);
|
|
490
|
+
return [
|
|
491
|
+
store.getPureState(),
|
|
492
|
+
finalPatches.patches,
|
|
493
|
+
finalPatches.inversePatches
|
|
494
|
+
];
|
|
495
|
+
});
|
|
496
|
+
else {
|
|
497
|
+
store.setState(null);
|
|
498
|
+
store.apply(next);
|
|
499
|
+
}
|
|
500
|
+
const pureState = clone(store.getPureState());
|
|
501
|
+
lastSyncedState = isPlainObject(pureState) ? pureState : {};
|
|
502
|
+
} finally {
|
|
503
|
+
syncingFromYjs = false;
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
const applyRemoteOperations = (operations) => {
|
|
507
|
+
if (operations.length === 0) return;
|
|
508
|
+
assertRemoteOperationsSerializable(operations);
|
|
509
|
+
syncingFromYjs = true;
|
|
510
|
+
try {
|
|
511
|
+
store.setState((draft) => {
|
|
512
|
+
const mutableDraft = draft;
|
|
513
|
+
for (const operation of operations) if (operation.type === "set") setAtPath(mutableDraft, operation.path, operation.value);
|
|
514
|
+
else deleteAtPath(mutableDraft, operation.path);
|
|
515
|
+
});
|
|
516
|
+
const pureState = clone(store.getPureState());
|
|
517
|
+
lastSyncedState = isPlainObject(pureState) ? pureState : {};
|
|
518
|
+
} finally {
|
|
519
|
+
syncingFromYjs = false;
|
|
520
|
+
}
|
|
521
|
+
};
|
|
522
|
+
const getStateMap = () => {
|
|
523
|
+
const state = map.get(STATE_KEY);
|
|
524
|
+
if (state instanceof Y.Map) return state;
|
|
525
|
+
return null;
|
|
526
|
+
};
|
|
527
|
+
const scheduleFlushFromYjs = () => {
|
|
528
|
+
if (destroyed || flushScheduled) return;
|
|
529
|
+
flushScheduled = true;
|
|
530
|
+
scheduleMicrotask(flushFromYjs);
|
|
531
|
+
};
|
|
532
|
+
const flushFromYjs = () => {
|
|
533
|
+
flushScheduled = false;
|
|
534
|
+
if (destroyed) return;
|
|
535
|
+
if (pendingSnapshot) {
|
|
536
|
+
const snapshot = pendingSnapshot;
|
|
537
|
+
pendingSnapshot = null;
|
|
538
|
+
pendingOperations = [];
|
|
539
|
+
try {
|
|
540
|
+
applyRemoteState(snapshot);
|
|
541
|
+
} catch (error) {
|
|
542
|
+
if (isSetStateReentryError(error)) {
|
|
543
|
+
pendingSnapshot = snapshot;
|
|
544
|
+
setTimeout(scheduleFlushFromYjs, 0);
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
if (isYjsSerializableStateError(error)) {
|
|
548
|
+
restoreRootState();
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
throw error;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
if (pendingOperations.length === 0) return;
|
|
555
|
+
const operations = compactOperations(pendingOperations);
|
|
556
|
+
pendingOperations = [];
|
|
557
|
+
try {
|
|
558
|
+
applyRemoteOperations(operations);
|
|
559
|
+
} catch (error) {
|
|
560
|
+
if (isSetStateReentryError(error)) {
|
|
561
|
+
pendingOperations = [...operations, ...pendingOperations];
|
|
562
|
+
setTimeout(scheduleFlushFromYjs, 0);
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
if (isYjsSerializableStateError(error)) {
|
|
566
|
+
restoreRootState();
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
throw error;
|
|
570
|
+
}
|
|
571
|
+
};
|
|
572
|
+
const enqueueSnapshot = (snapshot) => {
|
|
573
|
+
pendingSnapshot = snapshot;
|
|
574
|
+
pendingOperations = [];
|
|
575
|
+
scheduleFlushFromYjs();
|
|
576
|
+
};
|
|
577
|
+
const enqueueOperations = (operations) => {
|
|
578
|
+
if (operations.length === 0) return;
|
|
579
|
+
if (!pendingSnapshot) pendingOperations.push(...operations);
|
|
580
|
+
scheduleFlushFromYjs();
|
|
581
|
+
};
|
|
582
|
+
const syncNow = () => {
|
|
583
|
+
if (destroyed || syncingFromYjs) return;
|
|
584
|
+
try {
|
|
585
|
+
assertYjsSerializableState(store.getPureState());
|
|
586
|
+
} catch (error) {
|
|
587
|
+
restoreLastSyncedState();
|
|
588
|
+
throw error;
|
|
589
|
+
}
|
|
590
|
+
const pureState = clone(store.getPureState());
|
|
591
|
+
if (!isPlainObject(pureState)) return;
|
|
592
|
+
doc.transact(() => {
|
|
593
|
+
syncObjectToYMap(stateMap, lastSyncedState, pureState);
|
|
594
|
+
}, localOrigin);
|
|
595
|
+
lastSyncedState = pureState;
|
|
596
|
+
};
|
|
597
|
+
const stateObserver = (events, transaction) => {
|
|
598
|
+
if (transaction.origin === localOrigin) return;
|
|
599
|
+
enqueueOperations(collectRemoteOperations(events, stateMap));
|
|
600
|
+
};
|
|
601
|
+
let stateMap;
|
|
602
|
+
let stateMapObserved = false;
|
|
603
|
+
const unobserveStateMap = () => {
|
|
604
|
+
if (!stateMapObserved) return;
|
|
605
|
+
stateMap.unobserveDeep(stateObserver);
|
|
606
|
+
stateMapObserved = false;
|
|
607
|
+
};
|
|
608
|
+
const observeStateMap = (nextStateMap) => {
|
|
609
|
+
if (stateMap === nextStateMap && stateMapObserved) return;
|
|
610
|
+
unobserveStateMap();
|
|
611
|
+
stateMap = nextStateMap;
|
|
612
|
+
stateMap.observeDeep(stateObserver);
|
|
613
|
+
stateMapObserved = true;
|
|
614
|
+
};
|
|
615
|
+
const migrateRootState = (nextState) => {
|
|
616
|
+
const migrated = createYMap(nextState);
|
|
617
|
+
doc.transact(() => {
|
|
618
|
+
map.set(STATE_KEY, migrated);
|
|
619
|
+
}, localOrigin);
|
|
620
|
+
observeStateMap(migrated);
|
|
621
|
+
enqueueSnapshot(nextState);
|
|
622
|
+
};
|
|
623
|
+
const restoreRootState = () => {
|
|
624
|
+
const pureState = clone(store.getPureState());
|
|
625
|
+
const nextState = isPlainObject(pureState) ? pureState : lastSyncedState;
|
|
626
|
+
const restored = createYMap(nextState);
|
|
627
|
+
doc.transact(() => {
|
|
628
|
+
map.set(STATE_KEY, restored);
|
|
629
|
+
}, localOrigin);
|
|
630
|
+
observeStateMap(restored);
|
|
631
|
+
lastSyncedState = nextState;
|
|
632
|
+
};
|
|
633
|
+
const applyInitialRemoteState = (state) => {
|
|
634
|
+
runWithoutHistoryRecording(store, () => {
|
|
635
|
+
try {
|
|
636
|
+
applyRemoteState(state);
|
|
637
|
+
} catch (error) {
|
|
638
|
+
if (isYjsSerializableStateError(error)) {
|
|
639
|
+
restoreRootState();
|
|
640
|
+
return;
|
|
641
|
+
}
|
|
642
|
+
throw error;
|
|
643
|
+
}
|
|
644
|
+
});
|
|
645
|
+
};
|
|
646
|
+
const existingStateMap = getStateMap();
|
|
647
|
+
if (existingStateMap) {
|
|
648
|
+
stateMap = existingStateMap;
|
|
649
|
+
applyInitialRemoteState(toPlainObject(stateMap));
|
|
650
|
+
} else {
|
|
651
|
+
const currentState = map.get(STATE_KEY);
|
|
652
|
+
if (isPlainObject(currentState)) {
|
|
653
|
+
stateMap = createYMap(currentState);
|
|
654
|
+
doc.transact(() => {
|
|
655
|
+
map.set(STATE_KEY, stateMap);
|
|
656
|
+
}, localOrigin);
|
|
657
|
+
applyInitialRemoteState(currentState);
|
|
658
|
+
} else {
|
|
659
|
+
const pureState = clone(store.getPureState());
|
|
660
|
+
stateMap = createYMap(isPlainObject(pureState) ? pureState : {});
|
|
661
|
+
doc.transact(() => {
|
|
662
|
+
map.set(STATE_KEY, stateMap);
|
|
663
|
+
}, localOrigin);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
observeStateMap(stateMap);
|
|
667
|
+
const observer = (event) => {
|
|
668
|
+
if (event.transaction.origin === localOrigin) return;
|
|
669
|
+
if (!event.keysChanged.has(STATE_KEY)) return;
|
|
670
|
+
const stateChange = event.changes.keys.get(STATE_KEY);
|
|
671
|
+
const nextStateMap = getStateMap();
|
|
672
|
+
if (nextStateMap) {
|
|
673
|
+
observeStateMap(nextStateMap);
|
|
674
|
+
enqueueSnapshot(toPlainObject(nextStateMap));
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
const currentState = map.get(STATE_KEY);
|
|
678
|
+
if (isPlainObject(currentState)) {
|
|
679
|
+
migrateRootState(currentState);
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
if (stateChange?.action === "delete") {
|
|
683
|
+
migrateRootState({});
|
|
684
|
+
return;
|
|
685
|
+
}
|
|
686
|
+
restoreRootState();
|
|
687
|
+
};
|
|
688
|
+
map.observe(observer);
|
|
689
|
+
const unsubscribe = store.subscribe(() => {
|
|
690
|
+
syncNow();
|
|
691
|
+
});
|
|
692
|
+
const binding = {
|
|
693
|
+
doc,
|
|
694
|
+
map,
|
|
695
|
+
syncNow,
|
|
696
|
+
destroy: () => {
|
|
697
|
+
if (destroyed) return;
|
|
698
|
+
destroyed = true;
|
|
699
|
+
unsubscribe();
|
|
700
|
+
map.unobserve(observer);
|
|
701
|
+
unobserveStateMap();
|
|
702
|
+
if (!options.doc) doc.destroy();
|
|
703
|
+
}
|
|
704
|
+
};
|
|
705
|
+
if (process.env.NODE_ENV === "test") binding.__unsafeTestOnly__ = { applyRemoteOperations: (operations) => {
|
|
706
|
+
applyRemoteOperations(operations);
|
|
707
|
+
} };
|
|
708
|
+
return binding;
|
|
569
709
|
};
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
710
|
+
const yjs = (options = {}) => (store) => {
|
|
711
|
+
let binding;
|
|
712
|
+
const cancelBinding = onStoreReady(store, () => {
|
|
713
|
+
binding = bindYjs(store, options);
|
|
714
|
+
});
|
|
715
|
+
const baseDestroy = store.destroy;
|
|
716
|
+
let destroyed = false;
|
|
717
|
+
store.destroy = () => {
|
|
718
|
+
if (destroyed) return;
|
|
719
|
+
destroyed = true;
|
|
720
|
+
cancelBinding();
|
|
721
|
+
binding?.destroy();
|
|
722
|
+
binding = void 0;
|
|
723
|
+
baseDestroy();
|
|
724
|
+
};
|
|
725
|
+
return store;
|
|
577
726
|
};
|
|
727
|
+
//#endregion
|
|
728
|
+
export { __unsafeTestOnly__, bindYjs, yjs };
|