coaction 1.5.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -6
- package/dist/index.d.mts +384 -306
- package/dist/index.d.ts +384 -306
- package/dist/index.js +1420 -1147
- package/dist/index.mjs +1327 -1130
- package/package.json +25 -24
package/dist/index.mjs
CHANGED
|
@@ -1,1165 +1,1362 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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;
|
|
37
146
|
};
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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());
|
|
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);
|
|
138
158
|
};
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
},
|
|
147
|
-
{
|
|
148
|
-
patches,
|
|
149
|
-
sequence: internal.sequence
|
|
150
|
-
}
|
|
151
|
-
);
|
|
152
|
-
}
|
|
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
|
+
});
|
|
153
166
|
};
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
emit(store, internal, finalPatches.patches);
|
|
161
|
-
}
|
|
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;
|
|
162
173
|
};
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
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);
|
|
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;
|
|
220
178
|
};
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
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
|
+
}
|
|
228
196
|
};
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
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);
|
|
235
268
|
};
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
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
|
+
}
|
|
240
282
|
};
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
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)}.`);
|
|
245
287
|
};
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
if (!isEqual(prev[i], next[i])) {
|
|
253
|
-
return false;
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
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)}.`);
|
|
257
294
|
};
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
}
|
|
265
|
-
if (!Object.prototype.hasOwnProperty.call(target, key)) {
|
|
266
|
-
continue;
|
|
267
|
-
}
|
|
268
|
-
const sourceValue = source[key];
|
|
269
|
-
if (typeof sourceValue !== "object" || sourceValue === null) {
|
|
270
|
-
continue;
|
|
271
|
-
}
|
|
272
|
-
const targetValue = target[key];
|
|
273
|
-
if (typeof targetValue === "object" && targetValue !== null) {
|
|
274
|
-
assignOwnEnumerable(targetValue, sourceValue);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
} else {
|
|
279
|
-
if (typeof source === "object" && source !== null) {
|
|
280
|
-
assignOwnEnumerable(target, source);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
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);
|
|
283
301
|
};
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
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());
|
|
293
392
|
};
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
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
|
+
}
|
|
302
406
|
};
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
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
|
+
}
|
|
309
422
|
};
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
throw error;
|
|
349
|
-
}
|
|
350
|
-
);
|
|
351
|
-
}
|
|
352
|
-
done?.(result);
|
|
353
|
-
return result;
|
|
354
|
-
} catch (error) {
|
|
355
|
-
done?.(error);
|
|
356
|
-
throw error;
|
|
357
|
-
}
|
|
358
|
-
};
|
|
359
|
-
const keys = sliceKey ? [sliceKey, key] : [key];
|
|
360
|
-
return traceAction(
|
|
361
|
-
() => store.transport.emit("execute", keys, args).then(async (response) => {
|
|
362
|
-
const result = Array.isArray(response) ? response[0] : response;
|
|
363
|
-
const sequence = Array.isArray(response) ? typeof response[1] === "number" ? response[1] : internal.sequence : internal.sequence;
|
|
364
|
-
if (internal.sequence < sequence) {
|
|
365
|
-
if (process.env.NODE_ENV === "development") {
|
|
366
|
-
console.warn(
|
|
367
|
-
`The sequence of the action is not consistent.`,
|
|
368
|
-
sequence,
|
|
369
|
-
internal.sequence
|
|
370
|
-
);
|
|
371
|
-
}
|
|
372
|
-
await new Promise((resolve, reject) => {
|
|
373
|
-
let settled = false;
|
|
374
|
-
let unsubscribe = () => {
|
|
375
|
-
};
|
|
376
|
-
const timeoutRef = {};
|
|
377
|
-
const cleanup = () => {
|
|
378
|
-
unsubscribe();
|
|
379
|
-
if (typeof timeoutRef.current !== "undefined") {
|
|
380
|
-
clearTimeout(timeoutRef.current);
|
|
381
|
-
}
|
|
382
|
-
};
|
|
383
|
-
const finishResolve = () => {
|
|
384
|
-
if (settled) return;
|
|
385
|
-
settled = true;
|
|
386
|
-
cleanup();
|
|
387
|
-
resolve();
|
|
388
|
-
};
|
|
389
|
-
const finishReject = (error) => {
|
|
390
|
-
if (settled) return;
|
|
391
|
-
settled = true;
|
|
392
|
-
cleanup();
|
|
393
|
-
reject(error);
|
|
394
|
-
};
|
|
395
|
-
unsubscribe = store.subscribe(() => {
|
|
396
|
-
if (internal.sequence >= sequence) {
|
|
397
|
-
finishResolve();
|
|
398
|
-
}
|
|
399
|
-
});
|
|
400
|
-
timeoutRef.current = setTimeout(() => {
|
|
401
|
-
void store.transport.emit("fullSync").then((latest) => {
|
|
402
|
-
const next = latest;
|
|
403
|
-
if (typeof next.state !== "string" || typeof next.sequence !== "number") {
|
|
404
|
-
throw new Error("Invalid fullSync payload");
|
|
405
|
-
}
|
|
406
|
-
if (next.sequence >= sequence) {
|
|
407
|
-
store.apply(JSON.parse(next.state));
|
|
408
|
-
internal.sequence = next.sequence;
|
|
409
|
-
finishResolve();
|
|
410
|
-
return;
|
|
411
|
-
}
|
|
412
|
-
finishReject(
|
|
413
|
-
new Error(
|
|
414
|
-
`Stale fullSync sequence: expected >= ${sequence}, got ${next.sequence}`
|
|
415
|
-
)
|
|
416
|
-
);
|
|
417
|
-
}).catch((error) => {
|
|
418
|
-
finishReject(error);
|
|
419
|
-
});
|
|
420
|
-
}, clientExecuteSyncTimeoutMs);
|
|
421
|
-
});
|
|
422
|
-
}
|
|
423
|
-
if (isTransportErrorEnvelope(result)) {
|
|
424
|
-
throw new Error(result.message);
|
|
425
|
-
}
|
|
426
|
-
if (isLegacyTransportErrorEnvelope(result)) {
|
|
427
|
-
throw new Error(result.$$Error);
|
|
428
|
-
}
|
|
429
|
-
return result;
|
|
430
|
-
})
|
|
431
|
-
);
|
|
432
|
-
};
|
|
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
|
+
}, {});
|
|
433
461
|
};
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
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);
|
|
442
469
|
};
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
key,
|
|
447
|
-
options,
|
|
448
|
-
store,
|
|
449
|
-
sliceKey
|
|
450
|
-
}) => {
|
|
451
|
-
return (...args) => {
|
|
452
|
-
let actionId;
|
|
453
|
-
let done;
|
|
454
|
-
if (store.trace) {
|
|
455
|
-
actionId = uuid();
|
|
456
|
-
store.trace({
|
|
457
|
-
method: key,
|
|
458
|
-
parameters: args,
|
|
459
|
-
id: actionId,
|
|
460
|
-
sliceKey
|
|
461
|
-
});
|
|
462
|
-
done = (result) => {
|
|
463
|
-
store.trace({
|
|
464
|
-
method: key,
|
|
465
|
-
id: actionId,
|
|
466
|
-
result,
|
|
467
|
-
sliceKey
|
|
468
|
-
});
|
|
469
|
-
};
|
|
470
|
-
}
|
|
471
|
-
const traceAction = (run) => {
|
|
472
|
-
try {
|
|
473
|
-
const result = run();
|
|
474
|
-
if (result instanceof Promise) {
|
|
475
|
-
return result.then(
|
|
476
|
-
(value) => {
|
|
477
|
-
done?.(value);
|
|
478
|
-
return value;
|
|
479
|
-
},
|
|
480
|
-
(error) => {
|
|
481
|
-
done?.(error);
|
|
482
|
-
throw error;
|
|
483
|
-
}
|
|
484
|
-
);
|
|
485
|
-
}
|
|
486
|
-
done?.(result);
|
|
487
|
-
return result;
|
|
488
|
-
} catch (error) {
|
|
489
|
-
done?.(error);
|
|
490
|
-
throw error;
|
|
491
|
-
}
|
|
492
|
-
};
|
|
493
|
-
const enablePatches = store.transport ?? options.enablePatches;
|
|
494
|
-
return traceAction(() => {
|
|
495
|
-
if (internal.mutableInstance && !internal.isBatching && enablePatches) {
|
|
496
|
-
let result;
|
|
497
|
-
const handleResult = (isDrafted2) => {
|
|
498
|
-
handleDraft(store, internal);
|
|
499
|
-
if (isDrafted2) {
|
|
500
|
-
internal.backupState = internal.rootState;
|
|
501
|
-
const [draft2, finalize2] = createWithMutative(internal.rootState, {
|
|
502
|
-
enablePatches: true
|
|
503
|
-
});
|
|
504
|
-
internal.finalizeDraft = finalize2;
|
|
505
|
-
internal.rootState = draft2;
|
|
506
|
-
}
|
|
507
|
-
};
|
|
508
|
-
const isDrafted = isDraft(internal.rootState);
|
|
509
|
-
if (isDrafted) {
|
|
510
|
-
handleResult();
|
|
511
|
-
}
|
|
512
|
-
internal.backupState = internal.rootState;
|
|
513
|
-
const [draft, finalize] = createWithMutative(internal.rootState, {
|
|
514
|
-
enablePatches: true
|
|
515
|
-
});
|
|
516
|
-
internal.finalizeDraft = finalize;
|
|
517
|
-
internal.rootState = draft;
|
|
518
|
-
let asyncResult;
|
|
519
|
-
try {
|
|
520
|
-
result = fn.apply(getActionTarget(store, sliceKey), args);
|
|
521
|
-
if (result instanceof Promise) {
|
|
522
|
-
asyncResult = result;
|
|
523
|
-
}
|
|
524
|
-
} finally {
|
|
525
|
-
if (!asyncResult) {
|
|
526
|
-
handleResult(isDrafted);
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
if (asyncResult) {
|
|
530
|
-
return asyncResult.then(
|
|
531
|
-
(value) => {
|
|
532
|
-
handleResult(isDrafted);
|
|
533
|
-
return value;
|
|
534
|
-
},
|
|
535
|
-
(error) => {
|
|
536
|
-
handleResult(isDrafted);
|
|
537
|
-
throw error;
|
|
538
|
-
}
|
|
539
|
-
);
|
|
540
|
-
}
|
|
541
|
-
return result;
|
|
542
|
-
}
|
|
543
|
-
if (internal.mutableInstance && internal.actMutable) {
|
|
544
|
-
return internal.actMutable(() => {
|
|
545
|
-
return fn.apply(getActionTarget(store, sliceKey), args);
|
|
546
|
-
});
|
|
547
|
-
}
|
|
548
|
-
return fn.apply(getActionTarget(store, sliceKey), args);
|
|
549
|
-
});
|
|
550
|
-
};
|
|
470
|
+
const isTransportErrorEnvelope = (value) => {
|
|
471
|
+
if (typeof value !== "object" || value === null) return false;
|
|
472
|
+
return value[transportErrorMarker$1] === true && typeof value.message === "string";
|
|
551
473
|
};
|
|
552
|
-
|
|
553
|
-
|
|
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;
|
|
554
659
|
var Computed = class {
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
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
|
+
}
|
|
559
729
|
};
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
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;
|
|
570
768
|
};
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
return resultFunc.apply(this, arguments);
|
|
575
|
-
});
|
|
576
|
-
return function() {
|
|
577
|
-
return memoizedResultFunc.apply(
|
|
578
|
-
this,
|
|
579
|
-
dependenciesFunc.apply(null, [this])
|
|
580
|
-
);
|
|
581
|
-
};
|
|
582
|
-
};
|
|
769
|
+
const prepareAccessorDescriptor = ({ descriptor, internal }) => {
|
|
770
|
+
if (internal.mutableInstance || typeof descriptor.get !== "function") return;
|
|
771
|
+
descriptor.get = createCachedGetter(internal, descriptor.get);
|
|
583
772
|
};
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
sliceKey
|
|
593
|
-
}) => {
|
|
594
|
-
const isComputed = descriptor.value instanceof Computed;
|
|
595
|
-
if (internal.mutableInstance) {
|
|
596
|
-
Object.defineProperty(rawState, key, {
|
|
597
|
-
get: () => internal.mutableInstance[key],
|
|
598
|
-
set: (value) => {
|
|
599
|
-
internal.mutableInstance[key] = value;
|
|
600
|
-
},
|
|
601
|
-
enumerable: true
|
|
602
|
-
});
|
|
603
|
-
} else if (!isComputed) {
|
|
604
|
-
setOwnEnumerable(rawState, key, descriptor.value);
|
|
605
|
-
}
|
|
606
|
-
if (isComputed) {
|
|
607
|
-
if (internal.mutableInstance) {
|
|
608
|
-
throw new Error("Computed is not supported with mutable instance");
|
|
609
|
-
}
|
|
610
|
-
const { deps, fn } = descriptor.value;
|
|
611
|
-
const depsCallbackSelector = createSelectorWithArray(
|
|
612
|
-
// the root state should be updated, and the computed property will be updated.
|
|
613
|
-
() => [internal.rootState],
|
|
614
|
-
() => {
|
|
615
|
-
return deps(internal.module);
|
|
616
|
-
}
|
|
617
|
-
);
|
|
618
|
-
const selector = createSelectorWithArray(
|
|
619
|
-
(that) => depsCallbackSelector.call(that),
|
|
620
|
-
fn
|
|
621
|
-
);
|
|
622
|
-
descriptor.get = function() {
|
|
623
|
-
return selector.call(this);
|
|
624
|
-
};
|
|
625
|
-
} else if (sliceKey) {
|
|
626
|
-
descriptor.get = () => internal.rootState[sliceKey][key];
|
|
627
|
-
descriptor.set = (value) => {
|
|
628
|
-
internal.rootState[sliceKey][key] = value;
|
|
629
|
-
};
|
|
630
|
-
} else {
|
|
631
|
-
descriptor.get = () => internal.rootState[key];
|
|
632
|
-
descriptor.set = (value) => {
|
|
633
|
-
internal.rootState[key] = value;
|
|
634
|
-
};
|
|
635
|
-
}
|
|
636
|
-
delete descriptor.value;
|
|
637
|
-
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;
|
|
638
781
|
};
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
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;
|
|
653
848
|
};
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
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
|
+
};
|
|
727
972
|
};
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
var handleState = (store, internal, options) => {
|
|
735
|
-
const setState = (next, updater = (next2) => {
|
|
736
|
-
const merge = (_next = next2) => {
|
|
737
|
-
mergeObject(internal.rootState, _next, store.isSliceStore);
|
|
738
|
-
};
|
|
739
|
-
const fn = typeof next2 === "function" ? () => {
|
|
740
|
-
const returnValue = next2(internal.module);
|
|
741
|
-
if (returnValue instanceof Promise) {
|
|
742
|
-
throw new Error(
|
|
743
|
-
"setState with async function is not supported"
|
|
744
|
-
);
|
|
745
|
-
}
|
|
746
|
-
if (typeof returnValue === "object" && returnValue !== null) {
|
|
747
|
-
merge(returnValue);
|
|
748
|
-
}
|
|
749
|
-
} : merge;
|
|
750
|
-
const enablePatches = store.transport ?? options.enablePatches;
|
|
751
|
-
if (!enablePatches && internal.mutableInstance) {
|
|
752
|
-
if (internal.actMutable) {
|
|
753
|
-
internal.actMutable(() => {
|
|
754
|
-
fn.apply(null);
|
|
755
|
-
});
|
|
756
|
-
return [];
|
|
757
|
-
}
|
|
758
|
-
fn.apply(null);
|
|
759
|
-
return [];
|
|
760
|
-
}
|
|
761
|
-
internal.backupState = internal.rootState;
|
|
762
|
-
let patches;
|
|
763
|
-
let inversePatches;
|
|
764
|
-
try {
|
|
765
|
-
const result = createWithMutative2(
|
|
766
|
-
internal.rootState,
|
|
767
|
-
(draft) => {
|
|
768
|
-
internal.rootState = draft;
|
|
769
|
-
return fn.apply(null);
|
|
770
|
-
},
|
|
771
|
-
{
|
|
772
|
-
enablePatches: true
|
|
773
|
-
}
|
|
774
|
-
);
|
|
775
|
-
patches = result[1];
|
|
776
|
-
inversePatches = result[2];
|
|
777
|
-
} finally {
|
|
778
|
-
internal.rootState = internal.backupState;
|
|
779
|
-
}
|
|
780
|
-
const finalPatches = store.patch ? store.patch({ patches, inversePatches }) : { patches, inversePatches };
|
|
781
|
-
if (finalPatches.patches.length) {
|
|
782
|
-
store.apply(internal.rootState, finalPatches.patches);
|
|
783
|
-
}
|
|
784
|
-
return [
|
|
785
|
-
internal.rootState,
|
|
786
|
-
finalPatches.patches,
|
|
787
|
-
finalPatches.inversePatches
|
|
788
|
-
];
|
|
789
|
-
}) => {
|
|
790
|
-
if (store.share === "client") {
|
|
791
|
-
throw new Error(
|
|
792
|
-
`setState() cannot be called in the client store. To update the state, please trigger a store method with setState() instead.`
|
|
793
|
-
);
|
|
794
|
-
}
|
|
795
|
-
if (internal.isBatching) {
|
|
796
|
-
throw new Error("setState cannot be called within the updater");
|
|
797
|
-
}
|
|
798
|
-
if (next === null) {
|
|
799
|
-
return [];
|
|
800
|
-
}
|
|
801
|
-
internal.isBatching = true;
|
|
802
|
-
if (!store.share && !options.enablePatches && !internal.mutableInstance) {
|
|
803
|
-
if (typeof next === "function") {
|
|
804
|
-
try {
|
|
805
|
-
internal.backupState = internal.rootState;
|
|
806
|
-
internal.rootState = createWithMutative2(
|
|
807
|
-
internal.rootState,
|
|
808
|
-
(draft) => {
|
|
809
|
-
internal.rootState = draft;
|
|
810
|
-
return next(internal.module);
|
|
811
|
-
}
|
|
812
|
-
);
|
|
813
|
-
} catch (error) {
|
|
814
|
-
internal.rootState = internal.backupState;
|
|
815
|
-
internal.isBatching = false;
|
|
816
|
-
throw error;
|
|
817
|
-
}
|
|
818
|
-
} else {
|
|
819
|
-
const copy = cloneOwnEnumerable(internal.rootState);
|
|
820
|
-
if (store.isSliceStore) {
|
|
821
|
-
for (const key of Object.keys(next)) {
|
|
822
|
-
if (!Object.prototype.hasOwnProperty.call(copy, key)) {
|
|
823
|
-
continue;
|
|
824
|
-
}
|
|
825
|
-
const sourceValue = next[key];
|
|
826
|
-
if (typeof sourceValue !== "object" || sourceValue === null) {
|
|
827
|
-
continue;
|
|
828
|
-
}
|
|
829
|
-
const targetValue = copy[key];
|
|
830
|
-
if (typeof targetValue !== "object" || targetValue === null) {
|
|
831
|
-
continue;
|
|
832
|
-
}
|
|
833
|
-
const sliceCopy = cloneOwnEnumerable(
|
|
834
|
-
targetValue
|
|
835
|
-
);
|
|
836
|
-
mergeObject(sliceCopy, sourceValue);
|
|
837
|
-
setOwnEnumerable(copy, key, sliceCopy);
|
|
838
|
-
}
|
|
839
|
-
} else {
|
|
840
|
-
mergeObject(copy, next);
|
|
841
|
-
}
|
|
842
|
-
internal.rootState = copy;
|
|
843
|
-
}
|
|
844
|
-
if (internal.updateImmutable) {
|
|
845
|
-
internal.updateImmutable(internal.rootState);
|
|
846
|
-
} else {
|
|
847
|
-
internal.listeners.forEach((listener) => listener());
|
|
848
|
-
}
|
|
849
|
-
internal.isBatching = false;
|
|
850
|
-
return [];
|
|
851
|
-
}
|
|
852
|
-
let result;
|
|
853
|
-
try {
|
|
854
|
-
const isDrafted = internal.mutableInstance && isDraft2(internal.rootState);
|
|
855
|
-
if (isDrafted) {
|
|
856
|
-
handleDraft(store, internal);
|
|
857
|
-
}
|
|
858
|
-
result = updater(next);
|
|
859
|
-
if (isDrafted) {
|
|
860
|
-
internal.backupState = internal.rootState;
|
|
861
|
-
const [draft, finalize] = createWithMutative2(
|
|
862
|
-
internal.rootState,
|
|
863
|
-
{
|
|
864
|
-
enablePatches: true
|
|
865
|
-
}
|
|
866
|
-
);
|
|
867
|
-
internal.finalizeDraft = finalize;
|
|
868
|
-
internal.rootState = draft;
|
|
869
|
-
}
|
|
870
|
-
} finally {
|
|
871
|
-
internal.isBatching = false;
|
|
872
|
-
}
|
|
873
|
-
emit(store, internal, result?.[1]);
|
|
874
|
-
return result;
|
|
875
|
-
};
|
|
876
|
-
const getState = (deps, selector) => deps && selector ? new Computed(deps, selector) : internal.module;
|
|
877
|
-
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";
|
|
878
979
|
};
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
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);
|
|
887
991
|
};
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
}
|
|
894
|
-
}
|
|
895
|
-
const nextStore = middleware(store2);
|
|
896
|
-
if (process.env.NODE_ENV === "development") {
|
|
897
|
-
if (!isStoreLike(nextStore)) {
|
|
898
|
-
throw new Error(
|
|
899
|
-
`middlewares[${index}] should return a store-like object`
|
|
900
|
-
);
|
|
901
|
-
}
|
|
902
|
-
}
|
|
903
|
-
return nextStore;
|
|
904
|
-
}, 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);
|
|
905
997
|
};
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
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;
|
|
914
1031
|
};
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
if (typeof base === "function") {
|
|
934
|
-
base = base.bind(obj);
|
|
935
|
-
}
|
|
936
|
-
obj = base;
|
|
937
|
-
}
|
|
938
|
-
if (typeof base !== "function") {
|
|
939
|
-
throw new Error("The function is not found");
|
|
940
|
-
}
|
|
941
|
-
const result = await base(...args);
|
|
942
|
-
return [result, internal.sequence];
|
|
943
|
-
} catch (error) {
|
|
944
|
-
if (process.env.NODE_ENV === "development") {
|
|
945
|
-
console.error(error);
|
|
946
|
-
}
|
|
947
|
-
return [
|
|
948
|
-
{
|
|
949
|
-
[transportErrorMarker2]: true,
|
|
950
|
-
message: getErrorMessage(error)
|
|
951
|
-
},
|
|
952
|
-
internal.sequence
|
|
953
|
-
];
|
|
954
|
-
}
|
|
955
|
-
});
|
|
956
|
-
transport.listen("fullSync", async () => {
|
|
957
|
-
return {
|
|
958
|
-
state: JSON.stringify(internal.rootState),
|
|
959
|
-
sequence: internal.sequence
|
|
960
|
-
};
|
|
961
|
-
});
|
|
962
|
-
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
|
+
};
|
|
963
1050
|
};
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
const storeTransport = options.transport;
|
|
972
|
-
const clientTransport = options.clientTransport;
|
|
973
|
-
const worker = options.worker;
|
|
974
|
-
const explicitWorkerType = options.workerType;
|
|
975
|
-
if (storeTransport && clientTransport) {
|
|
976
|
-
throw new Error(
|
|
977
|
-
"transport and clientTransport cannot be used together, please use one authority model per store."
|
|
978
|
-
);
|
|
979
|
-
}
|
|
980
|
-
if (storeTransport && worker) {
|
|
981
|
-
throw new Error(
|
|
982
|
-
"transport and worker cannot be used together, please use one authority model per store."
|
|
983
|
-
);
|
|
984
|
-
}
|
|
985
|
-
if (clientTransport && worker) {
|
|
986
|
-
throw new Error(
|
|
987
|
-
"clientTransport and worker cannot be used together, please use one client transport source."
|
|
988
|
-
);
|
|
989
|
-
}
|
|
990
|
-
if (isMainWorkerType(explicitWorkerType) && (clientTransport || worker)) {
|
|
991
|
-
throw new Error(
|
|
992
|
-
"main workerType cannot be combined with client transport settings."
|
|
993
|
-
);
|
|
994
|
-
}
|
|
995
|
-
if (isClientWorkerType(explicitWorkerType) && storeTransport) {
|
|
996
|
-
throw new Error("client workerType cannot be combined with transport.");
|
|
997
|
-
}
|
|
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();
|
|
998
1058
|
};
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
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.");
|
|
1012
1075
|
};
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
sequence: 0,
|
|
1023
|
-
isBatching: false,
|
|
1024
|
-
listeners: /* @__PURE__ */ new Set()
|
|
1025
|
-
};
|
|
1026
|
-
const name = options.name ?? defaultName;
|
|
1027
|
-
const shouldTrackName = share2 === "main" && process.env.NODE_ENV !== "test";
|
|
1028
|
-
const releaseStoreName = () => {
|
|
1029
|
-
if (shouldTrackName) {
|
|
1030
|
-
namespaceMap.delete(name);
|
|
1031
|
-
}
|
|
1032
|
-
};
|
|
1033
|
-
if (shouldTrackName) {
|
|
1034
|
-
if (namespaceMap.get(name)) {
|
|
1035
|
-
throw new Error(`Store name '${name}' is not unique.`);
|
|
1036
|
-
}
|
|
1037
|
-
namespaceMap.set(name, true);
|
|
1038
|
-
}
|
|
1039
|
-
try {
|
|
1040
|
-
const { setState, getState } = handleState(store2, internal2, options);
|
|
1041
|
-
const subscribe = (listener) => {
|
|
1042
|
-
internal2.listeners.add(listener);
|
|
1043
|
-
return () => internal2.listeners.delete(listener);
|
|
1044
|
-
};
|
|
1045
|
-
let isDestroyed = false;
|
|
1046
|
-
const destroy = () => {
|
|
1047
|
-
if (isDestroyed) {
|
|
1048
|
-
return;
|
|
1049
|
-
}
|
|
1050
|
-
isDestroyed = true;
|
|
1051
|
-
internal2.listeners.clear();
|
|
1052
|
-
store2.transport?.dispose();
|
|
1053
|
-
releaseStoreName();
|
|
1054
|
-
};
|
|
1055
|
-
const apply = (state = internal2.rootState, patches) => {
|
|
1056
|
-
internal2.rootState = patches ? applyWithMutative(state, patches) : state;
|
|
1057
|
-
if (internal2.updateImmutable) {
|
|
1058
|
-
internal2.updateImmutable(internal2.rootState);
|
|
1059
|
-
} else {
|
|
1060
|
-
internal2.listeners.forEach((listener) => listener());
|
|
1061
|
-
}
|
|
1062
|
-
};
|
|
1063
|
-
const getPureState = () => internal2.rootState;
|
|
1064
|
-
const isFunctionMapObject = () => {
|
|
1065
|
-
if (typeof createState === "object" && createState !== null) {
|
|
1066
|
-
const values = Object.values(createState);
|
|
1067
|
-
return values.length > 0 && values.every((value) => typeof value === "function");
|
|
1068
|
-
}
|
|
1069
|
-
return false;
|
|
1070
|
-
};
|
|
1071
|
-
const getIsSliceStore = () => {
|
|
1072
|
-
const sliceMode = options.sliceMode ?? "auto";
|
|
1073
|
-
if (sliceMode === "single") {
|
|
1074
|
-
return false;
|
|
1075
|
-
}
|
|
1076
|
-
if (sliceMode === "slices") {
|
|
1077
|
-
if (!isFunctionMapObject()) {
|
|
1078
|
-
throw new Error(
|
|
1079
|
-
`sliceMode: 'slices' requires createState to be an object of slice functions.`
|
|
1080
|
-
);
|
|
1081
|
-
}
|
|
1082
|
-
return true;
|
|
1083
|
-
}
|
|
1084
|
-
if (isFunctionMapObject()) {
|
|
1085
|
-
warnAmbiguousFunctionMap();
|
|
1086
|
-
return true;
|
|
1087
|
-
}
|
|
1088
|
-
return false;
|
|
1089
|
-
};
|
|
1090
|
-
const isSliceStore = getIsSliceStore();
|
|
1091
|
-
Object.assign(store2, {
|
|
1092
|
-
name,
|
|
1093
|
-
share: share2 ?? false,
|
|
1094
|
-
setState,
|
|
1095
|
-
getState,
|
|
1096
|
-
subscribe,
|
|
1097
|
-
destroy,
|
|
1098
|
-
apply,
|
|
1099
|
-
isSliceStore,
|
|
1100
|
-
getPureState
|
|
1101
|
-
});
|
|
1102
|
-
const middlewareStore = applyMiddlewares(
|
|
1103
|
-
store2,
|
|
1104
|
-
options.middlewares ?? []
|
|
1105
|
-
);
|
|
1106
|
-
if (middlewareStore !== store2) {
|
|
1107
|
-
Object.assign(store2, middlewareStore);
|
|
1108
|
-
}
|
|
1109
|
-
const initialState = getInitialState(store2, createState, internal2);
|
|
1110
|
-
store2.getInitialState = () => initialState;
|
|
1111
|
-
internal2.rootState = getRawState(
|
|
1112
|
-
store2,
|
|
1113
|
-
internal2,
|
|
1114
|
-
initialState,
|
|
1115
|
-
options
|
|
1116
|
-
);
|
|
1117
|
-
return { store: store2, internal: internal2 };
|
|
1118
|
-
} catch (error) {
|
|
1119
|
-
releaseStoreName();
|
|
1120
|
-
throw error;
|
|
1121
|
-
}
|
|
1122
|
-
};
|
|
1123
|
-
if (options.clientTransport || options.worker || options.workerType === "WebWorkerClient" || options.workerType === "SharedWorkerClient") {
|
|
1124
|
-
if (checkEnablePatches) {
|
|
1125
|
-
throw new Error(`enablePatches: true is required for the async store`);
|
|
1126
|
-
}
|
|
1127
|
-
const store2 = createAsyncClientStore(
|
|
1128
|
-
createStore,
|
|
1129
|
-
options
|
|
1130
|
-
);
|
|
1131
|
-
return wrapStore(store2);
|
|
1132
|
-
}
|
|
1133
|
-
const { store, internal } = createStore({
|
|
1134
|
-
share
|
|
1135
|
-
});
|
|
1136
|
-
handleMainTransport(
|
|
1137
|
-
store,
|
|
1138
|
-
internal,
|
|
1139
|
-
storeTransport,
|
|
1140
|
-
workerType,
|
|
1141
|
-
checkEnablePatches
|
|
1142
|
-
);
|
|
1143
|
-
return wrapStore(store);
|
|
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(" "));
|
|
1144
1085
|
};
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
}
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
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
|
+
});
|
|
1238
|
+
}
|
|
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);
|
|
1160
1249
|
}
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
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);
|
|
1165
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 };
|