coaction 2.1.0 → 3.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.
@@ -0,0 +1,591 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region \0rolldown/runtime.js
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __copyProps = (to, from, except, desc) => {
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;
18
+ };
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 mutative = require("mutative");
25
+ let alien_signals = require("alien-signals");
26
+ let alien_signals_system = require("alien-signals/system");
27
+ alien_signals_system = __toESM(alien_signals_system);
28
+ //#region packages/core/src/global.ts
29
+ const getGlobal = () => {
30
+ let _global;
31
+ if (typeof window !== "undefined") _global = window;
32
+ else if (typeof global !== "undefined") _global = global;
33
+ else if (typeof self !== "undefined") _global = self;
34
+ else _global = {};
35
+ return _global;
36
+ };
37
+ getGlobal().SharedWorkerGlobalScope || globalThis.WorkerGlobalScope;
38
+ const bindSymbol = Symbol("bind");
39
+ //#endregion
40
+ //#region packages/core/src/binder.ts
41
+ const createExternalStoreAdapter = ({ handleState, handleStore }) => ((state) => {
42
+ const { copyState, key, bind } = handleState(state);
43
+ const value = typeof key !== "undefined" ? copyState[key] : copyState;
44
+ Object.defineProperty(value, bindSymbol, {
45
+ configurable: true,
46
+ enumerable: typeof key !== "undefined",
47
+ value: {
48
+ handleStore,
49
+ bind
50
+ }
51
+ });
52
+ return copyState;
53
+ });
54
+ /**
55
+ * Build an adapter helper for bridging an external store implementation into
56
+ * Coaction.
57
+ *
58
+ * @remarks
59
+ * Import this compatibility helper from `coaction/adapter`.
60
+ *
61
+ * Official bindings use this to integrate stores such as Redux, Jotai, Pinia,
62
+ * Zustand, MobX, and Valtio. Binder-backed integrations are whole-store
63
+ * adapters; they are not compatible with Coaction slices mode.
64
+ */
65
+ function createBinder({ handleState, handleStore }) {
66
+ return createExternalStoreAdapter({
67
+ handleState,
68
+ handleStore
69
+ });
70
+ }
71
+ /**
72
+ * Define a whole-store adapter for integrating an external state runtime with
73
+ * Coaction.
74
+ *
75
+ * @remarks
76
+ * Import this helper from `coaction/adapter`. `createBinder()` remains as a
77
+ * compatibility alias for existing official and community integrations.
78
+ */
79
+ function defineExternalStoreAdapter(options) {
80
+ return createExternalStoreAdapter(options);
81
+ }
82
+ //#endregion
83
+ //#region packages/core/src/utils.ts
84
+ const isUnsafeKey = (key) => key === "__proto__" || key === "prototype" || key === "constructor";
85
+ const isUnsafePathSegment = (segment) => typeof segment === "string" && isUnsafeKey(segment);
86
+ var UnsafePatchPathError = class extends Error {
87
+ name = "UnsafePatchPathError";
88
+ };
89
+ var StateSchemaError = class extends Error {
90
+ name = "StateSchemaError";
91
+ };
92
+ const isStateSchemaError = (error) => error instanceof StateSchemaError || error instanceof Error && error.name === "StateSchemaError";
93
+ const hasUnsafePatchPath = (path) => {
94
+ return (Array.isArray(path) ? path : typeof path === "string" ? path.split("/").filter(Boolean).map((segment) => segment.replace(/~1/g, "/").replace(/~0/g, "~")) : []).some(isUnsafePathSegment);
95
+ };
96
+ const formatPatchPath = (path) => Array.isArray(path) ? path.map((segment) => String(segment)).join(".") : String(path);
97
+ const getUnsafePatchPaths = (patches) => patches?.filter((patch) => hasUnsafePatchPath(patch.path)) ?? [];
98
+ const assertSafePatches = (patches, source = "patches") => {
99
+ const unsafePatches = getUnsafePatchPaths(patches);
100
+ if (!unsafePatches.length) return;
101
+ const paths = unsafePatches.map((patch) => `'${formatPatchPath(patch.path)}'`).join(", ");
102
+ throw new UnsafePatchPathError(`Unsafe patch path${unsafePatches.length > 1 ? "s" : ""} ${paths} cannot be applied from ${source}.`);
103
+ };
104
+ const warnDroppedUnsafePatches = (unsafePatches, source) => {
105
+ if (process.env.NODE_ENV !== "development" || !unsafePatches.length) return;
106
+ const paths = unsafePatches.map((patch) => `'${formatPatchPath(patch.path)}'`).join(", ");
107
+ console.warn(`Coaction dropped unsafe patch path${unsafePatches.length > 1 ? "s" : ""} ${paths} from ${source}.`);
108
+ };
109
+ const sanitizePatches = (patches, options = {}) => {
110
+ if (options.warnOnDropped) warnDroppedUnsafePatches(getUnsafePatchPaths(patches), options.source ?? "patches");
111
+ return patches?.filter((patch) => !hasUnsafePatchPath(patch.path)).map((patch) => Object.prototype.hasOwnProperty.call(patch, "value") ? {
112
+ ...patch,
113
+ value: sanitizeReplacementState(patch.value)
114
+ } : patch);
115
+ };
116
+ const sanitizeCheckedPatches = (patches, source) => {
117
+ assertSafePatches(patches, source);
118
+ return sanitizePatches(patches) ?? [];
119
+ };
120
+ const createRootReplacementPatches = (currentState, nextState) => {
121
+ const patches = [];
122
+ const inversePatches = [];
123
+ const nextKeys = new Set(getOwnEnumerableKeys(nextState));
124
+ for (const key of getOwnEnumerableKeys(currentState)) {
125
+ if (typeof key === "string" && isUnsafeKey(key)) continue;
126
+ if (nextKeys.has(key)) continue;
127
+ patches.push({
128
+ op: "remove",
129
+ path: [key]
130
+ });
131
+ inversePatches.push({
132
+ op: "add",
133
+ path: [key],
134
+ value: currentState[key]
135
+ });
136
+ }
137
+ for (const key of nextKeys) {
138
+ if (typeof key === "string" && isUnsafeKey(key)) continue;
139
+ if (!Object.prototype.hasOwnProperty.call(currentState, key)) {
140
+ patches.push({
141
+ op: "add",
142
+ path: [key],
143
+ value: nextState[key]
144
+ });
145
+ inversePatches.push({
146
+ op: "remove",
147
+ path: [key]
148
+ });
149
+ continue;
150
+ }
151
+ if (Object.is(currentState[key], nextState[key])) continue;
152
+ patches.push({
153
+ op: "replace",
154
+ path: [key],
155
+ value: nextState[key]
156
+ });
157
+ inversePatches.push({
158
+ op: "replace",
159
+ path: [key],
160
+ value: currentState[key]
161
+ });
162
+ }
163
+ return {
164
+ patches,
165
+ inversePatches
166
+ };
167
+ };
168
+ const createRootStateFromPatches = (currentState, patches) => {
169
+ const nextState = sanitizeReplacementState(currentState);
170
+ const seen = /* @__PURE__ */ new WeakMap();
171
+ for (const patch of patches) {
172
+ if (!Array.isArray(patch.path) || patch.path.length !== 1 || ![
173
+ "add",
174
+ "remove",
175
+ "replace"
176
+ ].includes(patch.op)) return;
177
+ const key = patch.path[0];
178
+ if (patch.op === "remove") {
179
+ delete nextState[key];
180
+ continue;
181
+ }
182
+ nextState[key] = sanitizeReplacementState(patch.value, seen);
183
+ }
184
+ return nextState;
185
+ };
186
+ const applyRootReplacementWithPatches = (store, nextState, options = {}) => {
187
+ const { patches, inversePatches } = createRootReplacementPatches(store.getPureState(), nextState);
188
+ const finalPatches = store.patch ? store.patch({
189
+ patches,
190
+ inversePatches
191
+ }) : {
192
+ patches,
193
+ inversePatches
194
+ };
195
+ const safePatches = sanitizeCheckedPatches(finalPatches.patches, "store.patch()");
196
+ const safeInversePatches = sanitizeCheckedPatches(finalPatches.inversePatches, "store.patch() inverse patches");
197
+ if (safePatches.length) {
198
+ const applyExactReplacement = options.applyExactReplacement;
199
+ const exactReplacementState = applyExactReplacement ? createRootStateFromPatches(store.getPureState(), safePatches) : void 0;
200
+ if (applyExactReplacement && exactReplacementState) applyExactReplacement(exactReplacementState);
201
+ else store.apply(store.getPureState(), safePatches);
202
+ }
203
+ return [
204
+ store.getPureState(),
205
+ safePatches,
206
+ safeInversePatches
207
+ ];
208
+ };
209
+ const setOwnEnumerable = (target, key, value) => {
210
+ if (typeof key === "string" && isUnsafeKey(key)) return;
211
+ target[key] = value;
212
+ };
213
+ const getOwnEnumerableKeys = (source) => {
214
+ if (typeof source !== "object" || source === null) return [];
215
+ return Reflect.ownKeys(source).filter((key) => Object.prototype.propertyIsEnumerable.call(source, key));
216
+ };
217
+ const isArrayIndexKey$1 = (key) => {
218
+ if (typeof key !== "string") return false;
219
+ const index = Number(key);
220
+ return Number.isInteger(index) && index >= 0 && index < 2 ** 32 - 1 && String(index) === key;
221
+ };
222
+ const replaceOwnEnumerable = (target, source) => {
223
+ const seen = /* @__PURE__ */ new WeakMap();
224
+ seen.set(source, target);
225
+ const nextKeys = /* @__PURE__ */ new Set();
226
+ for (const key of getOwnEnumerableKeys(source)) {
227
+ if (typeof key === "string" && isUnsafeKey(key)) continue;
228
+ if (typeof source[key] === "function") continue;
229
+ nextKeys.add(key);
230
+ }
231
+ for (const key of getOwnEnumerableKeys(target)) if (!nextKeys.has(key)) delete target[key];
232
+ nextKeys.forEach((key) => {
233
+ setOwnEnumerable(target, key, sanitizeReplacementState(source[key], seen));
234
+ });
235
+ };
236
+ const sanitizeReplacementState = (source, seen = /* @__PURE__ */ new WeakMap()) => {
237
+ if (typeof source !== "object" || source === null) return source;
238
+ const cached = seen.get(source);
239
+ if (cached) return cached;
240
+ if (Array.isArray(source)) {
241
+ const target = [];
242
+ target.length = source.length;
243
+ seen.set(source, target);
244
+ for (let index = 0; index < source.length; index += 1) if (Object.prototype.hasOwnProperty.call(source, index)) target[index] = sanitizeReplacementState(source[index], seen);
245
+ for (const key of getOwnEnumerableKeys(source)) {
246
+ if (isArrayIndexKey$1(key) || typeof key === "string" && isUnsafeKey(key)) continue;
247
+ const value = source[key];
248
+ if (typeof value === "function") continue;
249
+ setOwnEnumerable(target, key, sanitizeReplacementState(value, seen));
250
+ }
251
+ return target;
252
+ }
253
+ const prototype = Object.getPrototypeOf(source);
254
+ if (prototype !== Object.prototype && prototype !== null) return source;
255
+ const target = Object.create(prototype);
256
+ seen.set(source, target);
257
+ for (const key of getOwnEnumerableKeys(source)) {
258
+ if (typeof key === "string" && isUnsafeKey(key)) continue;
259
+ const value = source[key];
260
+ if (typeof value === "function") continue;
261
+ setOwnEnumerable(target, key, sanitizeReplacementState(value, seen));
262
+ }
263
+ return target;
264
+ };
265
+ const sanitizeInitialStateValue = (source, seen = /* @__PURE__ */ new WeakMap()) => {
266
+ if (typeof source !== "object" || source === null) return source;
267
+ const cached = seen.get(source);
268
+ if (cached) return cached;
269
+ if (Array.isArray(source)) {
270
+ const target = [];
271
+ target.length = source.length;
272
+ seen.set(source, target);
273
+ for (let index = 0; index < source.length; index += 1) if (Object.prototype.hasOwnProperty.call(source, index)) target[index] = sanitizeInitialStateValue(source[index], seen);
274
+ for (const key of getOwnEnumerableKeys(source)) {
275
+ if (isArrayIndexKey$1(key) || typeof key === "string" && isUnsafeKey(key)) continue;
276
+ setOwnEnumerable(target, key, sanitizeInitialStateValue(source[key], seen));
277
+ }
278
+ return target;
279
+ }
280
+ const prototype = Object.getPrototypeOf(source);
281
+ if (prototype !== Object.prototype && prototype !== null) return source;
282
+ const target = Object.create(prototype);
283
+ seen.set(source, target);
284
+ for (const key of getOwnEnumerableKeys(source)) {
285
+ if (typeof key === "string" && isUnsafeKey(key)) continue;
286
+ setOwnEnumerable(target, key, sanitizeInitialStateValue(source[key], seen));
287
+ }
288
+ return target;
289
+ };
290
+ //#endregion
291
+ //#region packages/core/src/externalMutableAdapterUtils.ts
292
+ const getMutableAdapterOwnEnumerableKeys = (value) => Reflect.ownKeys(value).filter((key) => Object.prototype.propertyIsEnumerable.call(value, key));
293
+ const isMutableAdapterUnsafeKey = (key) => typeof key === "string" && isUnsafeKey(key);
294
+ const isArrayIndexKey = (key) => {
295
+ if (typeof key !== "string") return false;
296
+ const index = Number(key);
297
+ return Number.isInteger(index) && index >= 0 && index < 2 ** 32 - 1 && String(index) === key;
298
+ };
299
+ const isObjectRecord = (value) => Object.prototype.toString.call(value) === "[object Object]";
300
+ const assertCanSetMutableAdapterPublicStateKey = (publicState, key) => {
301
+ if (Object.prototype.hasOwnProperty.call(publicState, key)) return;
302
+ if (Object.isExtensible(publicState)) return;
303
+ throw new StateSchemaError(`Unknown state key '${String(key)}' cannot be added after store initialization. Coaction state schema is fixed.`);
304
+ };
305
+ const ensureMutableAdapterRawDescriptor = (rawState, mutableState, publicState, key) => {
306
+ if (rawState === mutableState) return;
307
+ const rawDescriptor = Object.getOwnPropertyDescriptor(rawState, key);
308
+ if (rawDescriptor?.get && rawDescriptor.set) return;
309
+ const publicDescriptor = Object.getOwnPropertyDescriptor(publicState, key);
310
+ if (!publicDescriptor || rawDescriptor?.configurable === false) return;
311
+ Object.defineProperty(rawState, key, {
312
+ get: () => mutableState[key],
313
+ set: (value) => {
314
+ mutableState[key] = value;
315
+ },
316
+ configurable: true,
317
+ enumerable: publicDescriptor.enumerable
318
+ });
319
+ };
320
+ const replaceMutableAdapterState = (rawState, mutableState, publicState, source) => {
321
+ const nextKeys = /* @__PURE__ */ new Set();
322
+ for (const key of getMutableAdapterOwnEnumerableKeys(source)) {
323
+ if (isMutableAdapterUnsafeKey(key)) continue;
324
+ if (typeof source[key] === "function") continue;
325
+ nextKeys.add(key);
326
+ }
327
+ nextKeys.forEach((key) => {
328
+ assertCanSetMutableAdapterPublicStateKey(publicState, key);
329
+ });
330
+ for (const key of getMutableAdapterOwnEnumerableKeys(rawState)) {
331
+ if (isMutableAdapterUnsafeKey(key)) {
332
+ delete rawState[key];
333
+ delete mutableState[key];
334
+ continue;
335
+ }
336
+ if (typeof rawState[key] === "function") continue;
337
+ if (!nextKeys.has(key)) {
338
+ delete rawState[key];
339
+ delete mutableState[key];
340
+ }
341
+ }
342
+ const rawSeen = /* @__PURE__ */ new WeakMap();
343
+ const mutableSeen = /* @__PURE__ */ new WeakMap();
344
+ const publicSeen = /* @__PURE__ */ new WeakMap();
345
+ rawSeen.set(source, rawState);
346
+ mutableSeen.set(source, mutableState);
347
+ publicSeen.set(source, publicState);
348
+ nextKeys.forEach((key) => {
349
+ ensureMutableAdapterRawDescriptor(rawState, mutableState, publicState, key);
350
+ rawState[key] = sanitizeReplacementState(source[key], rawSeen);
351
+ mutableState[key] = sanitizeReplacementState(source[key], mutableSeen);
352
+ publicState[key] = sanitizeReplacementState(source[key], publicSeen);
353
+ });
354
+ };
355
+ const applyMutableAdapterPatches = (baseState, patches, rawState, mutableState, publicState, validateState) => {
356
+ assertSafePatches(patches, "mutable adapter apply()");
357
+ const nextState = (0, mutative.apply)(toMutableAdapterSnapshot(baseState === publicState ? rawState : baseState), patches);
358
+ validateState?.(nextState);
359
+ replaceMutableAdapterState(rawState, mutableState, publicState, nextState);
360
+ };
361
+ const toMutableAdapterSnapshot = (value, visited = /* @__PURE__ */ new WeakMap()) => {
362
+ if (Array.isArray(value)) {
363
+ if (visited.has(value)) return visited.get(value);
364
+ const next = [];
365
+ next.length = value.length;
366
+ visited.set(value, next);
367
+ for (let index = 0; index < value.length; index += 1) if (Object.prototype.hasOwnProperty.call(value, index)) next[index] = toMutableAdapterSnapshot(value[index], visited);
368
+ const source = value;
369
+ const target = next;
370
+ for (const key of getMutableAdapterOwnEnumerableKeys(value)) {
371
+ if (isArrayIndexKey(key) || isMutableAdapterUnsafeKey(key)) continue;
372
+ const child = source[key];
373
+ if (typeof child !== "function") target[key] = toMutableAdapterSnapshot(child, visited);
374
+ }
375
+ return next;
376
+ }
377
+ if (typeof value === "object" && value !== null) {
378
+ if (!isObjectRecord(value)) return value;
379
+ if (visited.has(value)) return visited.get(value);
380
+ const next = {};
381
+ visited.set(value, next);
382
+ for (const key of getMutableAdapterOwnEnumerableKeys(value)) {
383
+ if (isMutableAdapterUnsafeKey(key)) continue;
384
+ const child = value[key];
385
+ if (typeof child !== "function") next[key] = toMutableAdapterSnapshot(child, visited);
386
+ }
387
+ return next;
388
+ }
389
+ return value;
390
+ };
391
+ const snapshotMutableAdapterPureState = (store) => toMutableAdapterSnapshot(store.getPureState());
392
+ const isEqualMutableAdapterSnapshot = (left, right, visited = /* @__PURE__ */ new WeakMap()) => {
393
+ if (Object.is(left, right)) return true;
394
+ if (typeof left !== "object" || left === null || typeof right !== "object" || right === null) return false;
395
+ const leftIsArray = Array.isArray(left);
396
+ const rightIsArray = Array.isArray(right);
397
+ if (leftIsArray || rightIsArray) {
398
+ if (!leftIsArray || !rightIsArray || left.length !== right.length) return false;
399
+ } else if (!isObjectRecord(left) || !isObjectRecord(right)) return false;
400
+ let seenTargets = visited.get(left);
401
+ if (!seenTargets) {
402
+ seenTargets = /* @__PURE__ */ new WeakSet();
403
+ visited.set(left, seenTargets);
404
+ } else if (seenTargets.has(right)) return true;
405
+ seenTargets.add(right);
406
+ const leftRecord = left;
407
+ const rightRecord = right;
408
+ const leftKeys = getMutableAdapterOwnEnumerableKeys(left);
409
+ const rightKeys = getMutableAdapterOwnEnumerableKeys(right);
410
+ if (leftKeys.length !== rightKeys.length) return false;
411
+ for (const key of leftKeys) {
412
+ if (!Object.prototype.hasOwnProperty.call(rightRecord, key)) return false;
413
+ if (!isEqualMutableAdapterSnapshot(leftRecord[key], rightRecord[key], visited)) return false;
414
+ }
415
+ return true;
416
+ };
417
+ //#endregion
418
+ //#region packages/core/src/reactiveTracker.ts
419
+ const ReactiveFlags = alien_signals_system.ReactiveFlags;
420
+ const unwatch = (node) => {
421
+ if (!(node.flags & ReactiveFlags.Mutable)) {
422
+ node.depsTail = void 0;
423
+ node.flags = 0;
424
+ purgeDeps(node);
425
+ const sub = node.subs;
426
+ if (sub !== void 0) unlink(sub);
427
+ return;
428
+ }
429
+ if (node.depsTail !== void 0) {
430
+ node.depsTail = void 0;
431
+ node.flags = ReactiveFlags.Mutable | ReactiveFlags.Dirty;
432
+ purgeDeps(node);
433
+ }
434
+ };
435
+ const unlink = (link, sub = link.sub) => {
436
+ const dep = link.dep;
437
+ const prevDep = link.prevDep;
438
+ const nextDep = link.nextDep;
439
+ const nextSub = link.nextSub;
440
+ const prevSub = link.prevSub;
441
+ if (nextDep !== void 0) nextDep.prevDep = prevDep;
442
+ else sub.depsTail = prevDep;
443
+ if (prevDep !== void 0) prevDep.nextDep = nextDep;
444
+ else sub.deps = nextDep;
445
+ if (nextSub !== void 0) nextSub.prevSub = prevSub;
446
+ else dep.subsTail = prevSub;
447
+ if (prevSub !== void 0) prevSub.nextSub = nextSub;
448
+ else if ((dep.subs = nextSub) === void 0) unwatch(dep);
449
+ return nextDep;
450
+ };
451
+ const purgeDeps = (sub) => {
452
+ const depsTail = sub.depsTail;
453
+ let dep = depsTail !== void 0 ? depsTail.nextDep : sub.deps;
454
+ while (dep !== void 0) dep = unlink(dep, sub);
455
+ };
456
+ /**
457
+ * Create a low-level signal dependency tracker for framework adapters.
458
+ *
459
+ * @remarks
460
+ * Adapter and framework authors import this helper from `coaction/adapter`.
461
+ */
462
+ const createReactiveTracker = () => {
463
+ let version = 0;
464
+ let disposed = false;
465
+ const listeners = /* @__PURE__ */ new Set();
466
+ const node = {
467
+ deps: void 0,
468
+ depsTail: void 0,
469
+ subs: void 0,
470
+ subsTail: void 0,
471
+ flags: ReactiveFlags.Watching,
472
+ fn: () => {
473
+ if (disposed) return;
474
+ version += 1;
475
+ listeners.forEach((listener) => listener());
476
+ }
477
+ };
478
+ const dispose = () => {
479
+ if (disposed) return;
480
+ disposed = true;
481
+ listeners.clear();
482
+ node.depsTail = void 0;
483
+ purgeDeps(node);
484
+ node.flags = 0;
485
+ };
486
+ return {
487
+ getSnapshot: () => version,
488
+ subscribe(listener) {
489
+ if (disposed) return () => void 0;
490
+ listeners.add(listener);
491
+ return () => {
492
+ listeners.delete(listener);
493
+ };
494
+ },
495
+ track(fn) {
496
+ if (disposed) return fn();
497
+ node.depsTail = void 0;
498
+ node.flags = ReactiveFlags.Watching | ReactiveFlags.RecursedCheck;
499
+ const prevSub = (0, alien_signals.setActiveSub)(node);
500
+ try {
501
+ return fn();
502
+ } finally {
503
+ (0, alien_signals.setActiveSub)(prevSub);
504
+ node.flags &= ~ReactiveFlags.RecursedCheck;
505
+ purgeDeps(node);
506
+ }
507
+ },
508
+ dispose
509
+ };
510
+ };
511
+ //#endregion
512
+ //#region packages/core/src/lifecycle.ts
513
+ const readyStores = /* @__PURE__ */ new WeakSet();
514
+ const readyCallbacks = /* @__PURE__ */ new WeakMap();
515
+ const onStoreReady = (store, callback) => {
516
+ if (readyStores.has(store)) {
517
+ callback();
518
+ return () => void 0;
519
+ }
520
+ let callbacks = readyCallbacks.get(store);
521
+ if (!callbacks) {
522
+ callbacks = /* @__PURE__ */ new Set();
523
+ readyCallbacks.set(store, callbacks);
524
+ }
525
+ callbacks.add(callback);
526
+ return () => {
527
+ callbacks?.delete(callback);
528
+ };
529
+ };
530
+ //#endregion
531
+ //#region packages/core/src/replaceExternalStoreState.ts
532
+ const replaceExternalStoreState = (store, internal, source, { syncImmutable = true } = {}) => {
533
+ internal.validateReplacementSource?.(source);
534
+ const [nextState, patches, inversePatches] = (0, mutative.create)(internal.rootState, (draft) => {
535
+ replaceOwnEnumerable(draft, source);
536
+ }, { enablePatches: true });
537
+ internal.validateState?.(nextState);
538
+ const safePatches = sanitizeCheckedPatches((store.patch ? store.patch({
539
+ patches,
540
+ inversePatches
541
+ }) : {
542
+ patches,
543
+ inversePatches
544
+ }).patches, "store.patch()");
545
+ if (!safePatches.length) return;
546
+ const updateImmutable = internal.updateImmutable;
547
+ if (!syncImmutable) internal.updateImmutable = void 0;
548
+ try {
549
+ store.apply(internal.rootState, safePatches);
550
+ } finally {
551
+ internal.updateImmutable = updateImmutable;
552
+ }
553
+ internal.emitPatches?.(safePatches);
554
+ };
555
+ //#endregion
556
+ //#region packages/core/src/wrapStore.ts
557
+ /**
558
+ * Convert a store object into Coaction's callable store shape.
559
+ *
560
+ * @remarks
561
+ * Framework bindings use this to attach selector-aware readers while
562
+ * preserving the underlying store API on the returned function object. Most
563
+ * applications should use a public `create` entry instead of calling
564
+ * `wrapStore()` directly. Framework authors import this helper from
565
+ * `coaction/local` or `coaction/adapter`.
566
+ */
567
+ const wrapStore = (store, getState = () => store.getState()) => {
568
+ const { name, ..._store } = store;
569
+ return Object.assign({ [name]: (...args) => getState(...args) }[name], _store);
570
+ };
571
+ //#endregion
572
+ exports.StateSchemaError = StateSchemaError;
573
+ exports.applyMutableAdapterPatches = applyMutableAdapterPatches;
574
+ exports.applyRootReplacementWithPatches = applyRootReplacementWithPatches;
575
+ exports.createBinder = createBinder;
576
+ exports.createReactiveTracker = createReactiveTracker;
577
+ exports.createRootReplacementPatches = createRootReplacementPatches;
578
+ exports.defineExternalStoreAdapter = defineExternalStoreAdapter;
579
+ exports.getMutableAdapterOwnEnumerableKeys = getMutableAdapterOwnEnumerableKeys;
580
+ exports.isEqualMutableAdapterSnapshot = isEqualMutableAdapterSnapshot;
581
+ exports.isMutableAdapterUnsafeKey = isMutableAdapterUnsafeKey;
582
+ exports.isStateSchemaError = isStateSchemaError;
583
+ exports.onStoreReady = onStoreReady;
584
+ exports.replaceExternalStoreState = replaceExternalStoreState;
585
+ exports.replaceMutableAdapterState = replaceMutableAdapterState;
586
+ exports.replaceOwnEnumerable = replaceOwnEnumerable;
587
+ exports.sanitizeInitialStateValue = sanitizeInitialStateValue;
588
+ exports.sanitizeReplacementState = sanitizeReplacementState;
589
+ exports.snapshotMutableAdapterPureState = snapshotMutableAdapterPureState;
590
+ exports.toMutableAdapterSnapshot = toMutableAdapterSnapshot;
591
+ exports.wrapStore = wrapStore;