coaction 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/dist/index.mjs CHANGED
@@ -1,1051 +1,1362 @@
1
- // src/create.ts
2
- import { apply as applyWithMutative } from "mutative";
3
-
4
- // src/global.ts
5
- var getGlobal = () => {
6
- let _global2;
7
- if (typeof window !== "undefined") {
8
- _global2 = window;
9
- } else if (typeof global !== "undefined") {
10
- _global2 = global;
11
- } else if (typeof self !== "undefined") {
12
- _global2 = self;
13
- } else {
14
- _global2 = {};
15
- }
16
- return _global2;
17
- };
18
- var _global = getGlobal();
19
-
20
- // src/constant.ts
21
- var WorkerType = _global.SharedWorkerGlobalScope ? "SharedWorkerInternal" : globalThis.WorkerGlobalScope ? "WebWorkerInternal" : null;
22
- var bindSymbol = /* @__PURE__ */ Symbol("bind");
23
- var defaultName = "default";
24
-
25
- // src/asyncClientStore.ts
1
+ import { apply, create as create$1, isDraft } from "mutative";
26
2
  import { createTransport } from "data-transport";
27
-
28
- // src/wrapStore.ts
29
- var wrapStore = (store, getState = () => store.getState()) => {
30
- const { name, ..._store } = store;
31
- return Object.assign(
32
- {
33
- [name]: (...args) => getState(...args)
34
- }[name],
35
- _store
36
- );
3
+ import { computed, computed as computed$1, effect, effectScope, endBatch, endBatch as endBatch$1, isComputed, isEffect, isEffectScope, isSignal, setActiveSub, signal, signal as signal$1, startBatch, startBatch as startBatch$1, trigger } from "alien-signals";
4
+ import * as alienSignalsSystem from "alien-signals/system";
5
+ //#region packages/core/src/global.ts
6
+ const getGlobal = () => {
7
+ let _global;
8
+ if (typeof window !== "undefined") _global = window;
9
+ else if (typeof global !== "undefined") _global = global;
10
+ else if (typeof self !== "undefined") _global = self;
11
+ else _global = {};
12
+ return _global;
13
+ };
14
+ //#endregion
15
+ //#region packages/core/src/constant.ts
16
+ const WorkerType = getGlobal().SharedWorkerGlobalScope ? "SharedWorkerInternal" : globalThis.WorkerGlobalScope ? "WebWorkerInternal" : null;
17
+ const bindSymbol = Symbol("bind");
18
+ //#endregion
19
+ //#region packages/core/src/wrapStore.ts
20
+ /**
21
+ * Convert a store object into Coaction's callable store shape.
22
+ *
23
+ * @remarks
24
+ * Framework bindings use this to attach selector-aware readers while
25
+ * preserving the underlying store API on the returned function object. Most
26
+ * applications should call {@link create} instead of using `wrapStore()`
27
+ * directly.
28
+ */
29
+ const wrapStore = (store, getState = () => store.getState()) => {
30
+ const { name, ..._store } = store;
31
+ return Object.assign({ [name]: (...args) => getState(...args) }[name], _store);
32
+ };
33
+ //#endregion
34
+ //#region packages/core/src/utils.ts
35
+ const isEqual = (x, y) => {
36
+ if (x === y) return x !== 0 || y !== 0 || 1 / x === 1 / y;
37
+ return x !== x && y !== y;
38
+ };
39
+ const isUnsafeKey = (key) => key === "__proto__" || key === "prototype" || key === "constructor";
40
+ const isUnsafePathSegment = (segment) => typeof segment === "string" && isUnsafeKey(segment);
41
+ const hasUnsafePatchPath = (path) => {
42
+ return (Array.isArray(path) ? path : typeof path === "string" ? path.split("/").filter(Boolean).map((segment) => segment.replace(/~1/g, "/").replace(/~0/g, "~")) : []).some(isUnsafePathSegment);
43
+ };
44
+ const sanitizePatches = (patches) => patches?.filter((patch) => !hasUnsafePatchPath(patch.path)).map((patch) => Object.prototype.hasOwnProperty.call(patch, "value") ? {
45
+ ...patch,
46
+ value: sanitizeReplacementState(patch.value)
47
+ } : patch);
48
+ const setOwnEnumerable = (target, key, value) => {
49
+ if (typeof key === "string" && isUnsafeKey(key)) return;
50
+ target[key] = value;
51
+ };
52
+ const getOwnEnumerableKeys = (source) => {
53
+ if (typeof source !== "object" || source === null) return [];
54
+ return Reflect.ownKeys(source).filter((key) => Object.prototype.propertyIsEnumerable.call(source, key));
55
+ };
56
+ const isArrayIndexKey$1 = (key) => {
57
+ if (typeof key !== "string") return false;
58
+ const index = Number(key);
59
+ return Number.isInteger(index) && index >= 0 && index < 2 ** 32 - 1 && String(index) === key;
60
+ };
61
+ const assignOwnEnumerable = (target, source, seen = /* @__PURE__ */ new WeakMap()) => {
62
+ if (!seen.has(source)) seen.set(source, target);
63
+ for (const key of getOwnEnumerableKeys(source)) setOwnEnumerable(target, key, sanitizeReplacementState(source[key], seen));
64
+ };
65
+ const replaceOwnEnumerable = (target, source) => {
66
+ const seen = /* @__PURE__ */ new WeakMap();
67
+ seen.set(source, target);
68
+ const nextKeys = /* @__PURE__ */ new Set();
69
+ for (const key of getOwnEnumerableKeys(source)) {
70
+ if (typeof key === "string" && isUnsafeKey(key)) continue;
71
+ if (typeof source[key] === "function") continue;
72
+ nextKeys.add(key);
73
+ }
74
+ for (const key of getOwnEnumerableKeys(target)) if (!nextKeys.has(key)) delete target[key];
75
+ nextKeys.forEach((key) => {
76
+ setOwnEnumerable(target, key, sanitizeReplacementState(source[key], seen));
77
+ });
78
+ };
79
+ const cloneOwnEnumerable = (source) => {
80
+ const target = {};
81
+ assignOwnEnumerable(target, source);
82
+ return target;
83
+ };
84
+ const sanitizeReplacementState = (source, seen = /* @__PURE__ */ new WeakMap()) => {
85
+ if (typeof source !== "object" || source === null) return source;
86
+ const cached = seen.get(source);
87
+ if (cached) return cached;
88
+ if (Array.isArray(source)) {
89
+ const target = [];
90
+ target.length = source.length;
91
+ seen.set(source, target);
92
+ for (let index = 0; index < source.length; index += 1) if (Object.prototype.hasOwnProperty.call(source, index)) target[index] = sanitizeReplacementState(source[index], seen);
93
+ for (const key of getOwnEnumerableKeys(source)) {
94
+ if (isArrayIndexKey$1(key) || typeof key === "string" && isUnsafeKey(key)) continue;
95
+ const value = source[key];
96
+ if (typeof value === "function") continue;
97
+ setOwnEnumerable(target, key, sanitizeReplacementState(value, seen));
98
+ }
99
+ return target;
100
+ }
101
+ const prototype = Object.getPrototypeOf(source);
102
+ if (prototype !== Object.prototype && prototype !== null) return source;
103
+ const target = Object.create(prototype);
104
+ seen.set(source, target);
105
+ for (const key of getOwnEnumerableKeys(source)) {
106
+ if (typeof key === "string" && isUnsafeKey(key)) continue;
107
+ const value = source[key];
108
+ if (typeof value === "function") continue;
109
+ setOwnEnumerable(target, key, sanitizeReplacementState(value, seen));
110
+ }
111
+ return target;
112
+ };
113
+ const sanitizeInitialStateValue = (source, seen = /* @__PURE__ */ new WeakMap()) => {
114
+ if (typeof source !== "object" || source === null) return source;
115
+ const cached = seen.get(source);
116
+ if (cached) return cached;
117
+ if (Array.isArray(source)) {
118
+ const target = [];
119
+ target.length = source.length;
120
+ seen.set(source, target);
121
+ for (let index = 0; index < source.length; index += 1) if (Object.prototype.hasOwnProperty.call(source, index)) target[index] = sanitizeInitialStateValue(source[index], seen);
122
+ for (const key of getOwnEnumerableKeys(source)) {
123
+ if (isArrayIndexKey$1(key) || typeof key === "string" && isUnsafeKey(key)) continue;
124
+ setOwnEnumerable(target, key, sanitizeInitialStateValue(source[key], seen));
125
+ }
126
+ return target;
127
+ }
128
+ const prototype = Object.getPrototypeOf(source);
129
+ if (prototype !== Object.prototype && prototype !== null) return source;
130
+ const target = Object.create(prototype);
131
+ seen.set(source, target);
132
+ for (const key of getOwnEnumerableKeys(source)) {
133
+ if (typeof key === "string" && isUnsafeKey(key)) continue;
134
+ setOwnEnumerable(target, key, sanitizeInitialStateValue(source[key], seen));
135
+ }
136
+ return target;
137
+ };
138
+ const areShallowEqualWithArray = (prev, next) => {
139
+ if (prev === null || next === null || prev.length !== next.length) return false;
140
+ const { length } = prev;
141
+ for (let i = 0; i < length; i += 1) {
142
+ if (Object.prototype.hasOwnProperty.call(prev, i) !== Object.prototype.hasOwnProperty.call(next, i)) return false;
143
+ if (!isEqual(prev[i], next[i])) return false;
144
+ }
145
+ return true;
146
+ };
147
+ const mergeObject = (target, source, isSlice) => {
148
+ if (isSlice) {
149
+ if (typeof source === "object" && source !== null) for (const key of getOwnEnumerableKeys(source)) {
150
+ if (typeof key === "string" && isUnsafeKey(key)) continue;
151
+ if (!Object.prototype.hasOwnProperty.call(target, key)) continue;
152
+ const sourceValue = source[key];
153
+ if (typeof sourceValue !== "object" || sourceValue === null) continue;
154
+ const targetValue = target[key];
155
+ if (typeof targetValue === "object" && targetValue !== null) assignOwnEnumerable(targetValue, sourceValue);
156
+ }
157
+ } else if (typeof source === "object" && source !== null) assignOwnEnumerable(target, source);
158
+ };
159
+ const uuid = () => {
160
+ let timestamp = (/* @__PURE__ */ new Date()).getTime();
161
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (char) => {
162
+ const randomNum = (timestamp + Math.random() * 16) % 16 | 0;
163
+ timestamp = Math.floor(timestamp / 16);
164
+ return (char === "x" ? randomNum : randomNum & 3 | 8).toString(16);
165
+ });
37
166
  };
