coaction 0.1.5 → 0.2.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/LICENSE +21 -0
- package/README.md +12 -0
- package/dist/{declarations/src/interface.d.ts → index.d.mts} +135 -61
- package/dist/index.d.ts +290 -0
- package/dist/index.js +841 -0
- package/dist/index.mjs +818 -0
- package/package.json +24 -22
- package/dist/coaction.cjs.d.mts +0 -2
- package/dist/coaction.cjs.d.ts +0 -2
- package/dist/coaction.cjs.js +0 -679
- package/dist/coaction.cjs.mjs +0 -5
- package/dist/coaction.esm.js +0 -673
- package/dist/coaction.umd.min.js +0 -2
- package/dist/coaction.umd.min.js.map +0 -1
- package/dist/declarations/src/binder.d.ts +0 -28
- package/dist/declarations/src/create.d.ts +0 -5
- package/dist/declarations/src/index.d.ts +0 -4
- package/dist/declarations/src/internal.d.ts +0 -48
- package/dist/declarations/src/wrapStore.d.ts +0 -5
package/dist/index.js
ADDED
|
@@ -0,0 +1,841 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
create: () => create,
|
|
24
|
+
createBinder: () => createBinder,
|
|
25
|
+
wrapStore: () => wrapStore
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
|
|
29
|
+
// src/create.ts
|
|
30
|
+
var import_mutative3 = require("mutative");
|
|
31
|
+
|
|
32
|
+
// src/global.ts
|
|
33
|
+
var getGlobal = () => {
|
|
34
|
+
let _global2;
|
|
35
|
+
if (typeof window !== "undefined") {
|
|
36
|
+
_global2 = window;
|
|
37
|
+
} else if (typeof global !== "undefined") {
|
|
38
|
+
_global2 = global;
|
|
39
|
+
} else if (typeof self !== "undefined") {
|
|
40
|
+
_global2 = self;
|
|
41
|
+
} else {
|
|
42
|
+
_global2 = {};
|
|
43
|
+
}
|
|
44
|
+
return _global2;
|
|
45
|
+
};
|
|
46
|
+
var _global = getGlobal();
|
|
47
|
+
|
|
48
|
+
// src/constant.ts
|
|
49
|
+
var WorkerType = _global.SharedWorkerGlobalScope ? "SharedWorkerInternal" : globalThis.WorkerGlobalScope ? "WebWorkerInternal" : null;
|
|
50
|
+
var bindSymbol = /* @__PURE__ */ Symbol("bind");
|
|
51
|
+
var defaultName = "default";
|
|
52
|
+
|
|
53
|
+
// src/asyncClientStore.ts
|
|
54
|
+
var import_data_transport = require("data-transport");
|
|
55
|
+
|
|
56
|
+
// src/wrapStore.ts
|
|
57
|
+
var wrapStore = (store, getState = () => store.getState()) => {
|
|
58
|
+
const { name, ..._store } = store;
|
|
59
|
+
return Object.assign(
|
|
60
|
+
{
|
|
61
|
+
[name]: (...args) => getState(...args)
|
|
62
|
+
}[name],
|
|
63
|
+
_store
|
|
64
|
+
);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// src/asyncClientStore.ts
|
|
68
|
+
var createAsyncClientStore = (createStore, asyncStoreClientOption) => {
|
|
69
|
+
const { store: asyncClientStore, internal } = createStore({
|
|
70
|
+
share: "client"
|
|
71
|
+
});
|
|
72
|
+
const transport = asyncStoreClientOption.worker ? (0, import_data_transport.createTransport)(
|
|
73
|
+
asyncStoreClientOption.worker instanceof SharedWorker ? "SharedWorkerClient" : "WebWorkerClient",
|
|
74
|
+
{
|
|
75
|
+
worker: asyncStoreClientOption.worker,
|
|
76
|
+
prefix: asyncClientStore.name
|
|
77
|
+
}
|
|
78
|
+
) : asyncStoreClientOption.clientTransport;
|
|
79
|
+
if (!transport) {
|
|
80
|
+
throw new Error("transport is required");
|
|
81
|
+
}
|
|
82
|
+
asyncClientStore.transport = transport;
|
|
83
|
+
const fullSync = async () => {
|
|
84
|
+
const latest = await transport.emit("fullSync");
|
|
85
|
+
asyncClientStore.apply(JSON.parse(latest.state));
|
|
86
|
+
internal.sequence = latest.sequence;
|
|
87
|
+
};
|
|
88
|
+
if (typeof transport.onConnect !== "function") {
|
|
89
|
+
throw new Error("transport.onConnect is required");
|
|
90
|
+
}
|
|
91
|
+
transport.onConnect?.(async () => {
|
|
92
|
+
await fullSync();
|
|
93
|
+
});
|
|
94
|
+
transport.listen("update", async (options) => {
|
|
95
|
+
if (typeof options.sequence === "number" && options.sequence === internal.sequence + 1) {
|
|
96
|
+
internal.sequence = options.sequence;
|
|
97
|
+
asyncClientStore.apply(void 0, options.patches);
|
|
98
|
+
} else {
|
|
99
|
+
await fullSync();
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
return wrapStore(asyncClientStore, () => asyncClientStore.getState());
|
|
103
|
+
};
|
|
104
|
+
var emit = (store, internal, patches) => {
|
|
105
|
+
if (store.transport && patches?.length) {
|
|
106
|
+
internal.sequence += 1;
|
|
107
|
+
store.transport.emit(
|
|
108
|
+
{
|
|
109
|
+
name: "update",
|
|
110
|
+
respond: false
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
patches,
|
|
114
|
+
sequence: internal.sequence
|
|
115
|
+
}
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
var handleDraft = (store, internal) => {
|
|
120
|
+
internal.rootState = internal.backupState;
|
|
121
|
+
const [, patches, inversePatches] = internal.finalizeDraft();
|
|
122
|
+
const finalPatches = store.patch ? store.patch({ patches, inversePatches }) : { patches, inversePatches };
|
|
123
|
+
if (finalPatches.patches.length) {
|
|
124
|
+
store.apply(internal.rootState, finalPatches.patches);
|
|
125
|
+
emit(store, internal, finalPatches.patches);
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
// src/getInitialState.ts
|
|
130
|
+
var isObject = (value) => typeof value === "object" && value !== null;
|
|
131
|
+
var isStateFactory = (value) => typeof value === "function";
|
|
132
|
+
var hasGetState = (value) => (typeof value === "object" || typeof value === "function") && value !== null && typeof value.getState === "function";
|
|
133
|
+
var hasBindState = (value) => isObject(value) && !!value[bindSymbol];
|
|
134
|
+
var getInitialState = (store, createState, internal) => {
|
|
135
|
+
const makeState = (stateOrFn, key) => {
|
|
136
|
+
let state;
|
|
137
|
+
if (isStateFactory(stateOrFn)) {
|
|
138
|
+
state = stateOrFn(store.setState, store.getState, store);
|
|
139
|
+
} else if (isObject(stateOrFn)) {
|
|
140
|
+
state = stateOrFn;
|
|
141
|
+
} else {
|
|
142
|
+
if (process.env.NODE_ENV !== "production") {
|
|
143
|
+
throw new Error(
|
|
144
|
+
`Invalid state value encountered in makeState: ${key ? `for key ${key}, ` : ""}${typeof stateOrFn}`
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
return {};
|
|
148
|
+
}
|
|
149
|
+
if (hasGetState(state)) {
|
|
150
|
+
state = state.getState();
|
|
151
|
+
} else if (typeof state === "function") {
|
|
152
|
+
state = state();
|
|
153
|
+
}
|
|
154
|
+
if (hasBindState(state)) {
|
|
155
|
+
if (store.isSliceStore) {
|
|
156
|
+
throw new Error(
|
|
157
|
+
"Third-party state binding does not support Slices mode. Please inject a whole store instead."
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
const binder = state[bindSymbol];
|
|
161
|
+
const rawState = binder.bind(state);
|
|
162
|
+
binder.handleStore(
|
|
163
|
+
store,
|
|
164
|
+
rawState,
|
|
165
|
+
state,
|
|
166
|
+
internal,
|
|
167
|
+
key
|
|
168
|
+
);
|
|
169
|
+
delete state[bindSymbol];
|
|
170
|
+
return rawState;
|
|
171
|
+
}
|
|
172
|
+
return state;
|
|
173
|
+
};
|
|
174
|
+
return store.isSliceStore ? Object.entries(createState).reduce(
|
|
175
|
+
(stateTree, [key, value]) => Object.assign(stateTree, {
|
|
176
|
+
[key]: makeState(value, key)
|
|
177
|
+
}),
|
|
178
|
+
{}
|
|
179
|
+
) : makeState(createState);
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
// src/getRawState.ts
|
|
183
|
+
var import_mutative = require("mutative");
|
|
184
|
+
|
|
185
|
+
// src/utils.ts
|
|
186
|
+
var isEqual = (x, y) => {
|
|
187
|
+
if (x === y) {
|
|
188
|
+
return x !== 0 || y !== 0 || 1 / x === 1 / y;
|
|
189
|
+
}
|
|
190
|
+
return x !== x && y !== y;
|
|
191
|
+
};
|
|
192
|
+
var areShallowEqualWithArray = (prev, next) => {
|
|
193
|
+
if (prev === null || next === null || prev.length !== next.length) {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
const { length } = prev;
|
|
197
|
+
for (let i = 0; i < length; i += 1) {
|
|
198
|
+
if (!isEqual(prev[i], next[i])) {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return true;
|
|
203
|
+
};
|
|
204
|
+
var mergeObject = (target, source, isSlice) => {
|
|
205
|
+
if (isSlice) {
|
|
206
|
+
if (typeof source === "object" && source !== null) {
|
|
207
|
+
for (const key in source) {
|
|
208
|
+
if (typeof source[key] === "object" && source[key] !== null) {
|
|
209
|
+
Object.assign(target[key], source[key]);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
} else {
|
|
214
|
+
Object.assign(target, source);
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
var uuid = () => {
|
|
218
|
+
let timestamp = (/* @__PURE__ */ new Date()).getTime();
|
|
219
|
+
const uuidTemplate = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
|
|
220
|
+
const uuid2 = uuidTemplate.replace(/[xy]/g, (char) => {
|
|
221
|
+
let randomNum = (timestamp + Math.random() * 16) % 16 | 0;
|
|
222
|
+
timestamp = Math.floor(timestamp / 16);
|
|
223
|
+
return (char === "x" ? randomNum : randomNum & 3 | 8).toString(16);
|
|
224
|
+
});
|
|
225
|
+
return uuid2;
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
// src/computed.ts
|
|
229
|
+
var Computed = class {
|
|
230
|
+
constructor(deps, fn) {
|
|
231
|
+
this.deps = deps;
|
|
232
|
+
this.fn = fn;
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
var defaultMemoize = (func) => {
|
|
236
|
+
const lastArgs = /* @__PURE__ */ new WeakMap();
|
|
237
|
+
const lastResult = /* @__PURE__ */ new WeakMap();
|
|
238
|
+
return function() {
|
|
239
|
+
if (!areShallowEqualWithArray(lastArgs.get(this) ?? [], arguments)) {
|
|
240
|
+
lastResult.set(this, func.apply(this, arguments));
|
|
241
|
+
}
|
|
242
|
+
lastArgs.set(this, arguments);
|
|
243
|
+
return lastResult.get(this);
|
|
244
|
+
};
|
|
245
|
+
};
|
|
246
|
+
var createSelectorCreatorWithArray = (memoize = defaultMemoize) => {
|
|
247
|
+
return (dependenciesFunc, resultFunc) => {
|
|
248
|
+
const memoizedResultFunc = memoize(function() {
|
|
249
|
+
return resultFunc.apply(this, arguments);
|
|
250
|
+
});
|
|
251
|
+
return function() {
|
|
252
|
+
return memoizedResultFunc.apply(
|
|
253
|
+
this,
|
|
254
|
+
dependenciesFunc.apply(null, [this])
|
|
255
|
+
);
|
|
256
|
+
};
|
|
257
|
+
};
|
|
258
|
+
};
|
|
259
|
+
var createSelectorWithArray = createSelectorCreatorWithArray();
|
|
260
|
+
|
|
261
|
+
// src/getRawState.ts
|
|
262
|
+
var getRawState = (store, internal, initialState, options) => {
|
|
263
|
+
const rawState = {};
|
|
264
|
+
const handle = (_rawState, _initialState, sliceKey) => {
|
|
265
|
+
internal.mutableInstance = internal.toMutableRaw?.(_initialState);
|
|
266
|
+
const descriptors = Object.getOwnPropertyDescriptors(_initialState);
|
|
267
|
+
Object.entries(descriptors).forEach(([key, descriptor]) => {
|
|
268
|
+
if (Object.prototype.hasOwnProperty.call(descriptor, "value")) {
|
|
269
|
+
if (typeof descriptor.value !== "function") {
|
|
270
|
+
const isComputed = descriptor.value instanceof Computed;
|
|
271
|
+
if (internal.mutableInstance) {
|
|
272
|
+
Object.defineProperty(_rawState, key, {
|
|
273
|
+
get: () => internal.mutableInstance[key],
|
|
274
|
+
set: (value) => {
|
|
275
|
+
internal.mutableInstance[key] = value;
|
|
276
|
+
},
|
|
277
|
+
enumerable: true
|
|
278
|
+
});
|
|
279
|
+
} else if (!isComputed) {
|
|
280
|
+
_rawState[key] = descriptor.value;
|
|
281
|
+
}
|
|
282
|
+
if (isComputed) {
|
|
283
|
+
if (internal.mutableInstance) {
|
|
284
|
+
throw new Error(
|
|
285
|
+
"Computed is not supported with mutable instance"
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
const { deps, fn } = descriptor.value;
|
|
289
|
+
const depsCallbackSelector = createSelectorWithArray(
|
|
290
|
+
// the root state should be updated, and the computed property will be updated.
|
|
291
|
+
() => [internal.rootState],
|
|
292
|
+
() => {
|
|
293
|
+
return deps(internal.module);
|
|
294
|
+
}
|
|
295
|
+
);
|
|
296
|
+
const selector = createSelectorWithArray(
|
|
297
|
+
(that) => depsCallbackSelector.call(that),
|
|
298
|
+
fn
|
|
299
|
+
);
|
|
300
|
+
descriptor.get = function() {
|
|
301
|
+
return selector.call(this);
|
|
302
|
+
};
|
|
303
|
+
} else {
|
|
304
|
+
if (sliceKey) {
|
|
305
|
+
descriptor.get = () => internal.rootState[sliceKey][key];
|
|
306
|
+
descriptor.set = (value) => {
|
|
307
|
+
internal.rootState[sliceKey][key] = value;
|
|
308
|
+
};
|
|
309
|
+
} else {
|
|
310
|
+
descriptor.get = () => internal.rootState[key];
|
|
311
|
+
descriptor.set = (value) => {
|
|
312
|
+
internal.rootState[key] = value;
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
delete descriptor.value;
|
|
317
|
+
delete descriptor.writable;
|
|
318
|
+
} else if (store.share === "client") {
|
|
319
|
+
descriptor.value = (...args) => {
|
|
320
|
+
let actionId;
|
|
321
|
+
let done;
|
|
322
|
+
if (store.trace) {
|
|
323
|
+
actionId = uuid();
|
|
324
|
+
store.trace({
|
|
325
|
+
method: key,
|
|
326
|
+
parameters: args,
|
|
327
|
+
id: actionId,
|
|
328
|
+
sliceKey
|
|
329
|
+
});
|
|
330
|
+
done = (result) => {
|
|
331
|
+
store.trace({
|
|
332
|
+
method: key,
|
|
333
|
+
id: actionId,
|
|
334
|
+
result,
|
|
335
|
+
sliceKey
|
|
336
|
+
});
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
const keys = sliceKey ? [sliceKey, key] : [key];
|
|
340
|
+
return store.transport.emit("execute", keys, args).then(([result, sequence]) => {
|
|
341
|
+
if (internal.sequence >= sequence) {
|
|
342
|
+
if (result?.$$Error) {
|
|
343
|
+
done?.(result);
|
|
344
|
+
throw new Error(result.$$Error);
|
|
345
|
+
}
|
|
346
|
+
done?.(result);
|
|
347
|
+
return result;
|
|
348
|
+
}
|
|
349
|
+
if (process.env.NODE_ENV === "development") {
|
|
350
|
+
console.warn(
|
|
351
|
+
`The sequence of the action is not consistent.`,
|
|
352
|
+
sequence,
|
|
353
|
+
internal.sequence
|
|
354
|
+
);
|
|
355
|
+
}
|
|
356
|
+
return new Promise((resolve) => {
|
|
357
|
+
const unsubscribe = store.subscribe(() => {
|
|
358
|
+
if (internal.sequence >= sequence) {
|
|
359
|
+
unsubscribe();
|
|
360
|
+
done?.(result);
|
|
361
|
+
resolve(result);
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
});
|
|
365
|
+
});
|
|
366
|
+
};
|
|
367
|
+
} else {
|
|
368
|
+
const fn = descriptor.value;
|
|
369
|
+
descriptor.value = (...args) => {
|
|
370
|
+
let actionId;
|
|
371
|
+
let done;
|
|
372
|
+
if (store.trace) {
|
|
373
|
+
actionId = uuid();
|
|
374
|
+
store.trace({
|
|
375
|
+
method: key,
|
|
376
|
+
parameters: args,
|
|
377
|
+
id: actionId,
|
|
378
|
+
sliceKey
|
|
379
|
+
});
|
|
380
|
+
done = (result2) => {
|
|
381
|
+
store.trace({
|
|
382
|
+
method: key,
|
|
383
|
+
id: actionId,
|
|
384
|
+
result: result2,
|
|
385
|
+
sliceKey
|
|
386
|
+
});
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
const enablePatches = store.transport ?? options.enablePatches;
|
|
390
|
+
if (internal.mutableInstance && !internal.isBatching && enablePatches) {
|
|
391
|
+
let result2;
|
|
392
|
+
const handleResult = (isDrafted2) => {
|
|
393
|
+
handleDraft(store, internal);
|
|
394
|
+
if (isDrafted2) {
|
|
395
|
+
internal.backupState = internal.rootState;
|
|
396
|
+
const [draft2, finalize2] = (0, import_mutative.create)(
|
|
397
|
+
internal.rootState,
|
|
398
|
+
{
|
|
399
|
+
enablePatches: true
|
|
400
|
+
}
|
|
401
|
+
);
|
|
402
|
+
internal.finalizeDraft = finalize2;
|
|
403
|
+
internal.rootState = draft2;
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
const isDrafted = (0, import_mutative.isDraft)(internal.rootState);
|
|
407
|
+
if (isDrafted) {
|
|
408
|
+
handleResult();
|
|
409
|
+
}
|
|
410
|
+
internal.backupState = internal.rootState;
|
|
411
|
+
const [draft, finalize] = (0, import_mutative.create)(internal.rootState, {
|
|
412
|
+
enablePatches: true
|
|
413
|
+
});
|
|
414
|
+
internal.finalizeDraft = finalize;
|
|
415
|
+
internal.rootState = draft;
|
|
416
|
+
try {
|
|
417
|
+
result2 = fn.apply(
|
|
418
|
+
sliceKey ? store.getState()[sliceKey] : store.getState(),
|
|
419
|
+
args
|
|
420
|
+
);
|
|
421
|
+
} finally {
|
|
422
|
+
if (result2 instanceof Promise) {
|
|
423
|
+
return result2.finally(() => {
|
|
424
|
+
const result3 = handleResult(isDrafted);
|
|
425
|
+
done?.(result3);
|
|
426
|
+
return result3;
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
handleResult(isDrafted);
|
|
430
|
+
}
|
|
431
|
+
done?.(result2);
|
|
432
|
+
return result2;
|
|
433
|
+
}
|
|
434
|
+
if (internal.mutableInstance && internal.actMutable) {
|
|
435
|
+
const result2 = internal.actMutable(() => {
|
|
436
|
+
return fn.apply(
|
|
437
|
+
sliceKey ? store.getState()[sliceKey] : store.getState(),
|
|
438
|
+
args
|
|
439
|
+
);
|
|
440
|
+
});
|
|
441
|
+
done?.(result2);
|
|
442
|
+
return result2;
|
|
443
|
+
}
|
|
444
|
+
const result = fn.apply(
|
|
445
|
+
sliceKey ? store.getState()[sliceKey] : store.getState(),
|
|
446
|
+
args
|
|
447
|
+
);
|
|
448
|
+
done?.(result);
|
|
449
|
+
return result;
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
});
|
|
454
|
+
const slice = Object.defineProperties({}, descriptors);
|
|
455
|
+
return slice;
|
|
456
|
+
};
|
|
457
|
+
if (store.isSliceStore) {
|
|
458
|
+
internal.module = {};
|
|
459
|
+
Object.entries(initialState).forEach(([key, value]) => {
|
|
460
|
+
rawState[key] = {};
|
|
461
|
+
internal.module[key] = handle(rawState[key], value, key);
|
|
462
|
+
});
|
|
463
|
+
} else {
|
|
464
|
+
internal.module = handle(rawState, initialState);
|
|
465
|
+
}
|
|
466
|
+
return rawState;
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
// src/handleState.ts
|
|
470
|
+
var import_mutative2 = require("mutative");
|
|
471
|
+
var handleState = (store, internal, options) => {
|
|
472
|
+
const setState = (next, updater = (next2) => {
|
|
473
|
+
const merge = (_next = next2) => {
|
|
474
|
+
mergeObject(internal.rootState, _next, store.isSliceStore);
|
|
475
|
+
};
|
|
476
|
+
const fn = typeof next2 === "function" ? () => {
|
|
477
|
+
const returnValue = next2(internal.module);
|
|
478
|
+
if (returnValue instanceof Promise) {
|
|
479
|
+
throw new Error(
|
|
480
|
+
"setState with async function is not supported"
|
|
481
|
+
);
|
|
482
|
+
}
|
|
483
|
+
if (typeof returnValue === "object" && returnValue !== null) {
|
|
484
|
+
merge(returnValue);
|
|
485
|
+
}
|
|
486
|
+
} : merge;
|
|
487
|
+
const enablePatches = store.transport ?? options.enablePatches;
|
|
488
|
+
if (!enablePatches && internal.mutableInstance) {
|
|
489
|
+
if (internal.actMutable) {
|
|
490
|
+
internal.actMutable(() => {
|
|
491
|
+
fn.apply(null);
|
|
492
|
+
});
|
|
493
|
+
return [];
|
|
494
|
+
}
|
|
495
|
+
fn.apply(null);
|
|
496
|
+
return [];
|
|
497
|
+
}
|
|
498
|
+
internal.backupState = internal.rootState;
|
|
499
|
+
let patches;
|
|
500
|
+
let inversePatches;
|
|
501
|
+
try {
|
|
502
|
+
const result = (0, import_mutative2.create)(
|
|
503
|
+
internal.rootState,
|
|
504
|
+
(draft) => {
|
|
505
|
+
internal.rootState = draft;
|
|
506
|
+
return fn.apply(null);
|
|
507
|
+
},
|
|
508
|
+
{
|
|
509
|
+
enablePatches: true
|
|
510
|
+
}
|
|
511
|
+
);
|
|
512
|
+
patches = result[1];
|
|
513
|
+
inversePatches = result[2];
|
|
514
|
+
} finally {
|
|
515
|
+
internal.rootState = internal.backupState;
|
|
516
|
+
}
|
|
517
|
+
const finalPatches = store.patch ? store.patch({ patches, inversePatches }) : { patches, inversePatches };
|
|
518
|
+
if (finalPatches.patches.length) {
|
|
519
|
+
store.apply(internal.rootState, finalPatches.patches);
|
|
520
|
+
if (!internal.mutableInstance) {
|
|
521
|
+
if (internal.updateImmutable) {
|
|
522
|
+
internal.updateImmutable(internal.rootState);
|
|
523
|
+
} else {
|
|
524
|
+
internal.listeners.forEach((listener) => listener());
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
return [internal.rootState, patches, inversePatches];
|
|
529
|
+
}) => {
|
|
530
|
+
if (store.share === "client") {
|
|
531
|
+
throw new Error(
|
|
532
|
+
`setState() cannot be called in the client store. To update the state, please trigger a store method with setState() instead.`
|
|
533
|
+
);
|
|
534
|
+
}
|
|
535
|
+
if (internal.isBatching) {
|
|
536
|
+
throw new Error("setState cannot be called within the updater");
|
|
537
|
+
}
|
|
538
|
+
internal.isBatching = true;
|
|
539
|
+
if (!store.share && !options.enablePatches && !internal.mutableInstance) {
|
|
540
|
+
if (typeof next === "function") {
|
|
541
|
+
try {
|
|
542
|
+
internal.backupState = internal.rootState;
|
|
543
|
+
internal.rootState = (0, import_mutative2.create)(
|
|
544
|
+
internal.rootState,
|
|
545
|
+
(draft) => {
|
|
546
|
+
internal.rootState = draft;
|
|
547
|
+
return next(internal.module);
|
|
548
|
+
}
|
|
549
|
+
);
|
|
550
|
+
} catch (error) {
|
|
551
|
+
internal.rootState = internal.backupState;
|
|
552
|
+
internal.isBatching = false;
|
|
553
|
+
throw error;
|
|
554
|
+
}
|
|
555
|
+
} else {
|
|
556
|
+
const copy = {};
|
|
557
|
+
const rootState = internal.rootState;
|
|
558
|
+
for (const key of Object.keys(rootState)) {
|
|
559
|
+
copy[key] = rootState[key];
|
|
560
|
+
}
|
|
561
|
+
for (const key of Object.keys(next)) {
|
|
562
|
+
copy[key] = next[key];
|
|
563
|
+
}
|
|
564
|
+
internal.rootState = copy;
|
|
565
|
+
}
|
|
566
|
+
if (internal.updateImmutable) {
|
|
567
|
+
internal.updateImmutable(internal.rootState);
|
|
568
|
+
} else {
|
|
569
|
+
internal.listeners.forEach((listener) => listener());
|
|
570
|
+
}
|
|
571
|
+
internal.isBatching = false;
|
|
572
|
+
return [];
|
|
573
|
+
}
|
|
574
|
+
let result;
|
|
575
|
+
try {
|
|
576
|
+
const isDrafted = internal.mutableInstance && (0, import_mutative2.isDraft)(internal.rootState);
|
|
577
|
+
if (isDrafted) {
|
|
578
|
+
handleDraft(store, internal);
|
|
579
|
+
}
|
|
580
|
+
result = updater(next);
|
|
581
|
+
if (isDrafted) {
|
|
582
|
+
internal.backupState = internal.rootState;
|
|
583
|
+
const [draft, finalize] = (0, import_mutative2.create)(
|
|
584
|
+
internal.rootState,
|
|
585
|
+
{
|
|
586
|
+
enablePatches: true
|
|
587
|
+
}
|
|
588
|
+
);
|
|
589
|
+
internal.finalizeDraft = finalize;
|
|
590
|
+
internal.rootState = draft;
|
|
591
|
+
}
|
|
592
|
+
} finally {
|
|
593
|
+
internal.isBatching = false;
|
|
594
|
+
}
|
|
595
|
+
emit(store, internal, result?.[1]);
|
|
596
|
+
return result;
|
|
597
|
+
};
|
|
598
|
+
const getState = (deps, selector) => deps && selector ? new Computed(deps, selector) : internal.module;
|
|
599
|
+
return { setState, getState };
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
// src/applyMiddlewares.ts
|
|
603
|
+
var isStoreLike = (value) => {
|
|
604
|
+
if (!value || typeof value !== "object") {
|
|
605
|
+
return false;
|
|
606
|
+
}
|
|
607
|
+
const candidate = value;
|
|
608
|
+
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";
|
|
609
|
+
};
|
|
610
|
+
var applyMiddlewares = (store, middlewares) => {
|
|
611
|
+
return middlewares.reduce((store2, middleware, index) => {
|
|
612
|
+
if (process.env.NODE_ENV === "development") {
|
|
613
|
+
if (typeof middleware !== "function") {
|
|
614
|
+
throw new Error(`middlewares[${index}] should be a function`);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
const nextStore = middleware(store2);
|
|
618
|
+
if (process.env.NODE_ENV === "development") {
|
|
619
|
+
if (!isStoreLike(nextStore)) {
|
|
620
|
+
throw new Error(
|
|
621
|
+
`middlewares[${index}] should return a store-like object`
|
|
622
|
+
);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
return nextStore;
|
|
626
|
+
}, store);
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
// src/handleMainTransport.ts
|
|
630
|
+
var import_data_transport2 = require("data-transport");
|
|
631
|
+
var getErrorMessage = (error) => {
|
|
632
|
+
if (error instanceof Error) {
|
|
633
|
+
return error.message;
|
|
634
|
+
}
|
|
635
|
+
return String(error);
|
|
636
|
+
};
|
|
637
|
+
var handleMainTransport = (store, internal, storeTransport, workerType, checkEnablePatches) => {
|
|
638
|
+
const transport = storeTransport ?? (workerType === "SharedWorkerInternal" || workerType === "WebWorkerInternal" ? (0, import_data_transport2.createTransport)(workerType, {
|
|
639
|
+
prefix: store.name
|
|
640
|
+
}) : void 0);
|
|
641
|
+
if (!transport) return;
|
|
642
|
+
if (typeof transport.onConnect !== "function") {
|
|
643
|
+
throw new Error("transport.onConnect is required");
|
|
644
|
+
}
|
|
645
|
+
if (checkEnablePatches) {
|
|
646
|
+
throw new Error(`enablePatches: true is required for the transport`);
|
|
647
|
+
}
|
|
648
|
+
transport.listen("execute", async (keys, args) => {
|
|
649
|
+
let base = store.getState();
|
|
650
|
+
let obj = base;
|
|
651
|
+
try {
|
|
652
|
+
for (const key of keys) {
|
|
653
|
+
base = base[key];
|
|
654
|
+
if (typeof base === "function") {
|
|
655
|
+
base = base.bind(obj);
|
|
656
|
+
}
|
|
657
|
+
obj = base;
|
|
658
|
+
}
|
|
659
|
+
if (typeof base !== "function") {
|
|
660
|
+
throw new Error("The function is not found");
|
|
661
|
+
}
|
|
662
|
+
return [base(...args), internal.sequence];
|
|
663
|
+
} catch (error) {
|
|
664
|
+
if (process.env.NODE_ENV === "development") {
|
|
665
|
+
console.error(error);
|
|
666
|
+
}
|
|
667
|
+
return [{ $$Error: getErrorMessage(error) }, internal.sequence];
|
|
668
|
+
}
|
|
669
|
+
});
|
|
670
|
+
transport.listen("fullSync", async () => {
|
|
671
|
+
return {
|
|
672
|
+
state: JSON.stringify(internal.rootState),
|
|
673
|
+
sequence: internal.sequence
|
|
674
|
+
};
|
|
675
|
+
});
|
|
676
|
+
store.transport = transport;
|
|
677
|
+
};
|
|
678
|
+
|
|
679
|
+
// src/create.ts
|
|
680
|
+
var namespaceMap = /* @__PURE__ */ new Map();
|
|
681
|
+
var create = (createState, options = {}) => {
|
|
682
|
+
const checkEnablePatches = Object.hasOwnProperty.call(options, "enablePatches") && !options.enablePatches;
|
|
683
|
+
const workerType = options.workerType ?? WorkerType;
|
|
684
|
+
if (process.env.NODE_ENV === "development" && options.transport && options.clientTransport) {
|
|
685
|
+
throw new Error(
|
|
686
|
+
`transport and clientTransport cannot be used together, please use one of them.`
|
|
687
|
+
);
|
|
688
|
+
}
|
|
689
|
+
const storeTransport = options.transport;
|
|
690
|
+
const share = workerType === "WebWorkerInternal" || workerType === "SharedWorkerInternal" || storeTransport ? "main" : void 0;
|
|
691
|
+
const createStore = ({ share: share2 }) => {
|
|
692
|
+
const store2 = {};
|
|
693
|
+
const internal2 = {
|
|
694
|
+
sequence: 0,
|
|
695
|
+
isBatching: false,
|
|
696
|
+
listeners: /* @__PURE__ */ new Set()
|
|
697
|
+
};
|
|
698
|
+
const name = options.name ?? defaultName;
|
|
699
|
+
const shouldTrackName = process.env.NODE_ENV === "development" && share2 === "main";
|
|
700
|
+
const releaseStoreName = () => {
|
|
701
|
+
if (shouldTrackName) {
|
|
702
|
+
namespaceMap.delete(name);
|
|
703
|
+
}
|
|
704
|
+
};
|
|
705
|
+
if (shouldTrackName) {
|
|
706
|
+
if (namespaceMap.get(name)) {
|
|
707
|
+
throw new Error(`Store name '${name}' is not unique.`);
|
|
708
|
+
}
|
|
709
|
+
namespaceMap.set(name, true);
|
|
710
|
+
}
|
|
711
|
+
try {
|
|
712
|
+
const { setState, getState } = handleState(store2, internal2, options);
|
|
713
|
+
const subscribe = (listener) => {
|
|
714
|
+
internal2.listeners.add(listener);
|
|
715
|
+
return () => internal2.listeners.delete(listener);
|
|
716
|
+
};
|
|
717
|
+
let isDestroyed = false;
|
|
718
|
+
const destroy = () => {
|
|
719
|
+
if (isDestroyed) {
|
|
720
|
+
return;
|
|
721
|
+
}
|
|
722
|
+
isDestroyed = true;
|
|
723
|
+
internal2.listeners.clear();
|
|
724
|
+
store2.transport?.dispose();
|
|
725
|
+
releaseStoreName();
|
|
726
|
+
};
|
|
727
|
+
const apply = (state = internal2.rootState, patches) => {
|
|
728
|
+
internal2.rootState = patches ? (0, import_mutative3.apply)(state, patches) : state;
|
|
729
|
+
if (internal2.updateImmutable) {
|
|
730
|
+
internal2.updateImmutable(internal2.rootState);
|
|
731
|
+
} else {
|
|
732
|
+
internal2.listeners.forEach((listener) => listener());
|
|
733
|
+
}
|
|
734
|
+
};
|
|
735
|
+
const getPureState = () => internal2.rootState;
|
|
736
|
+
const inferSliceStore = () => {
|
|
737
|
+
if (typeof createState === "object" && createState !== null) {
|
|
738
|
+
const values = Object.values(createState);
|
|
739
|
+
return values.length > 0 && values.every((value) => typeof value === "function");
|
|
740
|
+
}
|
|
741
|
+
return false;
|
|
742
|
+
};
|
|
743
|
+
const isValidSliceObject = () => {
|
|
744
|
+
if (typeof createState !== "object" || createState === null) {
|
|
745
|
+
return false;
|
|
746
|
+
}
|
|
747
|
+
const values = Object.values(createState);
|
|
748
|
+
return values.length > 0 && values.every((value) => typeof value === "function");
|
|
749
|
+
};
|
|
750
|
+
const getIsSliceStore = () => {
|
|
751
|
+
const sliceMode = options.sliceMode ?? "auto";
|
|
752
|
+
if (sliceMode === "single") {
|
|
753
|
+
return false;
|
|
754
|
+
}
|
|
755
|
+
if (sliceMode === "slices") {
|
|
756
|
+
if (!isValidSliceObject()) {
|
|
757
|
+
throw new Error(
|
|
758
|
+
`sliceMode: 'slices' requires createState to be an object of slice functions.`
|
|
759
|
+
);
|
|
760
|
+
}
|
|
761
|
+
return true;
|
|
762
|
+
}
|
|
763
|
+
return inferSliceStore();
|
|
764
|
+
};
|
|
765
|
+
const isSliceStore = getIsSliceStore();
|
|
766
|
+
Object.assign(store2, {
|
|
767
|
+
name,
|
|
768
|
+
share: share2 ?? false,
|
|
769
|
+
setState,
|
|
770
|
+
getState,
|
|
771
|
+
subscribe,
|
|
772
|
+
destroy,
|
|
773
|
+
apply,
|
|
774
|
+
isSliceStore,
|
|
775
|
+
getPureState
|
|
776
|
+
});
|
|
777
|
+
const middlewareStore = applyMiddlewares(
|
|
778
|
+
store2,
|
|
779
|
+
options.middlewares ?? []
|
|
780
|
+
);
|
|
781
|
+
if (middlewareStore !== store2) {
|
|
782
|
+
Object.assign(store2, middlewareStore);
|
|
783
|
+
}
|
|
784
|
+
const initialState = getInitialState(store2, createState, internal2);
|
|
785
|
+
store2.getInitialState = () => initialState;
|
|
786
|
+
internal2.rootState = getRawState(
|
|
787
|
+
store2,
|
|
788
|
+
internal2,
|
|
789
|
+
initialState,
|
|
790
|
+
options
|
|
791
|
+
);
|
|
792
|
+
return { store: store2, internal: internal2 };
|
|
793
|
+
} catch (error) {
|
|
794
|
+
releaseStoreName();
|
|
795
|
+
throw error;
|
|
796
|
+
}
|
|
797
|
+
};
|
|
798
|
+
if (options.clientTransport || options.worker || options.workerType === "WebWorkerClient" || options.workerType === "SharedWorkerClient") {
|
|
799
|
+
if (checkEnablePatches) {
|
|
800
|
+
throw new Error(`enablePatches: true is required for the async store`);
|
|
801
|
+
}
|
|
802
|
+
const store2 = createAsyncClientStore(
|
|
803
|
+
createStore,
|
|
804
|
+
options
|
|
805
|
+
);
|
|
806
|
+
return wrapStore(store2);
|
|
807
|
+
}
|
|
808
|
+
const { store, internal } = createStore({
|
|
809
|
+
share
|
|
810
|
+
});
|
|
811
|
+
handleMainTransport(
|
|
812
|
+
store,
|
|
813
|
+
internal,
|
|
814
|
+
storeTransport,
|
|
815
|
+
workerType,
|
|
816
|
+
checkEnablePatches
|
|
817
|
+
);
|
|
818
|
+
return wrapStore(store);
|
|
819
|
+
};
|
|
820
|
+
|
|
821
|
+
// src/binder.ts
|
|
822
|
+
function createBinder({
|
|
823
|
+
handleState: handleState2,
|
|
824
|
+
handleStore
|
|
825
|
+
}) {
|
|
826
|
+
return ((state) => {
|
|
827
|
+
const { copyState, key, bind } = handleState2(state);
|
|
828
|
+
const value = key ? copyState[key] : copyState;
|
|
829
|
+
value[bindSymbol] = {
|
|
830
|
+
handleStore,
|
|
831
|
+
bind
|
|
832
|
+
};
|
|
833
|
+
return copyState;
|
|
834
|
+
});
|
|
835
|
+
}
|
|
836
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
837
|
+
0 && (module.exports = {
|
|
838
|
+
create,
|
|
839
|
+
createBinder,
|
|
840
|
+
wrapStore
|
|
841
|
+
});
|