@coaction/yjs 1.5.0 → 2.1.0

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