38
-
39
- // src/asyncClientStore.ts
40
- var createAsyncClientStore = (createStore, asyncStoreClientOption) => {
41
- const { store: asyncClientStore, internal } = createStore({
42
- share: "client"
43
- });
44
- const transport = asyncStoreClientOption.worker ? createTransport(
45
- asyncStoreClientOption.worker instanceof SharedWorker ? "SharedWorkerClient" : "WebWorkerClient",
46
- {
47
- worker: asyncStoreClientOption.worker,
48
- prefix: asyncClientStore.name
49
- }
50
- ) : asyncStoreClientOption.clientTransport;
51
- if (!transport) {
52
- throw new Error("transport is required");
53
- }
54
- asyncClientStore.transport = transport;
55
- let syncingPromise = null;
56
- let awaitingReconnectSync = false;
57
- let reconnectSequenceBaseline = null;
58
- const fullSync = async (allowLowerSequence = false) => {
59
- if (!syncingPromise) {
60
- syncingPromise = (async () => {
61
- const latest = await transport.emit("fullSync");
62
- if (typeof latest.sequence !== "number" || typeof latest.state !== "string") {
63
- throw new Error("Invalid fullSync payload");
64
- }
65
- const canApplyLowerSequence = allowLowerSequence && awaitingReconnectSync && reconnectSequenceBaseline !== null && reconnectSequenceBaseline === internal.sequence;
66
- if (latest.sequence < internal.sequence && !canApplyLowerSequence) {
67
- return;
68
- }
69
- asyncClientStore.apply(JSON.parse(latest.state));
70
- internal.sequence = latest.sequence;
71
- awaitingReconnectSync = false;
72
- reconnectSequenceBaseline = null;
73
- })().finally(() => {
74
- syncingPromise = null;
75
- });
76
- }
77
- return syncingPromise;
78
- };
79
- if (typeof transport.onConnect !== "function") {
80
- throw new Error("transport.onConnect is required");
81
- }
82
- transport.onConnect?.(() => {
83
- awaitingReconnectSync = true;
84
- reconnectSequenceBaseline = internal.sequence;
85
- void fullSync(true).catch((error) => {
86
- if (process.env.NODE_ENV === "development") {
87
- console.error(error);
88
- }
89
- });
90
- });
91
- transport.listen("update", async (options) => {
92
- let shouldFullSync = false;
93
- let allowLowerSequence = false;
94
- try {
95
- if (typeof options.sequence !== "number") {
96
- shouldFullSync = true;
97
- } else if (options.sequence <= internal.sequence) {
98
- if (awaitingReconnectSync) {
99
- shouldFullSync = true;
100
- allowLowerSequence = true;
101
- } else if (options.sequence === 0 && internal.sequence > 0) {
102
- awaitingReconnectSync = true;
103
- reconnectSequenceBaseline = internal.sequence;
104
- shouldFullSync = true;
105
- allowLowerSequence = true;
106
- } else {
107
- return;
108
- }
109
- } else if (options.sequence === internal.sequence + 1) {
110
- asyncClientStore.apply(void 0, options.patches);
111
- internal.sequence = options.sequence;
112
- awaitingReconnectSync = false;
113
- reconnectSequenceBaseline = null;
114
- return;
115
- } else {
116
- shouldFullSync = true;
117
- allowLowerSequence = awaitingReconnectSync;
118
- }
119
- if (shouldFullSync) {
120
- await fullSync(allowLowerSequence);
121
- }
122
- } catch (error) {
123
- if (!shouldFullSync) {
124
- try {
125
- await fullSync(awaitingReconnectSync);
126
- } catch (syncError) {
127
- if (process.env.NODE_ENV === "development") {
128
- console.error(syncError);
129
- }
130
- }
131
- }
132
- if (process.env.NODE_ENV === "development") {
133
- console.error(error);
134
- }
135
- }
136
- });
137
- return wrapStore(asyncClientStore, () => asyncClientStore.getState());
167
+ //#endregion
168
+ //#region packages/core/src/sharedState.ts
169
+ const formatPropertyPath = (path) => path.length ? path.map((key) => String(key)).join(".") : "<root>";
170
+ const isPlainObject = (value) => {
171
+ const prototype = Object.getPrototypeOf(value);
172
+ return prototype === Object.prototype || prototype === null;
138
173
  };
139
- var emit = (store, internal, patches) => {
140
- if (store.transport && patches?.length) {
141
- internal.sequence += 1;
142
- store.transport.emit(
143
- {
144
- name: "update",
145
- respond: false
146
- },
147
- {
148
- patches,
149
- sequence: internal.sequence
150
- }
151
- );
152
- }
174
+ const isArrayIndexKey = (key, length) => {
175
+ if (key === "") return false;
176
+ const index = Number(key);
177
+ return Number.isInteger(index) && index >= 0 && index < length && String(index) === key;
153
178
  };
154
- var handleDraft = (store, internal) => {
155
- internal.rootState = internal.backupState;
156
- const [, patches, inversePatches] = internal.finalizeDraft();
157
- const finalPatches = store.patch ? store.patch({ patches, inversePatches }) : { patches, inversePatches };
158
- if (finalPatches.patches.length) {
159
- store.apply(internal.rootState, finalPatches.patches);
160
- emit(store, internal, finalPatches.patches);
161
- }
179
+ const findSymbolKeyViolation = (value, path = [], seen = /* @__PURE__ */ new WeakSet()) => {
180
+ if (typeof value !== "object" || value === null) return;
181
+ if (seen.has(value)) return;
182
+ seen.add(value);
183
+ const descriptors = Object.getOwnPropertyDescriptors(value);
184
+ for (const key of getOwnEnumerableKeys(value)) {
185
+ const nextPath = [...path, key];
186
+ if (typeof key === "symbol") return {
187
+ type: "symbol-key",
188
+ path: nextPath
189
+ };
190
+ const descriptor = descriptors[key];
191
+ if (descriptor && Object.prototype.hasOwnProperty.call(descriptor, "value")) {
192
+ const violation = findSymbolKeyViolation(descriptor.value, nextPath, seen);
193
+ if (violation) return violation;
194
+ }
195
+ }
162
196
  };
163
-
164
- // src/getInitialState.ts
165
- var isObject = (value) => typeof value === "object" && value !== null;
166
- var isStateFactory = (value) => typeof value === "function";
167
- var hasGetState = (value) => (typeof value === "object" || typeof value === "function") && value !== null && typeof value.getState === "function";
168
- var hasBindState = (value) => isObject(value) && !!value[bindSymbol];
169
- var formatInvalidStateMessage = (type, stateOrFn, key) => `Invalid state ${type} encountered in makeState: ${key ? `for key ${key}, ` : ""}${typeof stateOrFn}`;
170
- var getInitialState = (store, createState, internal) => {
171
- const makeState = (stateOrFn, key) => {
172
- let state;
173
- if (isStateFactory(stateOrFn)) {
174
- state = stateOrFn(store.setState, store.getState, store);
175
- } else if (isObject(stateOrFn)) {
176
- state = stateOrFn;
177
- } else {
178
- if (process.env.NODE_ENV !== "production") {
179
- throw new Error(formatInvalidStateMessage("value", stateOrFn, key));
180
- }
181
- return {};
182
- }
183
- if (hasGetState(state)) {
184
- state = state.getState();
185
- } else if (typeof state === "function") {
186
- state = state();
187
- }
188
- if (hasBindState(state)) {
189
- if (store.isSliceStore) {
190
- throw new Error(
191
- "Third-party state binding does not support Slices mode. Please inject a whole store instead."
192
- );
193
- }
194
- const binder = state[bindSymbol];
195
- const rawState = binder.bind(state);
196
- binder.handleStore(
197
- store,
198
- rawState,
199
- state,
200
- internal,
201
- key
202
- );
203
- delete state[bindSymbol];
204
- return rawState;
205
- }
206
- if (!isObject(state)) {
207
- if (process.env.NODE_ENV !== "production") {
208
- throw new Error(formatInvalidStateMessage("result", state, key));
209
- }
210
- return {};
211
- }
212
- return state;
213
- };
214
- return store.isSliceStore ? Object.entries(createState).reduce(
215
- (stateTree, [key, value]) => Object.assign(stateTree, {
216
- [key]: makeState(value, key)
217
- }),
218
- {}
219
- ) : makeState(createState);
197
+ const findJsonViolation = (value, path = [], ancestors = /* @__PURE__ */ new WeakSet()) => {
198
+ switch (typeof value) {
199
+ case "symbol": return {
200
+ type: "symbol-value",
201
+ path
202
+ };
203
+ case "bigint": return {
204
+ type: "bigint",
205
+ path
206
+ };
207
+ case "undefined": return {
208
+ type: "undefined",
209
+ path
210
+ };
211
+ case "function": return {
212
+ type: "function",
213
+ path
214
+ };
215
+ case "number": return Number.isFinite(value) ? void 0 : {
216
+ type: "non-finite-number",
217
+ path
218
+ };
219
+ default: break;
220
+ }
221
+ if (typeof value !== "object" || value === null) return;
222
+ if (ancestors.has(value)) return {
223
+ type: "circular-reference",
224
+ path
225
+ };
226
+ if (Array.isArray(value)) {
227
+ ancestors.add(value);
228
+ for (let index = 0; index < value.length; index += 1) if (!Object.prototype.hasOwnProperty.call(value, index)) return {
229
+ type: "array-hole",
230
+ path: [...path, index]
231
+ };
232
+ for (const key of getOwnEnumerableKeys(value)) {
233
+ const nextPath = [...path, key];
234
+ if (typeof key === "symbol") return {
235
+ type: "symbol-key",
236
+ path: nextPath
237
+ };
238
+ if (!isArrayIndexKey(key, value.length)) return {
239
+ type: "array-property",
240
+ path: nextPath
241
+ };
242
+ const violation = findJsonViolation(value[Number(key)], nextPath, ancestors);
243
+ if (violation) return violation;
244
+ }
245
+ ancestors.delete(value);
246
+ return;
247
+ }
248
+ if (!isPlainObject(value)) return {
249
+ type: "non-plain-object",
250
+ path
251
+ };
252
+ if (typeof value.toJSON === "function") return {
253
+ type: "to-json",
254
+ path
255
+ };
256
+ ancestors.add(value);
257
+ for (const key of getOwnEnumerableKeys(value)) {
258
+ const nextPath = [...path, key];
259
+ if (typeof key === "symbol") return {
260
+ type: "symbol-key",
261
+ path: nextPath
262
+ };
263
+ const child = value[key];
264
+ const violation = findJsonViolation(child, nextPath, ancestors);
265
+ if (violation) return violation;
266
+ }
267
+ ancestors.delete(value);
220
268
  };
221
-
222
- // src/utils.ts
223
- var isEqual = (x, y) => {
224
- if (x === y) {
225
- return x !== 0 || y !== 0 || 1 / x === 1 / y;
226
- }
227
- return x !== x && y !== y;
269
+ const getViolationLabel = (violation) => {
270
+ switch (violation.type) {
271
+ case "bigint": return "BigInt-valued state";
272
+ case "undefined": return "Undefined-valued state";
273
+ case "function": return "Function-valued state";
274
+ case "non-finite-number": return "NaN or infinite number state";
275
+ case "non-plain-object": return "Non-plain object state";
276
+ case "circular-reference": return "Circular state reference";
277
+ case "array-hole": return "Sparse array state";
278
+ case "array-property": return "Non-index array property state";
279
+ case "to-json": return "Custom toJSON state";
280
+ default: return;
281
+ }
228
282
  };
229
- var isUnsafeKey = (key) => key === "__proto__" || key === "prototype" || key === "constructor";
230
- var assignOwnEnumerable = (target, source) => {
231
- for (const key of Object.keys(source)) {
232
- if (isUnsafeKey(key)) {
233
- continue;
234
- }
235
- target[key] = source[key];
236
- }
283
+ const validateSharedActionPaths = (state) => {
284
+ const violation = findSymbolKeyViolation(state);
285
+ if (!violation) return;
286
+ throw new Error(`Symbol-keyed state is not supported in shared store mode because transport synchronization uses JSON and string action paths. Found symbol key at ${formatPropertyPath(violation.path)}.`);
237
287
  };
238
- var areShallowEqualWithArray = (prev, next) => {
239
- if (prev === null || next === null || prev.length !== next.length) {
240
- return false;
241
- }
242
- const { length } = prev;
243
- for (let i = 0; i < length; i += 1) {
244
- if (!isEqual(prev[i], next[i])) {
245
- return false;
246
- }
247
- }
248
- return true;
288
+ const validateSharedStateSerializable = (state) => {
289
+ const violation = findJsonViolation(state);
290
+ if (!violation) return;
291
+ if (violation.type === "symbol-key") throw new Error(`Symbol-keyed state is not supported in shared store mode because transport synchronization uses JSON and string action paths. Found symbol key at ${formatPropertyPath(violation.path)}.`);
292
+ if (violation.type === "symbol-value") throw new Error(`Symbol-valued state is not supported in shared store mode because transport synchronization uses JSON. Found symbol value at ${formatPropertyPath(violation.path)}.`);
293
+ throw new Error(`${getViolationLabel(violation)} is not supported in shared store mode because transport synchronization uses JSON. Found unsupported value at ${formatPropertyPath(violation.path)}.`);
249
294
  };
250
- var mergeObject = (target, source, isSlice) => {
251
- if (isSlice) {
252
- if (typeof source === "object" && source !== null) {
253
- for (const key of Object.keys(source)) {
254
- if (isUnsafeKey(key)) {
255
- continue;
256
- }
257
- if (!Object.prototype.hasOwnProperty.call(target, key)) {
258
- continue;
259
- }
260
- const sourceValue = source[key];
261
- if (typeof sourceValue !== "object" || sourceValue === null) {
262
- continue;
263
- }
264
- const targetValue = target[key];
265
- if (typeof targetValue === "object" && targetValue !== null) {
266
- assignOwnEnumerable(targetValue, sourceValue);
267
- }
268
- }
269
- }
270
- } else {
271
- if (typeof source === "object" && source !== null) {
272
- assignOwnEnumerable(target, source);
273
- }
274
- }
295
+ //#endregion
296
+ //#region packages/core/src/asyncClientStore.ts
297
+ const parseFullSyncState$1 = (state) => {
298
+ const parsed = JSON.parse(state);
299
+ if (typeof parsed !== "object" || parsed === null) throw new Error("Invalid fullSync payload");
300
+ return sanitizeReplacementState(parsed);
275
301
  };
276
- var uuid = () => {
277
- let timestamp = (/* @__PURE__ */ new Date()).getTime();
278
- const uuidTemplate = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
279
- const uuid2 = uuidTemplate.replace(/[xy]/g, (char) => {
280
- const randomNum = (timestamp + Math.random() * 16) % 16 | 0;
281
- timestamp = Math.floor(timestamp / 16);
282
- return (char === "x" ? randomNum : randomNum & 3 | 8).toString(16);
283
- });
284
- return uuid2;
302
+ const clientApplyErrorMessage = "apply() cannot be called in the client store. Client stores are mirrors; use a store method to update the main store instead.";
303
+ const createAsyncClientStore = (createStore, asyncStoreClientOption) => {
304
+ const { store: asyncClientStore, internal } = createStore({ share: "client" });
305
+ let isApplyingClientState = false;
306
+ const previousAssertMutationAllowed = internal.assertMutationAllowed;
307
+ internal.assertMutationAllowed = (operation) => {
308
+ if (operation === "apply" && !isApplyingClientState) throw new Error(clientApplyErrorMessage);
309
+ previousAssertMutationAllowed?.(operation);
310
+ };
311
+ const baseApply = asyncClientStore.apply.bind(asyncClientStore);
312
+ asyncClientStore.apply = (state, patches) => {
313
+ if (!isApplyingClientState) throw new Error(clientApplyErrorMessage);
314
+ return baseApply(state, patches);
315
+ };
316
+ internal.applyClientState = (...args) => {
317
+ isApplyingClientState = true;
318
+ try {
319
+ baseApply(...args);
320
+ } finally {
321
+ isApplyingClientState = false;
322
+ }
323
+ };
324
+ const isSharedWorker = typeof SharedWorker !== "undefined" && asyncStoreClientOption.worker instanceof SharedWorker;
325
+ const transport = asyncStoreClientOption.worker ? createTransport(isSharedWorker ? "SharedWorkerClient" : "WebWorkerClient", {
326
+ worker: asyncStoreClientOption.worker,
327
+ prefix: asyncClientStore.name
328
+ }) : asyncStoreClientOption.clientTransport;
329
+ if (!transport) throw new Error("transport is required");
330
+ asyncClientStore.transport = transport;
331
+ let syncingPromise = null;
332
+ let awaitingReconnectSync = false;
333
+ let reconnectSequenceBaseline = null;
334
+ const fullSync = async (allowLowerSequence = false) => {
335
+ if (!syncingPromise) syncingPromise = (async () => {
336
+ const latest = await transport.emit("fullSync");
337
+ if (typeof latest !== "object" || latest === null || typeof latest.sequence !== "number" || typeof latest.state !== "string") throw new Error("Invalid fullSync payload");
338
+ const canApplyLowerSequence = allowLowerSequence && awaitingReconnectSync && reconnectSequenceBaseline !== null && reconnectSequenceBaseline === internal.sequence;
339
+ if (latest.sequence < internal.sequence && !canApplyLowerSequence) return;
340
+ internal.applyClientState(parseFullSyncState$1(latest.state));
341
+ internal.sequence = latest.sequence;
342
+ awaitingReconnectSync = false;
343
+ reconnectSequenceBaseline = null;
344
+ })().finally(() => {
345
+ syncingPromise = null;
346
+ });
347
+ return syncingPromise;
348
+ };
349
+ if (typeof transport.onConnect !== "function") throw new Error("transport.onConnect is required");
350
+ transport.onConnect?.(() => {
351
+ awaitingReconnectSync = true;
352
+ reconnectSequenceBaseline = internal.sequence;
353
+ fullSync(true).catch((error) => {
354
+ if (process.env.NODE_ENV === "development") console.error(error);
355
+ });
356
+ });
357
+ transport.listen("update", async (options) => {
358
+ let shouldFullSync = false;
359
+ let allowLowerSequence = false;
360
+ try {
361
+ if (typeof options.sequence !== "number") shouldFullSync = true;
362
+ else if (options.sequence <= internal.sequence) if (awaitingReconnectSync) {
363
+ shouldFullSync = true;
364
+ allowLowerSequence = true;
365
+ } else if (options.sequence === 0 && internal.sequence > 0) {
366
+ awaitingReconnectSync = true;
367
+ reconnectSequenceBaseline = internal.sequence;
368
+ shouldFullSync = true;
369
+ allowLowerSequence = true;
370
+ } else return;
371
+ else if (options.sequence === internal.sequence + 1) {
372
+ internal.applyClientState(void 0, options.patches);
373
+ internal.sequence = options.sequence;
374
+ awaitingReconnectSync = false;
375
+ reconnectSequenceBaseline = null;
376
+ return;
377
+ } else {
378
+ shouldFullSync = true;
379
+ allowLowerSequence = awaitingReconnectSync;
380
+ }
381
+ if (shouldFullSync) await fullSync(allowLowerSequence);
382
+ } catch (error) {
383
+ if (!shouldFullSync) try {
384
+ await fullSync(awaitingReconnectSync);
385
+ } catch (syncError) {
386
+ if (process.env.NODE_ENV === "development") console.error(syncError);
387
+ }
388
+ if (process.env.NODE_ENV === "development") console.error(error);
389
+ }
390
+ });
391
+ return wrapStore(asyncClientStore, () => asyncClientStore.getState());
285
392
  };
286
-
287
- // src/getRawStateClientAction.ts
288
- var transportErrorMarker = "__coactionTransportError__";
289
- var isTransportErrorEnvelope = (value) => {
290
- if (typeof value !== "object" || value === null) {
291
- return false;
292
- }
293
- return value[transportErrorMarker] === true && typeof value.message === "string";
393
+ const emit = (store, internal, patches) => {
394
+ const safePatches = sanitizePatches(patches);
395
+ if (store.transport && safePatches?.length) {
396
+ validateSharedStateSerializable(internal.rootState);
397
+ internal.sequence += 1;
398
+ store.transport.emit({
399
+ name: "update",
400
+ respond: false
401
+ }, {
402
+ patches: safePatches,
403
+ sequence: internal.sequence
404
+ });
405
+ }
294
406
  };
295
- var isLegacyTransportErrorEnvelope = (value) => {
296
- if (typeof value !== "object" || value === null) {
297
- return false;
298
- }
299
- const candidate = value;
300
- return typeof candidate.$$Error === "string" && candidate.$$Error.length > 0 && Object.keys(candidate).length === 1;
407
+ const handleDraft = (store, internal) => {
408
+ internal.rootState = internal.backupState;
409
+ const [nextState, patches, inversePatches] = internal.finalizeDraft();
410
+ if (store.share === "main") validateSharedStateSerializable(nextState);
411
+ const safePatches = sanitizePatches((store.patch ? store.patch({
412
+ patches,
413
+ inversePatches
414
+ }) : {
415
+ patches,
416
+ inversePatches
417
+ }).patches) ?? [];
418
+ if (safePatches.length) {
419
+ store.apply(internal.rootState, safePatches);
420
+ emit(store, internal, safePatches);
421
+ }
301
422
  };
302
- var createClientAction = ({
303
- clientExecuteSyncTimeoutMs,
304
- internal,
305
- key,
306
- store,
307
- sliceKey
308
- }) => {
309
- return (...args) => {
310
- let actionId;
311
- let done;
312
- if (store.trace) {
313
- actionId = uuid();
314
- store.trace({
315
- method: key,
316
- parameters: args,
317
- id: actionId,
318
- sliceKey
319
- });
320
- done = (result) => {
321
- store.trace({
322
- method: key,
323
- id: actionId,
324
- result,
325
- sliceKey
326
- });
327
- };
328
- }
329
- const keys = sliceKey ? [sliceKey, key] : [key];
330
- return store.transport.emit("execute", keys, args).then(async (response) => {
331
- const result = Array.isArray(response) ? response[0] : response;
332
- const sequence = Array.isArray(response) ? typeof response[1] === "number" ? response[1] : internal.sequence : internal.sequence;
333
- if (internal.sequence < sequence) {
334
- if (process.env.NODE_ENV === "development") {
335
- console.warn(
336
- `The sequence of the action is not consistent.`,
337
- sequence,
338
- internal.sequence
339
- );
340
- }
341
- await new Promise((resolve, reject) => {
342
- let settled = false;
343
- let unsubscribe = () => {
344
- };
345
- const timeoutRef = {};
346
- const cleanup = () => {
347
- unsubscribe();
348
- if (typeof timeoutRef.current !== "undefined") {
349
- clearTimeout(timeoutRef.current);
350
- }
351
- };
352
- const finishResolve = () => {
353
- if (settled) return;
354
- settled = true;
355
- cleanup();
356
- resolve();
357
- };
358
- const finishReject = (error) => {
359
- if (settled) return;
360
- settled = true;
361
- cleanup();
362
- reject(error);
363
- };
364
- unsubscribe = store.subscribe(() => {
365
- if (internal.sequence >= sequence) {
366
- finishResolve();
367
- }
368
- });
369
- timeoutRef.current = setTimeout(() => {
370
- void store.transport.emit("fullSync").then((latest) => {
371
- const next = latest;
372
- if (typeof next.state !== "string" || typeof next.sequence !== "number") {
373
- throw new Error("Invalid fullSync payload");
374
- }
375
- store.apply(JSON.parse(next.state));
376
- internal.sequence = next.sequence;
377
- if (internal.sequence >= sequence) {
378
- finishResolve();
379
- return;
380
- }
381
- finishReject(
382
- new Error(
383
- `Stale fullSync sequence: expected >= ${sequence}, got ${internal.sequence}`
384
- )
385
- );
386
- }).catch((error) => {
387
- finishReject(error);
388
- });
389
- }, clientExecuteSyncTimeoutMs);
390
- });
391
- }
392
- if (isTransportErrorEnvelope(result)) {
393
- done?.(result);
394
- throw new Error(result.message);
395
- }
396
- if (isLegacyTransportErrorEnvelope(result)) {
397
- done?.(result);
398
- throw new Error(result.$$Error);
399
- }
400
- done?.(result);
401
- return result;
402
- });
403
- };
423
+ //#endregion
424
+ //#region packages/core/src/getInitialState.ts
425
+ const isObject = (value) => typeof value === "object" && value !== null;
426
+ const isStateFactory = (value) => typeof value === "function";
427
+ const hasGetState = (value) => (typeof value === "object" || typeof value === "function") && value !== null && typeof value.getState === "function";
428
+ const hasBindState = (value) => isObject(value) && !!value[bindSymbol];
429
+ const formatInvalidStateMessage = (type, stateOrFn, key) => `Invalid state ${type} encountered in makeState: ${typeof key !== "undefined" ? `for key ${String(key)}, ` : ""}${typeof stateOrFn}`;
430
+ const getInitialState = (store, createState, internal) => {
431
+ const makeState = (stateOrFn, key) => {
432
+ let state;
433
+ if (isStateFactory(stateOrFn)) state = stateOrFn(store.setState, store.getState, store);
434
+ else if (isObject(stateOrFn)) state = stateOrFn;
435
+ else {
436
+ if (process.env.NODE_ENV !== "production") throw new Error(formatInvalidStateMessage("value", stateOrFn, key));
437
+ return {};
438
+ }
439
+ if (hasGetState(state)) state = state.getState();
440
+ else if (typeof state === "function") state = state();
441
+ if (hasBindState(state)) {
442
+ if (store.isSliceStore) throw new Error("Third-party state binding does not support Slices mode. Please inject a whole store instead.");
443
+ const binder = state[bindSymbol];
444
+ const rawState = binder.bind(state);
445
+ binder.handleStore(store, rawState, state, internal, key);
446
+ delete state[bindSymbol];
447
+ return rawState;
448
+ }
449
+ if (!isObject(state)) {
450
+ if (process.env.NODE_ENV !== "production") throw new Error(formatInvalidStateMessage("result", state, key));
451
+ return {};
452
+ }
453
+ return state;
454
+ };
455
+ if (!store.isSliceStore) return makeState(createState);
456
+ return getOwnEnumerableKeys(createState).reduce((stateTree, key) => {
457
+ if (typeof key === "string" && isUnsafeKey(key)) return stateTree;
458
+ setOwnEnumerable(stateTree, key, makeState(createState[key], key));
459
+ return stateTree;
460
+ }, {});
404
461
  };
405
-
406
- // src/getRawStateLocalAction.ts
407
- import {
408
- create as createWithMutative,
409
- isDraft
410
- } from "mutative";
411
- var getActionTarget = (store, sliceKey) => {
412
- return sliceKey ? store.getState()[sliceKey] : store.getState();
462
+ //#endregion
463
+ //#region packages/core/src/getRawStateClientAction.ts
464
+ const transportErrorMarker$1 = "__coactionTransportError__";
465
+ const parseFullSyncState = (state) => {
466
+ const parsed = JSON.parse(state);
467
+ if (typeof parsed !== "object" || parsed === null) throw new Error("Invalid fullSync payload");
468
+ return sanitizeReplacementState(parsed);
413
469
  };
414
- var createLocalAction = ({
415
- fn,
416
- internal,
417
- key,
418
- options,
419
- store,
420
- sliceKey
421
- }) => {
422
- return (...args) => {
423
- let actionId;
424
- let done;
425
- if (store.trace) {
426
- actionId = uuid();
427
- store.trace({
428
- method: key,
429
- parameters: args,
430
- id: actionId,
431
- sliceKey
432
- });
433
- done = (result2) => {
434
- store.trace({
435
- method: key,
436
- id: actionId,
437
- result: result2,
438
- sliceKey
439
- });
440
- };
441
- }
442
- const enablePatches = store.transport ?? options.enablePatches;
443
- if (internal.mutableInstance && !internal.isBatching && enablePatches) {
444
- let result2;
445
- const handleResult = (isDrafted2) => {
446
- handleDraft(store, internal);
447
- if (isDrafted2) {
448
- internal.backupState = internal.rootState;
449
- const [draft2, finalize2] = createWithMutative(internal.rootState, {
450
- enablePatches: true
451
- });
452
- internal.finalizeDraft = finalize2;
453
- internal.rootState = draft2;
454
- }
455
- };
456
- const isDrafted = isDraft(internal.rootState);
457
- if (isDrafted) {
458
- handleResult();
459
- }
460
- internal.backupState = internal.rootState;
461
- const [draft, finalize] = createWithMutative(internal.rootState, {
462
- enablePatches: true
463
- });
464
- internal.finalizeDraft = finalize;
465
- internal.rootState = draft;
466
- let asyncResult;
467
- try {
468
- result2 = fn.apply(getActionTarget(store, sliceKey), args);
469
- if (result2 instanceof Promise) {
470
- asyncResult = result2;
471
- }
472
- } finally {
473
- if (asyncResult) {
474
- } else {
475
- handleResult(isDrafted);
476
- }
477
- }
478
- if (asyncResult) {
479
- return asyncResult.finally(() => {
480
- const result3 = handleResult(isDrafted);
481
- done?.(result3);
482
- return result3;
483
- });
484
- }
485
- done?.(result2);
486
- return result2;
487
- }
488
- if (internal.mutableInstance && internal.actMutable) {
489
- const result2 = internal.actMutable(() => {
490
- return fn.apply(getActionTarget(store, sliceKey), args);
491
- });
492
- done?.(result2);
493
- return result2;
494
- }
495
- const result = fn.apply(getActionTarget(store, sliceKey), args);
496
- done?.(result);
497
- return result;
498
- };
470
+ const isTransportErrorEnvelope = (value) => {
471
+ if (typeof value !== "object" || value === null) return false;
472
+ return value[transportErrorMarker$1] === true && typeof value.message === "string";
499
473
  };
500
-
501
- // src/computed.ts
474
+ const isLegacyTransportErrorEnvelope = (value) => {
475
+ if (typeof value !== "object" || value === null) return false;
476
+ const candidate = value;
477
+ return typeof candidate.$$Error === "string" && candidate.$$Error.length > 0 && Object.keys(candidate).length === 1;
478
+ };
479
+ const createClientAction = ({ clientExecuteSyncTimeoutMs, internal, key, store, sliceKey }) => {
480
+ return (...args) => {
481
+ let actionId;
482
+ let done;
483
+ if (store.trace) {
484
+ actionId = uuid();
485
+ store.trace({
486
+ method: key,
487
+ parameters: args,
488
+ id: actionId,
489
+ sliceKey
490
+ });
491
+ done = (result) => {
492
+ store.trace({
493
+ method: key,
494
+ id: actionId,
495
+ result,
496
+ sliceKey
497
+ });
498
+ };
499
+ }
500
+ const traceAction = (run) => {
501
+ try {
502
+ const result = run();
503
+ if (result instanceof Promise) return result.then((value) => {
504
+ done?.(value);
505
+ return value;
506
+ }, (error) => {
507
+ done?.(error);
508
+ throw error;
509
+ });
510
+ done?.(result);
511
+ return result;
512
+ } catch (error) {
513
+ done?.(error);
514
+ throw error;
515
+ }
516
+ };
517
+ if (typeof sliceKey === "symbol") throw new Error("Symbol-keyed slice actions are not supported in client store mode.");
518
+ const keys = typeof sliceKey !== "undefined" ? [String(sliceKey), key] : [key];
519
+ return traceAction(() => store.transport.emit("execute", keys, args).then(async (response) => {
520
+ const result = Array.isArray(response) ? response[0] : response;
521
+ const sequence = Array.isArray(response) ? typeof response[1] === "number" ? response[1] : internal.sequence : internal.sequence;
522
+ if (internal.sequence < sequence) {
523
+ if (process.env.NODE_ENV === "development") console.warn(`The sequence of the action is not consistent.`, sequence, internal.sequence);
524
+ await new Promise((resolve, reject) => {
525
+ let settled = false;
526
+ let unsubscribe = () => {};
527
+ const timeoutRef = {};
528
+ const cleanup = () => {
529
+ unsubscribe();
530
+ if (typeof timeoutRef.current !== "undefined") clearTimeout(timeoutRef.current);
531
+ };
532
+ const finishResolve = () => {
533
+ if (settled) return;
534
+ settled = true;
535
+ cleanup();
536
+ resolve();
537
+ };
538
+ const finishReject = (error) => {
539
+ if (settled) return;
540
+ settled = true;
541
+ cleanup();
542
+ reject(error);
543
+ };
544
+ unsubscribe = store.subscribe(() => {
545
+ if (internal.sequence >= sequence) finishResolve();
546
+ });
547
+ timeoutRef.current = setTimeout(() => {
548
+ store.transport.emit("fullSync").then((latest) => {
549
+ if (typeof latest !== "object" || latest === null) throw new Error("Invalid fullSync payload");
550
+ const next = latest;
551
+ if (typeof next.state !== "string" || typeof next.sequence !== "number") throw new Error("Invalid fullSync payload");
552
+ if (next.sequence >= sequence) {
553
+ (internal.applyClientState ?? store.apply.bind(store))(parseFullSyncState(next.state));
554
+ internal.sequence = next.sequence;
555
+ finishResolve();
556
+ return;
557
+ }
558
+ finishReject(/* @__PURE__ */ new Error(`Stale fullSync sequence: expected >= ${sequence}, got ${next.sequence}`));
559
+ }).catch((error) => {
560
+ finishReject(error);
561
+ });
562
+ }, clientExecuteSyncTimeoutMs);
563
+ });
564
+ }
565
+ if (isTransportErrorEnvelope(result)) throw new Error(result.message);
566
+ if (isLegacyTransportErrorEnvelope(result)) throw new Error(result.$$Error);
567
+ return result;
568
+ }));
569
+ };
570
+ };
571
+ //#endregion
572
+ //#region packages/core/src/getRawStateLocalAction.ts
573
+ const getActionTarget = (store, sliceKey) => {
574
+ return typeof sliceKey !== "undefined" ? store.getState()[sliceKey] : store.getState();
575
+ };
576
+ const createLocalAction = ({ fn, internal, key, options, store, sliceKey }) => {
577
+ return (...args) => {
578
+ let actionId;
579
+ let done;
580
+ if (store.trace) {
581
+ actionId = uuid();
582
+ store.trace({
583
+ method: String(key),
584
+ parameters: args,
585
+ id: actionId,
586
+ sliceKey
587
+ });
588
+ done = (result) => {
589
+ store.trace({
590
+ method: String(key),
591
+ id: actionId,
592
+ result,
593
+ sliceKey
594
+ });
595
+ };
596
+ }
597
+ const traceAction = (run) => {
598
+ try {
599
+ const result = run();
600
+ if (result instanceof Promise) return result.then((value) => {
601
+ done?.(value);
602
+ return value;
603
+ }, (error) => {
604
+ done?.(error);
605
+ throw error;
606
+ });
607
+ done?.(result);
608
+ return result;
609
+ } catch (error) {
610
+ done?.(error);
611
+ throw error;
612
+ }
613
+ };
614
+ const enablePatches = store.transport ?? options.enablePatches;
615
+ return traceAction(() => {
616
+ if (internal.mutableInstance && !internal.isBatching && enablePatches) {
617
+ let result;
618
+ const handleResult = (isDrafted) => {
619
+ handleDraft(store, internal);
620
+ if (isDrafted) {
621
+ internal.backupState = internal.rootState;
622
+ const [draft, finalize] = create$1(internal.rootState, { enablePatches: true });
623
+ internal.finalizeDraft = finalize;
624
+ internal.rootState = draft;
625
+ }
626
+ };
627
+ const isDrafted = isDraft(internal.rootState);
628
+ if (isDrafted) handleResult();
629
+ internal.backupState = internal.rootState;
630
+ const [draft, finalize] = create$1(internal.rootState, { enablePatches: true });
631
+ internal.finalizeDraft = finalize;
632
+ internal.rootState = draft;
633
+ let asyncResult;
634
+ try {
635
+ result = fn.apply(getActionTarget(store, sliceKey), args);
636
+ if (result instanceof Promise) asyncResult = result;
637
+ } finally {
638
+ if (!asyncResult) handleResult(isDrafted);
639
+ }
640
+ if (asyncResult) return asyncResult.then((value) => {
641
+ handleResult(isDrafted);
642
+ return value;
643
+ }, (error) => {
644
+ handleResult(isDrafted);
645
+ throw error;
646
+ });
647
+ return result;
648
+ }
649
+ if (internal.mutableInstance && internal.actMutable) return internal.actMutable(() => {
650
+ return fn.apply(getActionTarget(store, sliceKey), args);
651
+ });
652
+ return fn.apply(getActionTarget(store, sliceKey), args);
653
+ });
654
+ };
655
+ };
656
+ //#endregion
657
+ //#region packages/core/src/computed.ts
658
+ const isObjectLike = (value) => typeof value === "object" && value !== null;
502
659
  var Computed = class {
503
- constructor(deps, fn) {
504
- this.deps = deps;
505
- this.fn = fn;
506
- }
660
+ deps;
661
+ fn;
662
+ constructor(deps, fn) {
663
+ this.deps = deps;
664
+ this.fn = fn;
665
+ }
666
+ createGetter({ internal }) {
667
+ const memoByReceiver = /* @__PURE__ */ new WeakMap();
668
+ const lastArgs = /* @__PURE__ */ new WeakMap();
669
+ const lastResult = /* @__PURE__ */ new WeakMap();
670
+ const fallbackReceiver = {};
671
+ const evaluate = (receiver) => {
672
+ const args = this.deps(internal.module);
673
+ if (!lastArgs.has(receiver) || !areShallowEqualWithArray(lastArgs.get(receiver), args)) lastResult.set(receiver, this.fn.apply(receiver, args));
674
+ lastArgs.set(receiver, args);
675
+ return lastResult.get(receiver);
676
+ };
677
+ return function() {
678
+ const receiver = typeof this === "object" && this !== null ? this : fallbackReceiver;
679
+ if (internal.isBatching) return evaluate(receiver);
680
+ let accessor = memoByReceiver.get(receiver);
681
+ if (!accessor) {
682
+ accessor = computed$1(() => evaluate(receiver));
683
+ memoByReceiver.set(receiver, accessor);
684
+ }
685
+ return accessor();
686
+ };
687
+ }
688
+ };
689
+ const createCachedGetter = (internal, getter) => {
690
+ const accessors = /* @__PURE__ */ new WeakMap();
691
+ const fallbackReceiver = {};
692
+ return function() {
693
+ const receiver = typeof this === "object" && this !== null ? this : fallbackReceiver;
694
+ if (internal.isBatching) return getter.call(receiver);
695
+ let accessor = accessors.get(receiver);
696
+ if (!accessor) {
697
+ accessor = computed$1(() => getter.call(receiver));
698
+ accessors.set(receiver, accessor);
699
+ }
700
+ return accessor();
701
+ };
702
+ };
703
+ const createTrackedStateReader = (internal, read, initialValue) => {
704
+ const slotSignal = signal$1(initialValue);
705
+ const slotVersionSignal = signal$1(0);
706
+ let slotVersion = 0;
707
+ (internal.signalSlots ??= /* @__PURE__ */ new Set()).add({ refresh: () => {
708
+ const nextValue = read();
709
+ slotSignal(nextValue);
710
+ if (internal.mutableInstance && isObjectLike(nextValue)) {
711
+ slotVersion += 1;
712
+ slotVersionSignal(slotVersion);
713
+ }
714
+ } });
715
+ return () => {
716
+ const currentValue = slotSignal();
717
+ if (internal.mutableInstance && isObjectLike(currentValue)) slotVersionSignal();
718
+ return read();
719
+ };
720
+ };
721
+ const refreshSignalSlots = (internal) => {
722
+ if (!internal.signalSlots?.size) return;
723
+ startBatch$1();
724
+ try {
725
+ internal.signalSlots.forEach((slot) => slot.refresh());
726
+ } finally {
727
+ endBatch$1();
728
+ }
507
729
  };
508
- var defaultMemoize = (func) => {
509
- const lastArgs = /* @__PURE__ */ new WeakMap();
510
- const lastResult = /* @__PURE__ */ new WeakMap();
511
- return function() {
512
- if (!areShallowEqualWithArray(lastArgs.get(this) ?? [], arguments)) {
513
- lastResult.set(this, func.apply(this, arguments));
514
- }
515
- lastArgs.set(this, arguments);
516
- return lastResult.get(this);
517
- };
730
+ //#endregion
731
+ //#region packages/core/src/getRawStateStateProperty.ts
732
+ const prepareStateDescriptor = ({ descriptor, initialStateSeen, internal, key, rawState, sliceKey }) => {
733
+ const isComputed = descriptor.value instanceof Computed;
734
+ const readStateValue = () => typeof sliceKey !== "undefined" ? internal.rootState[sliceKey][key] : internal.rootState[key];
735
+ const initialValue = isComputed ? descriptor.value : sanitizeInitialStateValue(descriptor.value, initialStateSeen);
736
+ if (internal.mutableInstance) Object.defineProperty(rawState, key, {
737
+ get: () => internal.mutableInstance[key],
738
+ set: (value) => {
739
+ internal.mutableInstance[key] = value;
740
+ },
741
+ configurable: true,
742
+ enumerable: descriptor.enumerable
743
+ });
744
+ else if (!isComputed) Object.defineProperty(rawState, key, {
745
+ value: initialValue,
746
+ configurable: true,
747
+ enumerable: descriptor.enumerable,
748
+ writable: true
749
+ });
750
+ if (isComputed) {
751
+ if (internal.mutableInstance) throw new Error("Computed is not supported with mutable instance");
752
+ descriptor.get = descriptor.value.createGetter({ internal });
753
+ } else if (typeof sliceKey !== "undefined") {
754
+ const read = createTrackedStateReader(internal, readStateValue, initialValue);
755
+ descriptor.get = () => read();
756
+ descriptor.set = (value) => {
757
+ internal.rootState[sliceKey][key] = value;
758
+ };
759
+ } else {
760
+ const read = createTrackedStateReader(internal, readStateValue, initialValue);
761
+ descriptor.get = () => read();
762
+ descriptor.set = (value) => {
763
+ internal.rootState[key] = value;
764
+ };
765
+ }
766
+ delete descriptor.value;
767
+ delete descriptor.writable;
518
768
  };
519
- var createSelectorCreatorWithArray = (memoize = defaultMemoize) => {
520
- return (dependenciesFunc, resultFunc) => {
521
- const memoizedResultFunc = memoize(function() {
522
- return resultFunc.apply(this, arguments);
523
- });
524
- return function() {
525
- return memoizedResultFunc.apply(
526
- this,
527
- dependenciesFunc.apply(null, [this])
528
- );
529
- };
530
- };
769
+ const prepareAccessorDescriptor = ({ descriptor, internal }) => {
770
+ if (internal.mutableInstance || typeof descriptor.get !== "function") return;
771
+ descriptor.get = createCachedGetter(internal, descriptor.get);
531
772
  };
532
- var createSelectorWithArray = createSelectorCreatorWithArray();
533
-
534
- // src/getRawStateStateProperty.ts
535
- var prepareStateDescriptor = ({
536
- descriptor,
537
- internal,
538
- key,
539
- rawState,
540
- sliceKey
541
- }) => {
542
- const isComputed = descriptor.value instanceof Computed;
543
- if (internal.mutableInstance) {
544
- Object.defineProperty(rawState, key, {
545
- get: () => internal.mutableInstance[key],
546
- set: (value) => {
547
- internal.mutableInstance[key] = value;
548
- },
549
- enumerable: true
550
- });
551
- } else if (!isComputed) {
552
- rawState[key] = descriptor.value;
553
- }
554
- if (isComputed) {
555
- if (internal.mutableInstance) {
556
- throw new Error("Computed is not supported with mutable instance");
557
- }
558
- const { deps, fn } = descriptor.value;
559
- const depsCallbackSelector = createSelectorWithArray(
560
- // the root state should be updated, and the computed property will be updated.
561
- () => [internal.rootState],
562
- () => {
563
- return deps(internal.module);
564
- }
565
- );
566
- const selector = createSelectorWithArray(
567
- (that) => depsCallbackSelector.call(that),
568
- fn
569
- );
570
- descriptor.get = function() {
571
- return selector.call(this);
572
- };
573
- } else if (sliceKey) {
574
- descriptor.get = () => internal.rootState[sliceKey][key];
575
- descriptor.set = (value) => {
576
- internal.rootState[sliceKey][key] = value;
577
- };
578
- } else {
579
- descriptor.get = () => internal.rootState[key];
580
- descriptor.set = (value) => {
581
- internal.rootState[key] = value;
582
- };
583
- }
584
- delete descriptor.value;
585
- delete descriptor.writable;
773
+ //#endregion
774
+ //#region packages/core/src/getRawState.ts
775
+ const defaultClientExecuteSyncTimeoutMs = 1500;
776
+ const getClientExecuteSyncTimeoutMs = (options) => {
777
+ const timeout = options.executeSyncTimeoutMs;
778
+ if (typeof timeout === "undefined") return defaultClientExecuteSyncTimeoutMs;
779
+ if (!Number.isFinite(timeout) || timeout < 0) throw new Error("executeSyncTimeoutMs must be a finite number greater than or equal to 0");
780
+ return timeout;
586
781
  };
587
-
588
- // src/getRawState.ts
589
- var defaultClientExecuteSyncTimeoutMs = 1500;
590
- var getClientExecuteSyncTimeoutMs = (options) => {
591
- const timeout = options.executeSyncTimeoutMs;
592
- if (typeof timeout === "undefined") {
593
- return defaultClientExecuteSyncTimeoutMs;
594
- }
595
- if (!Number.isFinite(timeout) || timeout < 0) {
596
- throw new Error(
597
- "executeSyncTimeoutMs must be a finite number greater than or equal to 0"
598
- );
599
- }
600
- return timeout;
782
+ const getRawState = (store, internal, initialState, options) => {
783
+ const clientExecuteSyncTimeoutMs = getClientExecuteSyncTimeoutMs(options);
784
+ const rawState = {};
785
+ const handle = (_rawState, _initialState, sliceKey) => {
786
+ internal.mutableInstance = internal.toMutableRaw?.(_initialState);
787
+ const initialStateSeen = /* @__PURE__ */ new WeakMap();
788
+ initialStateSeen.set(_initialState, _rawState);
789
+ const safeDescriptors = {};
790
+ const descriptors = Object.getOwnPropertyDescriptors(_initialState);
791
+ Reflect.ownKeys(descriptors).forEach((key) => {
792
+ if (typeof key === "string" && isUnsafeKey(key)) return;
793
+ safeDescriptors[key] = Reflect.get(descriptors, key);
794
+ });
795
+ Reflect.ownKeys(safeDescriptors).forEach((key) => {
796
+ const descriptor = safeDescriptors[key];
797
+ if (typeof descriptor === "undefined") return;
798
+ if (!Object.prototype.hasOwnProperty.call(descriptor, "value")) {
799
+ prepareAccessorDescriptor({
800
+ descriptor,
801
+ internal
802
+ });
803
+ return;
804
+ }
805
+ if (Object.prototype.hasOwnProperty.call(descriptor, "value")) {
806
+ if (typeof descriptor.value !== "function") {
807
+ prepareStateDescriptor({
808
+ descriptor,
809
+ initialStateSeen,
810
+ internal,
811
+ key,
812
+ rawState: _rawState,
813
+ sliceKey
814
+ });
815
+ return;
816
+ }
817
+ if (store.share === "client") {
818
+ if (typeof key !== "string") return;
819
+ descriptor.value = createClientAction({
820
+ clientExecuteSyncTimeoutMs,
821
+ internal,
822
+ key,
823
+ store,
824
+ sliceKey
825
+ });
826
+ } else descriptor.value = createLocalAction({
827
+ fn: descriptor.value,
828
+ internal,
829
+ key,
830
+ options,
831
+ store,
832
+ sliceKey
833
+ });
834
+ }
835
+ });
836
+ return Object.defineProperties({}, safeDescriptors);
837
+ };
838
+ if (store.isSliceStore) {
839
+ internal.module = {};
840
+ getOwnEnumerableKeys(initialState).forEach((key) => {
841
+ if (typeof key === "string" && isUnsafeKey(key)) return;
842
+ const sliceRawState = {};
843
+ setOwnEnumerable(rawState, key, sliceRawState);
844
+ setOwnEnumerable(internal.module, key, handle(sliceRawState, initialState[key], key));
845
+ });
846
+ } else internal.module = handle(rawState, initialState);
847
+ return rawState;
601
848
  };
602
- var getRawState = (store, internal, initialState, options) => {
603
- const clientExecuteSyncTimeoutMs = getClientExecuteSyncTimeoutMs(options);
604
- const rawState = {};
605
- const handle = (_rawState, _initialState, sliceKey) => {
606
- internal.mutableInstance = internal.toMutableRaw?.(_initialState);
607
- const descriptors = Object.getOwnPropertyDescriptors(_initialState);
608
- Object.entries(descriptors).forEach(([key, descriptor]) => {
609
- if (Object.prototype.hasOwnProperty.call(descriptor, "value")) {
610
- if (typeof descriptor.value !== "function") {
611
- prepareStateDescriptor({
612
- descriptor,
613
- internal,
614
- key,
615
- rawState: _rawState,
616
- sliceKey
617
- });
618
- } else if (store.share === "client") {
619
- descriptor.value = createClientAction({
620
- clientExecuteSyncTimeoutMs,
621
- internal,
622
- key,
623
- store,
624
- sliceKey
625
- });
626
- } else {
627
- descriptor.value = createLocalAction({
628
- fn: descriptor.value,
629
- internal,
630
- key,
631
- options,
632
- store,
633
- sliceKey
634
- });
635
- }
636
- }
637
- });
638
- const slice = Object.defineProperties({}, descriptors);
639
- return slice;
640
- };
641
- if (store.isSliceStore) {
642
- internal.module = {};
643
- Object.entries(initialState).forEach(([key, value]) => {
644
- rawState[key] = {};
645
- internal.module[key] = handle(rawState[key], value, key);
646
- });
647
- } else {
648
- internal.module = handle(rawState, initialState);
649
- }
650
- return rawState;
849
+ //#endregion
850
+ //#region packages/core/src/handleState.ts
851
+ const handleState = (store, internal, options) => {
852
+ const setState = (next, updater = (next) => {
853
+ const merge = (_next = next) => {
854
+ mergeObject(internal.rootState, _next, store.isSliceStore);
855
+ };
856
+ const fn = typeof next === "function" ? () => {
857
+ const returnValue = next(internal.module);
858
+ if (returnValue instanceof Promise) throw new Error("setState with async function is not supported");
859
+ if (typeof returnValue === "object" && returnValue !== null) merge(returnValue);
860
+ } : merge;
861
+ if (!(store.transport ?? options.enablePatches) && internal.mutableInstance) {
862
+ if (internal.actMutable) {
863
+ internal.actMutable(() => {
864
+ fn.apply(null);
865
+ });
866
+ return [];
867
+ }
868
+ fn.apply(null);
869
+ return [];
870
+ }
871
+ internal.backupState = internal.rootState;
872
+ let patches;
873
+ let inversePatches;
874
+ try {
875
+ const result = create$1(internal.rootState, (draft) => {
876
+ internal.rootState = draft;
877
+ return fn.apply(null);
878
+ }, { enablePatches: true });
879
+ if (store.share === "main") validateSharedStateSerializable(result[0]);
880
+ patches = result[1];
881
+ inversePatches = result[2];
882
+ } finally {
883
+ internal.rootState = internal.backupState;
884
+ }
885
+ const finalPatches = store.patch ? store.patch({
886
+ patches,
887
+ inversePatches
888
+ }) : {
889
+ patches,
890
+ inversePatches
891
+ };
892
+ const safePatches = sanitizePatches(finalPatches.patches) ?? [];
893
+ const safeInversePatches = sanitizePatches(finalPatches.inversePatches) ?? [];
894
+ if (safePatches.length) store.apply(internal.rootState, safePatches);
895
+ return [
896
+ internal.rootState,
897
+ safePatches,
898
+ safeInversePatches
899
+ ];
900
+ }) => {
901
+ internal.assertMutationAllowed?.("setState");
902
+ if (store.share === "client") throw new Error(`setState() cannot be called in the client store. To update the state, please trigger a store method with setState() instead.`);
903
+ if (internal.isBatching) throw new Error("setState cannot be called within the updater");
904
+ if (next === null) return [];
905
+ internal.isBatching = true;
906
+ if (!store.share && !options.enablePatches && !internal.mutableInstance) try {
907
+ if (typeof next === "function") try {
908
+ internal.backupState = internal.rootState;
909
+ internal.rootState = create$1(internal.rootState, (draft) => {
910
+ internal.rootState = draft;
911
+ const returnValue = next(internal.module);
912
+ if (returnValue instanceof Promise) throw new Error("setState with async function is not supported");
913
+ if (typeof returnValue === "object" && returnValue !== null) mergeObject(internal.rootState, returnValue, store.isSliceStore);
914
+ });
915
+ } catch (error) {
916
+ internal.rootState = internal.backupState;
917
+ throw error;
918
+ }
919
+ else {
920
+ const copy = cloneOwnEnumerable(internal.rootState);
921
+ if (store.isSliceStore) {
922
+ const nextRecord = next;
923
+ const copyRecord = copy;
924
+ for (const key of getOwnEnumerableKeys(nextRecord)) {
925
+ if (!Object.prototype.hasOwnProperty.call(copyRecord, key)) continue;
926
+ const sourceValue = nextRecord[key];
927
+ if (typeof sourceValue !== "object" || sourceValue === null) continue;
928
+ const targetValue = copyRecord[key];
929
+ if (typeof targetValue !== "object" || targetValue === null) continue;
930
+ const sliceCopy = cloneOwnEnumerable(targetValue);
931
+ mergeObject(sliceCopy, sourceValue);
932
+ setOwnEnumerable(copyRecord, key, sliceCopy);
933
+ }
934
+ } else mergeObject(copy, next);
935
+ internal.rootState = copy;
936
+ }
937
+ refreshSignalSlots(internal);
938
+ if (internal.updateImmutable) internal.updateImmutable(internal.rootState);
939
+ else internal.listeners.forEach((listener) => listener());
940
+ return [];
941
+ } finally {
942
+ internal.isBatching = false;
943
+ }
944
+ let result;
945
+ try {
946
+ const isDrafted = internal.mutableInstance && isDraft(internal.rootState);
947
+ if (isDrafted) handleDraft(store, internal);
948
+ result = updater(next);
949
+ if (store.share === "main") validateSharedStateSerializable(internal.rootState);
950
+ if (isDrafted) {
951
+ internal.backupState = internal.rootState;
952
+ const [draft, finalize] = create$1(internal.rootState, { enablePatches: true });
953
+ internal.finalizeDraft = finalize;
954
+ internal.rootState = draft;
955
+ }
956
+ } finally {
957
+ internal.isBatching = false;
958
+ }
959
+ if (result?.length) result = [
960
+ result[0],
961
+ sanitizePatches(result[1]) ?? [],
962
+ sanitizePatches(result[2]) ?? []
963
+ ];
964
+ emit(store, internal, result?.[1]);
965
+ return result;
966
+ };
967
+ const getState = (deps, selector) => deps && selector ? new Computed(deps, selector) : internal.module;
968
+ return {
969
+ setState,
970
+ getState
971
+ };
651
972
  };
652
-
653
- // src/handleState.ts
654
- import {
655
- create as createWithMutative2,
656
- isDraft as isDraft2
657
- } from "mutative";
658
- var handleState = (store, internal, options) => {
659
- const setState = (next, updater = (next2) => {
660
- const merge = (_next = next2) => {
661
- mergeObject(internal.rootState, _next, store.isSliceStore);
662
- };
663
- const fn = typeof next2 === "function" ? () => {
664
- const returnValue = next2(internal.module);
665
- if (returnValue instanceof Promise) {
666
- throw new Error(
667
- "setState with async function is not supported"
668
- );
669
- }
670
- if (typeof returnValue === "object" && returnValue !== null) {
671
- merge(returnValue);
672
- }
673
- } : merge;
674
- const enablePatches = store.transport ?? options.enablePatches;
675
- if (!enablePatches && internal.mutableInstance) {
676
- if (internal.actMutable) {
677
- internal.actMutable(() => {
678
- fn.apply(null);
679
- });
680
- return [];
681
- }
682
- fn.apply(null);
683
- return [];
684
- }
685
- internal.backupState = internal.rootState;
686
- let patches;
687
- let inversePatches;
688
- try {
689
- const result = createWithMutative2(
690
- internal.rootState,
691
- (draft) => {
692
- internal.rootState = draft;
693
- return fn.apply(null);
694
- },
695
- {
696
- enablePatches: true
697
- }
698
- );
699
- patches = result[1];
700
- inversePatches = result[2];
701
- } finally {
702
- internal.rootState = internal.backupState;
703
- }
704
- const finalPatches = store.patch ? store.patch({ patches, inversePatches }) : { patches, inversePatches };
705
- if (finalPatches.patches.length) {
706
- store.apply(internal.rootState, finalPatches.patches);
707
- if (!internal.mutableInstance) {
708
- if (internal.updateImmutable) {
709
- internal.updateImmutable(internal.rootState);
710
- } else {
711
- internal.listeners.forEach((listener) => listener());
712
- }
713
- }
714
- }
715
- return [
716
- internal.rootState,
717
- finalPatches.patches,
718
- finalPatches.inversePatches
719
- ];
720
- }) => {
721
- if (store.share === "client") {
722
- throw new Error(
723
- `setState() cannot be called in the client store. To update the state, please trigger a store method with setState() instead.`
724
- );
725
- }
726
- if (internal.isBatching) {
727
- throw new Error("setState cannot be called within the updater");
728
- }
729
- internal.isBatching = true;
730
- if (!store.share && !options.enablePatches && !internal.mutableInstance) {
731
- if (typeof next === "function") {
732
- try {
733
- internal.backupState = internal.rootState;
734
- internal.rootState = createWithMutative2(
735
- internal.rootState,
736
- (draft) => {
737
- internal.rootState = draft;
738
- return next(internal.module);
739
- }
740
- );
741
- } catch (error) {
742
- internal.rootState = internal.backupState;
743
- internal.isBatching = false;
744
- throw error;
745
- }
746
- } else {
747
- const copy = {};
748
- const rootState = internal.rootState;
749
- for (const key of Object.keys(rootState)) {
750
- copy[key] = rootState[key];
751
- }
752
- for (const key of Object.keys(next)) {
753
- copy[key] = next[key];
754
- }
755
- internal.rootState = copy;
756
- }
757
- if (internal.updateImmutable) {
758
- internal.updateImmutable(internal.rootState);
759
- } else {
760
- internal.listeners.forEach((listener) => listener());
761
- }
762
- internal.isBatching = false;
763
- return [];
764
- }
765
- let result;
766
- try {
767
- const isDrafted = internal.mutableInstance && isDraft2(internal.rootState);
768
- if (isDrafted) {
769
- handleDraft(store, internal);
770
- }
771
- result = updater(next);
772
- if (isDrafted) {
773
- internal.backupState = internal.rootState;
774
- const [draft, finalize] = createWithMutative2(
775
- internal.rootState,
776
- {
777
- enablePatches: true
778
- }
779
- );
780
- internal.finalizeDraft = finalize;
781
- internal.rootState = draft;
782
- }
783
- } finally {
784
- internal.isBatching = false;
785
- }
786
- emit(store, internal, result?.[1]);
787
- return result;
788
- };
789
- const getState = (deps, selector) => deps && selector ? new Computed(deps, selector) : internal.module;
790
- return { setState, getState };
973
+ //#endregion
974
+ //#region packages/core/src/applyMiddlewares.ts
975
+ const isStoreLike = (value) => {
976
+ if (!value || typeof value !== "object") return false;
977
+ const candidate = value;
978
+ return typeof candidate.setState === "function" && typeof candidate.getState === "function" && typeof candidate.subscribe === "function" && typeof candidate.destroy === "function" && typeof candidate.apply === "function" && typeof candidate.getPureState === "function";
791
979
  };
792
-
793
- // src/applyMiddlewares.ts
794
- var isStoreLike = (value) => {
795
- if (!value || typeof value !== "object") {
796
- return false;
797
- }
798
- const candidate = value;
799
- return typeof candidate.setState === "function" && typeof candidate.getState === "function" && typeof candidate.subscribe === "function" && typeof candidate.destroy === "function" && typeof candidate.apply === "function" && typeof candidate.getPureState === "function";
980
+ const applyMiddlewares = (store, middlewares) => {
981
+ return middlewares.reduce((store, middleware, index) => {
982
+ if (process.env.NODE_ENV === "development") {
983
+ if (typeof middleware !== "function") throw new Error(`middlewares[${index}] should be a function`);
984
+ }
985
+ const nextStore = middleware(store);
986
+ if (process.env.NODE_ENV === "development") {
987
+ if (!isStoreLike(nextStore)) throw new Error(`middlewares[${index}] should return a store-like object`);
988
+ }
989
+ return nextStore;
990
+ }, store);
800
991
  };
801
- var applyMiddlewares = (store, middlewares) => {
802
- return middlewares.reduce((store2, middleware, index) => {
803
- if (process.env.NODE_ENV === "development") {
804
- if (typeof middleware !== "function") {
805
- throw new Error(`middlewares[${index}] should be a function`);
806
- }
807
- }
808
- const nextStore = middleware(store2);
809
- if (process.env.NODE_ENV === "development") {
810
- if (!isStoreLike(nextStore)) {
811
- throw new Error(
812
- `middlewares[${index}] should return a store-like object`
813
- );
814
- }
815
- }
816
- return nextStore;
817
- }, store);
992
+ //#endregion
993
+ //#region packages/core/src/handleMainTransport.ts
994
+ const getErrorMessage = (error) => {
995
+ if (error instanceof Error) return error.message;
996
+ return String(error);
818
997
  };
819
-
820
- // src/handleMainTransport.ts
821
- import { createTransport as createTransport2 } from "data-transport";
822
- var getErrorMessage = (error) => {
823
- if (error instanceof Error) {
824
- return error.message;
825
- }
826
- return String(error);
998
+ const transportErrorMarker = "__coactionTransportError__";
999
+ const handleMainTransport = (store, internal, storeTransport, workerType, checkEnablePatches) => {
1000
+ const transport = storeTransport ?? (workerType === "SharedWorkerInternal" || workerType === "WebWorkerInternal" ? createTransport(workerType, { prefix: store.name }) : void 0);
1001
+ if (!transport) return;
1002
+ if (typeof transport.onConnect !== "function") throw new Error("transport.onConnect is required");
1003
+ if (checkEnablePatches) throw new Error(`enablePatches: true is required for the transport`);
1004
+ transport.listen("execute", async (keys, args) => {
1005
+ let base = store.getState();
1006
+ try {
1007
+ for (const key of keys) {
1008
+ if (isUnsafePathSegment(key) || typeof base !== "object" && typeof base !== "function" || base === null || !Object.prototype.hasOwnProperty.call(base, key)) throw new Error("The function is not found");
1009
+ const obj = base;
1010
+ base = base[key];
1011
+ if (typeof base === "function") base = base.bind(obj);
1012
+ }
1013
+ if (typeof base !== "function") throw new Error("The function is not found");
1014
+ return [await base(...args), internal.sequence];
1015
+ } catch (error) {
1016
+ if (process.env.NODE_ENV === "development") console.error(error);
1017
+ return [{
1018
+ [transportErrorMarker]: true,
1019
+ message: getErrorMessage(error)
1020
+ }, internal.sequence];
1021
+ }
1022
+ });
1023
+ transport.listen("fullSync", async () => {
1024
+ validateSharedStateSerializable(internal.rootState);
1025
+ return {
1026
+ state: JSON.stringify(internal.rootState),
1027
+ sequence: internal.sequence
1028
+ };
1029
+ });
1030
+ store.transport = transport;
827
1031
  };
828
- var transportErrorMarker2 = "__coactionTransportError__";
829
- var handleMainTransport = (store, internal, storeTransport, workerType, checkEnablePatches) => {
830
- const transport = storeTransport ?? (workerType === "SharedWorkerInternal" || workerType === "WebWorkerInternal" ? createTransport2(workerType, {
831
- prefix: store.name
832
- }) : void 0);
833
- if (!transport) return;
834
- if (typeof transport.onConnect !== "function") {
835
- throw new Error("transport.onConnect is required");
836
- }
837
- if (checkEnablePatches) {
838
- throw new Error(`enablePatches: true is required for the transport`);
839
- }
840
- transport.listen("execute", async (keys, args) => {
841
- let base = store.getState();
842
- let obj = base;
843
- try {
844
- for (const key of keys) {
845
- base = base[key];
846
- if (typeof base === "function") {
847
- base = base.bind(obj);
848
- }
849
- obj = base;
850
- }
851
- if (typeof base !== "function") {
852
- throw new Error("The function is not found");
853
- }
854
- const result = await base(...args);
855
- return [result, internal.sequence];
856
- } catch (error) {
857
- if (process.env.NODE_ENV === "development") {
858
- console.error(error);
859
- }
860
- return [
861
- {
862
- [transportErrorMarker2]: true,
863
- message: getErrorMessage(error)
864
- },
865
- internal.sequence
866
- ];
867
- }
868
- });
869
- transport.listen("fullSync", async () => {
870
- return {
871
- state: JSON.stringify(internal.rootState),
872
- sequence: internal.sequence
873
- };
874
- });
875
- store.transport = transport;
1032
+ //#endregion
1033
+ //#region packages/core/src/lifecycle.ts
1034
+ const readyStores = /* @__PURE__ */ new WeakSet();
1035
+ const readyCallbacks = /* @__PURE__ */ new WeakMap();
1036
+ const onStoreReady = (store, callback) => {
1037
+ if (readyStores.has(store)) {
1038
+ callback();
1039
+ return () => void 0;
1040
+ }
1041
+ let callbacks = readyCallbacks.get(store);
1042
+ if (!callbacks) {
1043
+ callbacks = /* @__PURE__ */ new Set();
1044
+ readyCallbacks.set(store, callbacks);
1045
+ }
1046
+ callbacks.add(callback);
1047
+ return () => {
1048
+ callbacks?.delete(callback);
1049
+ };
876
1050
  };
877
-
878
- // src/create.ts
879
- var namespaceMap = /* @__PURE__ */ new Map();
880
- var hasWarnedAmbiguousFunctionMap = false;
881
- var warnAmbiguousFunctionMap = () => {
882
- if (hasWarnedAmbiguousFunctionMap || process.env.NODE_ENV === "production" || process.env.NODE_ENV === "test") {
883
- return;
884
- }
885
- hasWarnedAmbiguousFunctionMap = true;
886
- console.warn(
887
- [
888
- `sliceMode: 'auto' inferred slices from an object of functions.`,
889
- `This shape is ambiguous with a single store that only contains methods.`,
890
- `Use create({ ping() {} }, { sliceMode: 'single' }) for a plain method store,`,
891
- `or create({ counter: (set) => ({ count: 0 }) }, { sliceMode: 'slices' }) for slices.`
892
- ].join(" ")
893
- );
1051
+ const markStoreReady = (store) => {
1052
+ readyStores.add(store);
1053
+ const callbacks = readyCallbacks.get(store);
1054
+ if (!callbacks) return;
1055
+ readyCallbacks.delete(store);
1056
+ callbacks.forEach((callback) => callback());
1057
+ callbacks.clear();
894
1058
  };
895
- var create = (createState, options = {}) => {
896
- const checkEnablePatches = Object.hasOwnProperty.call(options, "enablePatches") && !options.enablePatches;
897
- const workerType = options.workerType ?? WorkerType;
898
- if (process.env.NODE_ENV === "development" && options.transport && options.clientTransport) {
899
- throw new Error(
900
- `transport and clientTransport cannot be used together, please use one of them.`
901
- );
902
- }
903
- const storeTransport = options.transport;
904
- const share = workerType === "WebWorkerInternal" || workerType === "SharedWorkerInternal" || storeTransport ? "main" : void 0;
905
- const createStore = ({ share: share2 }) => {
906
- const store2 = {};
907
- const internal2 = {
908
- sequence: 0,
909
- isBatching: false,
910
- listeners: /* @__PURE__ */ new Set()
911
- };
912
- const name = options.name ?? defaultName;
913
- const shouldTrackName = share2 === "main" && process.env.NODE_ENV !== "test";
914
- const releaseStoreName = () => {
915
- if (shouldTrackName) {
916
- namespaceMap.delete(name);
917
- }
918
- };
919
- if (shouldTrackName) {
920
- if (namespaceMap.get(name)) {
921
- throw new Error(`Store name '${name}' is not unique.`);
922
- }
923
- namespaceMap.set(name, true);
924
- }
925
- try {
926
- const { setState, getState } = handleState(store2, internal2, options);
927
- const subscribe = (listener) => {
928
- internal2.listeners.add(listener);
929
- return () => internal2.listeners.delete(listener);
930
- };
931
- let isDestroyed = false;
932
- const destroy = () => {
933
- if (isDestroyed) {
934
- return;
935
- }
936
- isDestroyed = true;
937
- internal2.listeners.clear();
938
- store2.transport?.dispose();
939
- releaseStoreName();
940
- };
941
- const apply = (state = internal2.rootState, patches) => {
942
- internal2.rootState = patches ? applyWithMutative(state, patches) : state;
943
- if (internal2.updateImmutable) {
944
- internal2.updateImmutable(internal2.rootState);
945
- } else {
946
- internal2.listeners.forEach((listener) => listener());
947
- }
948
- };
949
- const getPureState = () => internal2.rootState;
950
- const isFunctionMapObject = () => {
951
- if (typeof createState === "object" && createState !== null) {
952
- const values = Object.values(createState);
953
- return values.length > 0 && values.every((value) => typeof value === "function");
954
- }
955
- return false;
956
- };
957
- const getIsSliceStore = () => {
958
- const sliceMode = options.sliceMode ?? "auto";
959
- if (sliceMode === "single") {
960
- return false;
961
- }
962
- if (sliceMode === "slices") {
963
- if (!isFunctionMapObject()) {
964
- throw new Error(
965
- `sliceMode: 'slices' requires createState to be an object of slice functions.`
966
- );
967
- }
968
- return true;
969
- }
970
- if (isFunctionMapObject()) {
971
- warnAmbiguousFunctionMap();
972
- return true;
973
- }
974
- return false;
975
- };
976
- const isSliceStore = getIsSliceStore();
977
- Object.assign(store2, {
978
- name,
979
- share: share2 ?? false,
980
- setState,
981
- getState,
982
- subscribe,
983
- destroy,
984
- apply,
985
- isSliceStore,
986
- getPureState
987
- });
988
- const middlewareStore = applyMiddlewares(
989
- store2,
990
- options.middlewares ?? []
991
- );
992
- if (middlewareStore !== store2) {
993
- Object.assign(store2, middlewareStore);
994
- }
995
- const initialState = getInitialState(store2, createState, internal2);
996
- store2.getInitialState = () => initialState;
997
- internal2.rootState = getRawState(
998
- store2,
999
- internal2,
1000
- initialState,
1001
- options
1002
- );
1003
- return { store: store2, internal: internal2 };
1004
- } catch (error) {
1005
- releaseStoreName();
1006
- throw error;
1007
- }
1008
- };
1009
- if (options.clientTransport || options.worker || options.workerType === "WebWorkerClient" || options.workerType === "SharedWorkerClient") {
1010
- if (checkEnablePatches) {
1011
- throw new Error(`enablePatches: true is required for the async store`);
1012
- }
1013
- const store2 = createAsyncClientStore(
1014
- createStore,
1015
- options
1016
- );
1017
- return wrapStore(store2);
1018
- }
1019
- const { store, internal } = createStore({
1020
- share
1021
- });
1022
- handleMainTransport(
1023
- store,
1024
- internal,
1025
- storeTransport,
1026
- workerType,
1027
- checkEnablePatches
1028
- );
1029
- return wrapStore(store);
1059
+ //#endregion
1060
+ //#region packages/core/src/create.ts
1061
+ const namespaceMap = /* @__PURE__ */ new Map();
1062
+ let hasWarnedAmbiguousFunctionMap = false;
1063
+ const isMainWorkerType = (workerType) => workerType === "SharedWorkerInternal" || workerType === "WebWorkerInternal";
1064
+ const isClientWorkerType = (workerType) => workerType === "SharedWorkerClient" || workerType === "WebWorkerClient";
1065
+ const validateCreateModeOptions = (options) => {
1066
+ const storeTransport = options.transport;
1067
+ const clientTransport = options.clientTransport;
1068
+ const worker = options.worker;
1069
+ const explicitWorkerType = options.workerType;
1070
+ if (storeTransport && clientTransport) throw new Error("transport and clientTransport cannot be used together, please use one authority model per store.");
1071
+ if (storeTransport && worker) throw new Error("transport and worker cannot be used together, please use one authority model per store.");
1072
+ if (clientTransport && worker) throw new Error("clientTransport and worker cannot be used together, please use one client transport source.");
1073
+ if (isMainWorkerType(explicitWorkerType) && (clientTransport || worker)) throw new Error("main workerType cannot be combined with client transport settings.");
1074
+ if (isClientWorkerType(explicitWorkerType) && storeTransport) throw new Error("client workerType cannot be combined with transport.");
1030
1075
  };
1031
-
1032
- // src/binder.ts
1033
- function createBinder({
1034
- handleState: handleState2,
1035
- handleStore
1036
- }) {
1037
- return ((state) => {
1038
- const { copyState, key, bind } = handleState2(state);
1039
- const value = key ? copyState[key] : copyState;
1040
- value[bindSymbol] = {
1041
- handleStore,
1042
- bind
1043
- };
1044
- return copyState;
1045
- });
1076
+ const warnAmbiguousFunctionMap = () => {
1077
+ if (hasWarnedAmbiguousFunctionMap || process.env.NODE_ENV === "production" || process.env.NODE_ENV === "test") return;
1078
+ hasWarnedAmbiguousFunctionMap = true;
1079
+ console.warn([
1080
+ `sliceMode: 'auto' inferred slices from an object of functions.`,
1081
+ `This shape is ambiguous with a single store that only contains methods.`,
1082
+ `Use create({ ping() {} }, { sliceMode: 'single' }) for a plain method store,`,
1083
+ `or create({ counter: (set) => ({ count: 0 }) }, { sliceMode: 'slices' }) for slices.`
1084
+ ].join(" "));
1085
+ };
1086
+ /**
1087
+ * Create a local store, the main side of a shared store, or a client mirror of
1088
+ * a shared store.
1089
+ *
1090
+ * @remarks
1091
+ * - Pass a {@link Slice} function for a single store.
1092
+ * - Pass an object of slice factories for a slices store.
1093
+ * - When an object input only contains functions, prefer explicit `sliceMode`
1094
+ * to avoid ambiguous inference.
1095
+ * - When `clientTransport` or `worker` is provided, returned store methods
1096
+ * become promise-returning methods because execution happens on the main
1097
+ * shared store.
1098
+ * - New semantics should prefer explicit helpers or variants over adding more
1099
+ * ambiguous `create()` input forms.
1100
+ */
1101
+ const create = (createState, options = {}) => {
1102
+ const checkEnablePatches = Object.hasOwnProperty.call(options, "enablePatches") && !options.enablePatches;
1103
+ validateCreateModeOptions(options);
1104
+ const workerType = options.workerType ?? WorkerType;
1105
+ const storeTransport = options.transport;
1106
+ const share = workerType === "WebWorkerInternal" || workerType === "SharedWorkerInternal" || storeTransport ? "main" : void 0;
1107
+ const createStore = ({ share }) => {
1108
+ const store = {};
1109
+ const internal = {
1110
+ sequence: 0,
1111
+ isBatching: false,
1112
+ listeners: /* @__PURE__ */ new Set()
1113
+ };
1114
+ internal.notifyStateChange = () => {
1115
+ refreshSignalSlots(internal);
1116
+ internal.listeners.forEach((listener) => listener());
1117
+ };
1118
+ const name = options.name ?? "default";
1119
+ const shouldTrackName = share === "main" && process.env.NODE_ENV !== "test";
1120
+ const releaseStoreName = () => {
1121
+ if (shouldTrackName) namespaceMap.delete(name);
1122
+ };
1123
+ if (shouldTrackName) {
1124
+ if (namespaceMap.get(name)) throw new Error(`Store name '${name}' is not unique.`);
1125
+ namespaceMap.set(name, true);
1126
+ }
1127
+ try {
1128
+ const { setState, getState } = handleState(store, internal, options);
1129
+ const subscribe = (listener) => {
1130
+ internal.listeners.add(listener);
1131
+ return () => internal.listeners.delete(listener);
1132
+ };
1133
+ let isDestroyed = false;
1134
+ const destroy = () => {
1135
+ if (isDestroyed) return;
1136
+ isDestroyed = true;
1137
+ internal.listeners.clear();
1138
+ store.transport?.dispose();
1139
+ releaseStoreName();
1140
+ };
1141
+ const apply$1 = (state = internal.rootState, patches) => {
1142
+ internal.assertMutationAllowed?.("apply");
1143
+ const safePatches = sanitizePatches(patches);
1144
+ const nextState = sanitizeReplacementState(safePatches ? apply(state, safePatches) : state);
1145
+ if (store.share === "main") validateSharedStateSerializable(nextState);
1146
+ internal.rootState = nextState;
1147
+ refreshSignalSlots(internal);
1148
+ if (internal.updateImmutable) internal.updateImmutable(internal.rootState);
1149
+ else internal.listeners.forEach((listener) => listener());
1150
+ };
1151
+ const getPureState = () => internal.rootState;
1152
+ const isFunctionMapObject = () => {
1153
+ if (typeof createState === "object" && createState !== null) {
1154
+ const values = getOwnEnumerableKeys(createState).map((key) => createState[key]);
1155
+ return values.length > 0 && values.every((value) => typeof value === "function");
1156
+ }
1157
+ return false;
1158
+ };
1159
+ const getIsSliceStore = () => {
1160
+ const sliceMode = options.sliceMode ?? "auto";
1161
+ if (sliceMode === "single") return false;
1162
+ if (sliceMode === "slices") {
1163
+ if (!isFunctionMapObject()) throw new Error(`sliceMode: 'slices' requires createState to be an object of slice functions.`);
1164
+ return true;
1165
+ }
1166
+ if (isFunctionMapObject()) {
1167
+ warnAmbiguousFunctionMap();
1168
+ return true;
1169
+ }
1170
+ return false;
1171
+ };
1172
+ const isSliceStore = getIsSliceStore();
1173
+ Object.assign(store, {
1174
+ name,
1175
+ share: share ?? false,
1176
+ setState,
1177
+ getState,
1178
+ subscribe,
1179
+ destroy,
1180
+ apply: apply$1,
1181
+ isSliceStore,
1182
+ getPureState
1183
+ });
1184
+ const middlewareStore = applyMiddlewares(store, options.middlewares ?? []);
1185
+ if (middlewareStore !== store) Object.assign(store, middlewareStore);
1186
+ const initialState = getInitialState(store, createState, internal);
1187
+ if (share) validateSharedActionPaths(initialState);
1188
+ store.getInitialState = () => initialState;
1189
+ internal.rootState = getRawState(store, internal, initialState, options);
1190
+ if (share) validateSharedStateSerializable(internal.rootState);
1191
+ markStoreReady(store);
1192
+ return {
1193
+ store,
1194
+ internal
1195
+ };
1196
+ } catch (error) {
1197
+ releaseStoreName();
1198
+ throw error;
1199
+ }
1200
+ };
1201
+ if (options.clientTransport || options.worker || options.workerType === "WebWorkerClient" || options.workerType === "SharedWorkerClient") {
1202
+ if (checkEnablePatches) throw new Error(`enablePatches: true is required for the async store`);
1203
+ return wrapStore(createAsyncClientStore(createStore, options));
1204
+ }
1205
+ const { store, internal } = createStore({ share });
1206
+ handleMainTransport(store, internal, storeTransport, workerType, checkEnablePatches);
1207
+ return wrapStore(store);
1208
+ };
1209
+ //#endregion
1210
+ //#region packages/core/src/binder.ts
1211
+ const createExternalStoreAdapter = ({ handleState, handleStore }) => ((state) => {
1212
+ const { copyState, key, bind } = handleState(state);
1213
+ const value = typeof key !== "undefined" ? copyState[key] : copyState;
1214
+ Object.defineProperty(value, bindSymbol, {
1215
+ configurable: true,
1216
+ enumerable: typeof key !== "undefined",
1217
+ value: {
1218
+ handleStore,
1219
+ bind
1220
+ }
1221
+ });
1222
+ return copyState;
1223
+ });
1224
+ /**
1225
+ * Build an adapter helper for bridging an external store implementation into
1226
+ * Coaction.
1227
+ *
1228
+ * @remarks
1229
+ * Official bindings use this to integrate stores such as Redux, Jotai, Pinia,
1230
+ * Zustand, MobX, and Valtio. Binder-backed integrations are whole-store
1231
+ * adapters; they are not compatible with Coaction slices mode.
1232
+ */
1233
+ function createBinder({ handleState, handleStore }) {
1234
+ return createExternalStoreAdapter({
1235
+ handleState,
1236
+ handleStore
1237
+ });
1046
1238
  }
1047
- export {
1048
- create,
1049
- createBinder,
1050
- wrapStore
1239
+ /**
1240
+ * Define a whole-store adapter for integrating an external state runtime with
1241
+ * Coaction.
1242
+ *
1243
+ * @remarks
1244
+ * This is the stable 2.x name for adapter authors. `createBinder()` remains as
1245
+ * a compatibility alias for existing official and community integrations.
1246
+ */
1247
+ function defineExternalStoreAdapter(options) {
1248
+ return createExternalStoreAdapter(options);
1249
+ }
1250
+ //#endregion
1251
+ //#region packages/core/src/reactiveTracker.ts
1252
+ const ReactiveFlags = alienSignalsSystem.ReactiveFlags;
1253
+ const unwatch = (node) => {
1254
+ if (!(node.flags & ReactiveFlags.Mutable)) {
1255
+ node.depsTail = void 0;
1256
+ node.flags = 0;
1257
+ purgeDeps(node);
1258
+ const sub = node.subs;
1259
+ if (sub !== void 0) unlink(sub);
1260
+ return;
1261
+ }
1262
+ if (node.depsTail !== void 0) {
1263
+ node.depsTail = void 0;
1264
+ node.flags = ReactiveFlags.Mutable | ReactiveFlags.Dirty;
1265
+ purgeDeps(node);
1266
+ }
1267
+ };
1268
+ const unlink = (link, sub = link.sub) => {
1269
+ const dep = link.dep;
1270
+ const prevDep = link.prevDep;
1271
+ const nextDep = link.nextDep;
1272
+ const nextSub = link.nextSub;
1273
+ const prevSub = link.prevSub;
1274
+ if (nextDep !== void 0) nextDep.prevDep = prevDep;
1275
+ else sub.depsTail = prevDep;
1276
+ if (prevDep !== void 0) prevDep.nextDep = nextDep;
1277
+ else sub.deps = nextDep;
1278
+ if (nextSub !== void 0) nextSub.prevSub = prevSub;
1279
+ else dep.subsTail = prevSub;
1280
+ if (prevSub !== void 0) prevSub.nextSub = nextSub;
1281
+ else if ((dep.subs = nextSub) === void 0) unwatch(dep);
1282
+ return nextDep;
1283
+ };
1284
+ const purgeDeps = (sub) => {
1285
+ const depsTail = sub.depsTail;
1286
+ let dep = depsTail !== void 0 ? depsTail.nextDep : sub.deps;
1287
+ while (dep !== void 0) dep = unlink(dep, sub);
1288
+ };
1289
+ const createReactiveTracker = () => {
1290
+ let version = 0;
1291
+ let disposed = false;
1292
+ const listeners = /* @__PURE__ */ new Set();
1293
+ const node = {
1294
+ deps: void 0,
1295
+ depsTail: void 0,
1296
+ subs: void 0,
1297
+ subsTail: void 0,
1298
+ flags: ReactiveFlags.Watching,
1299
+ fn: () => {
1300
+ if (disposed) return;
1301
+ version += 1;
1302
+ listeners.forEach((listener) => listener());
1303
+ }
1304
+ };
1305
+ const dispose = () => {
1306
+ if (disposed) return;
1307
+ disposed = true;
1308
+ listeners.clear();
1309
+ node.depsTail = void 0;
1310
+ purgeDeps(node);
1311
+ node.flags = 0;
1312
+ };
1313
+ return {
1314
+ getSnapshot: () => version,
1315
+ subscribe(listener) {
1316
+ if (disposed) return () => void 0;
1317
+ listeners.add(listener);
1318
+ return () => {
1319
+ listeners.delete(listener);
1320
+ };
1321
+ },
1322
+ track(fn) {
1323
+ if (disposed) return fn();
1324
+ node.depsTail = void 0;
1325
+ node.flags = ReactiveFlags.Watching | ReactiveFlags.RecursedCheck;
1326
+ const prevSub = setActiveSub(node);
1327
+ try {
1328
+ return fn();
1329
+ } finally {
1330
+ setActiveSub(prevSub);
1331
+ node.flags &= ~ReactiveFlags.RecursedCheck;
1332
+ purgeDeps(node);
1333
+ }
1334
+ },
1335
+ dispose
1336
+ };
1337
+ };
1338
+ //#endregion
1339
+ //#region packages/core/src/replaceExternalStoreState.ts
1340
+ const replaceExternalStoreState = (store, internal, source, { syncImmutable = true } = {}) => {
1341
+ const [, patches, inversePatches] = create$1(internal.rootState, (draft) => {
1342
+ replaceOwnEnumerable(draft, source);
1343
+ }, { enablePatches: true });
1344
+ const safePatches = sanitizePatches((store.patch ? store.patch({
1345
+ patches,
1346
+ inversePatches
1347
+ }) : {
1348
+ patches,
1349
+ inversePatches
1350
+ }).patches) ?? [];
1351
+ if (!safePatches.length) return;
1352
+ const updateImmutable = internal.updateImmutable;
1353
+ if (!syncImmutable) internal.updateImmutable = void 0;
1354
+ try {
1355
+ store.apply(internal.rootState, safePatches);
1356
+ } finally {
1357
+ internal.updateImmutable = updateImmutable;
1358
+ }
1359
+ emit(store, internal, safePatches);
1051
1360
  };
1361
+ //#endregion
1362
+ export { computed, create, createBinder, createReactiveTracker, defineExternalStoreAdapter, effect, effectScope, endBatch, isComputed, isEffect, isEffectScope, isSignal, onStoreReady, replaceExternalStoreState, replaceOwnEnumerable, sanitizeInitialStateValue, sanitizeReplacementState, signal, startBatch, trigger, wrapStore };