wevu 0.0.1 → 1.0.0-alpha.1
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 +119 -0
- package/dist/index-0dF4y5p6.d.mts +80 -0
- package/dist/index-DVEFI-Uo.d.cts +80 -0
- package/dist/index.cjs +525 -154
- package/dist/index.d.cts +397 -54
- package/dist/index.d.mts +397 -54
- package/dist/index.mjs +465 -119
- package/dist/store-BcU7YVhB.mjs +638 -0
- package/dist/store-Cmw9vWBT.cjs +764 -0
- package/dist/store.cjs +4 -246
- package/dist/store.d.cts +2 -69
- package/dist/store.d.mts +2 -69
- package/dist/store.mjs +1 -243
- package/package.json +24 -16
- package/dist/ref-0isFdYQ8.d.cts +0 -9
- package/dist/ref-BHYwO374.d.mts +0 -9
- package/dist/ref-DS-k2oUF.mjs +0 -271
- package/dist/ref-DtCdcykK.cjs +0 -349
|
@@ -0,0 +1,638 @@
|
|
|
1
|
+
//#region src/scheduler.ts
|
|
2
|
+
const resolvedPromise = Promise.resolve();
|
|
3
|
+
const jobQueue = /* @__PURE__ */ new Set();
|
|
4
|
+
let isFlushing = false;
|
|
5
|
+
function flushJobs() {
|
|
6
|
+
isFlushing = true;
|
|
7
|
+
try {
|
|
8
|
+
jobQueue.forEach((job) => job());
|
|
9
|
+
} finally {
|
|
10
|
+
jobQueue.clear();
|
|
11
|
+
isFlushing = false;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function queueJob(job) {
|
|
15
|
+
jobQueue.add(job);
|
|
16
|
+
if (!isFlushing) resolvedPromise.then(flushJobs);
|
|
17
|
+
}
|
|
18
|
+
function nextTick(fn) {
|
|
19
|
+
return fn ? resolvedPromise.then(fn) : resolvedPromise;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/reactivity/core.ts
|
|
24
|
+
const targetMap = /* @__PURE__ */ new WeakMap();
|
|
25
|
+
let activeEffect = null;
|
|
26
|
+
const effectStack = [];
|
|
27
|
+
function cleanupEffect(effect$1) {
|
|
28
|
+
const { deps } = effect$1;
|
|
29
|
+
for (let i = 0; i < deps.length; i++) deps[i].delete(effect$1);
|
|
30
|
+
deps.length = 0;
|
|
31
|
+
}
|
|
32
|
+
function createReactiveEffect(fn, options = {}) {
|
|
33
|
+
const effect$1 = function reactiveEffect() {
|
|
34
|
+
if (!effect$1.active) return fn();
|
|
35
|
+
if (effectStack.includes(effect$1)) return fn();
|
|
36
|
+
cleanupEffect(effect$1);
|
|
37
|
+
try {
|
|
38
|
+
effectStack.push(effect$1);
|
|
39
|
+
activeEffect = effect$1;
|
|
40
|
+
return fn();
|
|
41
|
+
} finally {
|
|
42
|
+
effectStack.pop();
|
|
43
|
+
activeEffect = effectStack[effectStack.length - 1] ?? null;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
effect$1.deps = [];
|
|
47
|
+
effect$1.scheduler = options.scheduler;
|
|
48
|
+
effect$1.onStop = options.onStop;
|
|
49
|
+
effect$1.active = true;
|
|
50
|
+
effect$1._fn = fn;
|
|
51
|
+
return effect$1;
|
|
52
|
+
}
|
|
53
|
+
function effect(fn, options = {}) {
|
|
54
|
+
const _effect = createReactiveEffect(fn, options);
|
|
55
|
+
if (!options.lazy) _effect();
|
|
56
|
+
return _effect;
|
|
57
|
+
}
|
|
58
|
+
function stop(runner) {
|
|
59
|
+
if (!runner.active) return;
|
|
60
|
+
runner.active = false;
|
|
61
|
+
cleanupEffect(runner);
|
|
62
|
+
runner.onStop?.();
|
|
63
|
+
}
|
|
64
|
+
function track(target, key) {
|
|
65
|
+
if (!activeEffect) return;
|
|
66
|
+
let depsMap = targetMap.get(target);
|
|
67
|
+
if (!depsMap) {
|
|
68
|
+
depsMap = /* @__PURE__ */ new Map();
|
|
69
|
+
targetMap.set(target, depsMap);
|
|
70
|
+
}
|
|
71
|
+
let dep = depsMap.get(key);
|
|
72
|
+
if (!dep) {
|
|
73
|
+
dep = /* @__PURE__ */ new Set();
|
|
74
|
+
depsMap.set(key, dep);
|
|
75
|
+
}
|
|
76
|
+
if (!dep.has(activeEffect)) {
|
|
77
|
+
dep.add(activeEffect);
|
|
78
|
+
activeEffect.deps.push(dep);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function trigger(target, key) {
|
|
82
|
+
const depsMap = targetMap.get(target);
|
|
83
|
+
if (!depsMap) return;
|
|
84
|
+
const effects = depsMap.get(key);
|
|
85
|
+
if (!effects) return;
|
|
86
|
+
const effectsToRun = /* @__PURE__ */ new Set();
|
|
87
|
+
effects.forEach((ef) => {
|
|
88
|
+
if (ef !== activeEffect) effectsToRun.add(ef);
|
|
89
|
+
});
|
|
90
|
+
effectsToRun.forEach((ef) => {
|
|
91
|
+
if (ef.scheduler) ef.scheduler();
|
|
92
|
+
else ef();
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
function trackEffects(dep) {
|
|
96
|
+
if (!activeEffect) return;
|
|
97
|
+
if (!dep.has(activeEffect)) {
|
|
98
|
+
dep.add(activeEffect);
|
|
99
|
+
activeEffect.deps.push(dep);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
function triggerEffects(dep) {
|
|
103
|
+
dep.forEach((ef) => {
|
|
104
|
+
if (ef.scheduler) ef.scheduler();
|
|
105
|
+
else ef();
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
//#endregion
|
|
110
|
+
//#region src/reactivity/computed.ts
|
|
111
|
+
function computed(getterOrOptions) {
|
|
112
|
+
let getter;
|
|
113
|
+
let setter;
|
|
114
|
+
const onlyGetter = typeof getterOrOptions === "function";
|
|
115
|
+
if (onlyGetter) {
|
|
116
|
+
getter = getterOrOptions;
|
|
117
|
+
setter = () => {
|
|
118
|
+
throw new Error("Computed value is readonly");
|
|
119
|
+
};
|
|
120
|
+
} else {
|
|
121
|
+
getter = getterOrOptions.get;
|
|
122
|
+
setter = getterOrOptions.set;
|
|
123
|
+
}
|
|
124
|
+
let value;
|
|
125
|
+
let dirty = true;
|
|
126
|
+
let runner;
|
|
127
|
+
const obj = {
|
|
128
|
+
get value() {
|
|
129
|
+
if (dirty) {
|
|
130
|
+
value = runner();
|
|
131
|
+
dirty = false;
|
|
132
|
+
}
|
|
133
|
+
track(obj, "value");
|
|
134
|
+
return value;
|
|
135
|
+
},
|
|
136
|
+
set value(newValue) {
|
|
137
|
+
setter(newValue);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
runner = effect(getter, {
|
|
141
|
+
lazy: true,
|
|
142
|
+
scheduler: () => {
|
|
143
|
+
if (!dirty) {
|
|
144
|
+
dirty = true;
|
|
145
|
+
trigger(obj, "value");
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
return onlyGetter ? obj : obj;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
//#endregion
|
|
153
|
+
//#region src/reactivity/reactive.ts
|
|
154
|
+
const reactiveMap = /* @__PURE__ */ new WeakMap();
|
|
155
|
+
const rawMap = /* @__PURE__ */ new WeakMap();
|
|
156
|
+
const rawRootMap = /* @__PURE__ */ new WeakMap();
|
|
157
|
+
let ReactiveFlags = /* @__PURE__ */ function(ReactiveFlags$1) {
|
|
158
|
+
ReactiveFlags$1["IS_REACTIVE"] = "__r_isReactive";
|
|
159
|
+
ReactiveFlags$1["RAW"] = "__r_raw";
|
|
160
|
+
ReactiveFlags$1["SKIP"] = "__r_skip";
|
|
161
|
+
return ReactiveFlags$1;
|
|
162
|
+
}({});
|
|
163
|
+
function isObject$1(value) {
|
|
164
|
+
return typeof value === "object" && value !== null;
|
|
165
|
+
}
|
|
166
|
+
const VERSION_KEY = Symbol("wevu.version");
|
|
167
|
+
const mutableHandlers = {
|
|
168
|
+
get(target, key, receiver) {
|
|
169
|
+
if (key === ReactiveFlags.IS_REACTIVE) return true;
|
|
170
|
+
if (key === ReactiveFlags.RAW) return target;
|
|
171
|
+
const res = Reflect.get(target, key, receiver);
|
|
172
|
+
track(target, key);
|
|
173
|
+
if (isObject$1(res)) {
|
|
174
|
+
if (res[ReactiveFlags.SKIP]) return res;
|
|
175
|
+
const child = res;
|
|
176
|
+
const parentRoot = rawRootMap.get(target) ?? target;
|
|
177
|
+
if (!rawRootMap.has(child)) rawRootMap.set(child, parentRoot);
|
|
178
|
+
return reactive(res);
|
|
179
|
+
}
|
|
180
|
+
return res;
|
|
181
|
+
},
|
|
182
|
+
set(target, key, value, receiver) {
|
|
183
|
+
const oldValue = Reflect.get(target, key, receiver);
|
|
184
|
+
const result = Reflect.set(target, key, value, receiver);
|
|
185
|
+
if (!Object.is(oldValue, value)) {
|
|
186
|
+
trigger(target, key);
|
|
187
|
+
trigger(target, VERSION_KEY);
|
|
188
|
+
const root = rawRootMap.get(target);
|
|
189
|
+
if (root && root !== target) trigger(root, VERSION_KEY);
|
|
190
|
+
}
|
|
191
|
+
return result;
|
|
192
|
+
},
|
|
193
|
+
deleteProperty(target, key) {
|
|
194
|
+
const hadKey = Object.prototype.hasOwnProperty.call(target, key);
|
|
195
|
+
const result = Reflect.deleteProperty(target, key);
|
|
196
|
+
if (hadKey && result) {
|
|
197
|
+
trigger(target, key);
|
|
198
|
+
trigger(target, VERSION_KEY);
|
|
199
|
+
const root = rawRootMap.get(target);
|
|
200
|
+
if (root && root !== target) trigger(root, VERSION_KEY);
|
|
201
|
+
}
|
|
202
|
+
return result;
|
|
203
|
+
},
|
|
204
|
+
ownKeys(target) {
|
|
205
|
+
track(target, Symbol.iterator);
|
|
206
|
+
track(target, VERSION_KEY);
|
|
207
|
+
return Reflect.ownKeys(target);
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
function reactive(target) {
|
|
211
|
+
if (!isObject$1(target)) return target;
|
|
212
|
+
const existingProxy = reactiveMap.get(target);
|
|
213
|
+
if (existingProxy) return existingProxy;
|
|
214
|
+
if (target[ReactiveFlags.IS_REACTIVE]) return target;
|
|
215
|
+
const proxy = new Proxy(target, mutableHandlers);
|
|
216
|
+
reactiveMap.set(target, proxy);
|
|
217
|
+
rawMap.set(proxy, target);
|
|
218
|
+
if (!rawRootMap.has(target)) rawRootMap.set(target, target);
|
|
219
|
+
return proxy;
|
|
220
|
+
}
|
|
221
|
+
function isReactive(value) {
|
|
222
|
+
return Boolean(value && value[ReactiveFlags.IS_REACTIVE]);
|
|
223
|
+
}
|
|
224
|
+
function toRaw(observed) {
|
|
225
|
+
return observed?.[ReactiveFlags.RAW] ?? observed;
|
|
226
|
+
}
|
|
227
|
+
function convertToReactive(value) {
|
|
228
|
+
return isObject$1(value) ? reactive(value) : value;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Establish a dependency on the whole reactive object "version".
|
|
232
|
+
* 让 effect 订阅整个对象的“版本号”,无需深度遍历即可对任何字段变化做出响应。
|
|
233
|
+
*/
|
|
234
|
+
function touchReactive(target) {
|
|
235
|
+
track(toRaw(target), VERSION_KEY);
|
|
236
|
+
}
|
|
237
|
+
const shallowReactiveMap = /* @__PURE__ */ new WeakMap();
|
|
238
|
+
const shallowHandlers = {
|
|
239
|
+
get(target, key, receiver) {
|
|
240
|
+
if (key === ReactiveFlags.IS_REACTIVE) return true;
|
|
241
|
+
if (key === ReactiveFlags.RAW) return target;
|
|
242
|
+
const res = Reflect.get(target, key, receiver);
|
|
243
|
+
track(target, key);
|
|
244
|
+
return res;
|
|
245
|
+
},
|
|
246
|
+
set(target, key, value, receiver) {
|
|
247
|
+
const oldValue = Reflect.get(target, key, receiver);
|
|
248
|
+
const result = Reflect.set(target, key, value, receiver);
|
|
249
|
+
if (!Object.is(oldValue, value)) {
|
|
250
|
+
trigger(target, key);
|
|
251
|
+
trigger(target, VERSION_KEY);
|
|
252
|
+
}
|
|
253
|
+
return result;
|
|
254
|
+
},
|
|
255
|
+
deleteProperty(target, key) {
|
|
256
|
+
const hadKey = Object.prototype.hasOwnProperty.call(target, key);
|
|
257
|
+
const result = Reflect.deleteProperty(target, key);
|
|
258
|
+
if (hadKey && result) {
|
|
259
|
+
trigger(target, key);
|
|
260
|
+
trigger(target, VERSION_KEY);
|
|
261
|
+
}
|
|
262
|
+
return result;
|
|
263
|
+
},
|
|
264
|
+
ownKeys(target) {
|
|
265
|
+
track(target, Symbol.iterator);
|
|
266
|
+
track(target, VERSION_KEY);
|
|
267
|
+
return Reflect.ownKeys(target);
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
/**
|
|
271
|
+
* 创建一个浅层响应式代理:仅跟踪第一层属性变更,不深度递归嵌套对象。
|
|
272
|
+
*
|
|
273
|
+
* @param target 待转换的对象
|
|
274
|
+
* @returns 浅层响应式代理
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* ```ts
|
|
278
|
+
* const state = shallowReactive({ nested: { count: 0 } })
|
|
279
|
+
*
|
|
280
|
+
* state.nested.count++ // 不会触发 effect(嵌套对象未深度代理)
|
|
281
|
+
* state.nested = { count: 1 } // 会触发 effect(顶层属性变更)
|
|
282
|
+
* ```
|
|
283
|
+
*/
|
|
284
|
+
function shallowReactive(target) {
|
|
285
|
+
if (!isObject$1(target)) return target;
|
|
286
|
+
const existingProxy = shallowReactiveMap.get(target);
|
|
287
|
+
if (existingProxy) return existingProxy;
|
|
288
|
+
if (target[ReactiveFlags.IS_REACTIVE]) return target;
|
|
289
|
+
const proxy = new Proxy(target, shallowHandlers);
|
|
290
|
+
shallowReactiveMap.set(target, proxy);
|
|
291
|
+
rawMap.set(proxy, target);
|
|
292
|
+
return proxy;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Checks if a value is a shallow reactive object
|
|
296
|
+
*/
|
|
297
|
+
function isShallowReactive(value) {
|
|
298
|
+
const raw = toRaw(value);
|
|
299
|
+
return shallowReactiveMap.has(raw);
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* 标记对象为“原始”状态,后续不会被转换为响应式,返回原对象本身。
|
|
303
|
+
*
|
|
304
|
+
* @param value 需要标记的对象
|
|
305
|
+
* @returns 带有跳过标记的原对象
|
|
306
|
+
*
|
|
307
|
+
* @example
|
|
308
|
+
* ```ts
|
|
309
|
+
* const foo = markRaw({
|
|
310
|
+
* nested: {}
|
|
311
|
+
* })
|
|
312
|
+
*
|
|
313
|
+
* const state = reactive({
|
|
314
|
+
* foo
|
|
315
|
+
* })
|
|
316
|
+
*
|
|
317
|
+
* state.foo // 不是响应式对象
|
|
318
|
+
* ```
|
|
319
|
+
*/
|
|
320
|
+
function markRaw(value) {
|
|
321
|
+
if (!isObject$1(value)) return value;
|
|
322
|
+
Object.defineProperty(value, ReactiveFlags.SKIP, {
|
|
323
|
+
value: true,
|
|
324
|
+
configurable: true,
|
|
325
|
+
enumerable: false,
|
|
326
|
+
writable: true
|
|
327
|
+
});
|
|
328
|
+
return value;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* 判断某个值是否被标记为原始(即不应转换为响应式)。
|
|
332
|
+
*
|
|
333
|
+
* @param value 待检测的值
|
|
334
|
+
* @returns 若含有跳过标记则返回 true
|
|
335
|
+
*/
|
|
336
|
+
function isRaw(value) {
|
|
337
|
+
return isObject$1(value) && ReactiveFlags.SKIP in value;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
//#endregion
|
|
341
|
+
//#region src/reactivity/ref.ts
|
|
342
|
+
function isRef(value) {
|
|
343
|
+
return Boolean(value && typeof value === "object" && "value" in value);
|
|
344
|
+
}
|
|
345
|
+
var RefImpl = class {
|
|
346
|
+
_value;
|
|
347
|
+
_rawValue;
|
|
348
|
+
dep;
|
|
349
|
+
constructor(value) {
|
|
350
|
+
this._rawValue = value;
|
|
351
|
+
this._value = convertToReactive(value);
|
|
352
|
+
}
|
|
353
|
+
get value() {
|
|
354
|
+
if (!this.dep) this.dep = /* @__PURE__ */ new Set();
|
|
355
|
+
trackEffects(this.dep);
|
|
356
|
+
return this._value;
|
|
357
|
+
}
|
|
358
|
+
set value(newValue) {
|
|
359
|
+
if (!Object.is(newValue, this._rawValue)) {
|
|
360
|
+
this._rawValue = newValue;
|
|
361
|
+
this._value = convertToReactive(newValue);
|
|
362
|
+
if (this.dep) triggerEffects(this.dep);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
function ref(value) {
|
|
367
|
+
if (isRef(value)) return value;
|
|
368
|
+
return new RefImpl(value);
|
|
369
|
+
}
|
|
370
|
+
function unref(value) {
|
|
371
|
+
return isRef(value) ? value.value : value;
|
|
372
|
+
}
|
|
373
|
+
var CustomRefImpl = class {
|
|
374
|
+
_value;
|
|
375
|
+
_factory;
|
|
376
|
+
dep;
|
|
377
|
+
constructor(factory, defaultValue) {
|
|
378
|
+
this._factory = factory;
|
|
379
|
+
this._value = defaultValue;
|
|
380
|
+
}
|
|
381
|
+
get value() {
|
|
382
|
+
if (!this.dep) this.dep = /* @__PURE__ */ new Set();
|
|
383
|
+
trackEffects(this.dep);
|
|
384
|
+
return this._factory.get();
|
|
385
|
+
}
|
|
386
|
+
set value(newValue) {
|
|
387
|
+
this._factory.set(newValue);
|
|
388
|
+
if (this.dep) triggerEffects(this.dep);
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
function customRef(factory, defaultValue) {
|
|
392
|
+
return new CustomRefImpl(factory, defaultValue);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
//#endregion
|
|
396
|
+
//#region src/store/actions.ts
|
|
397
|
+
function wrapAction(store, name, action, actionSubs) {
|
|
398
|
+
return function wrapped(...args) {
|
|
399
|
+
const afterCbs = [];
|
|
400
|
+
const errorCbs = [];
|
|
401
|
+
const after = (cb) => afterCbs.push(cb);
|
|
402
|
+
const onError = (cb) => errorCbs.push(cb);
|
|
403
|
+
actionSubs.forEach((sub) => {
|
|
404
|
+
try {
|
|
405
|
+
sub({
|
|
406
|
+
name,
|
|
407
|
+
store,
|
|
408
|
+
args,
|
|
409
|
+
after,
|
|
410
|
+
onError
|
|
411
|
+
});
|
|
412
|
+
} catch {}
|
|
413
|
+
});
|
|
414
|
+
let res;
|
|
415
|
+
try {
|
|
416
|
+
res = action.apply(store, args);
|
|
417
|
+
} catch (e) {
|
|
418
|
+
errorCbs.forEach((cb) => cb(e));
|
|
419
|
+
throw e;
|
|
420
|
+
}
|
|
421
|
+
const finalize = (r) => {
|
|
422
|
+
afterCbs.forEach((cb) => cb(r));
|
|
423
|
+
return r;
|
|
424
|
+
};
|
|
425
|
+
if (res && typeof res.then === "function") return res.then((r) => finalize(r), (e) => {
|
|
426
|
+
errorCbs.forEach((cb) => cb(e));
|
|
427
|
+
return Promise.reject(e);
|
|
428
|
+
});
|
|
429
|
+
return finalize(res);
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
//#endregion
|
|
434
|
+
//#region src/store/utils.ts
|
|
435
|
+
function isObject(val) {
|
|
436
|
+
return typeof val === "object" && val !== null;
|
|
437
|
+
}
|
|
438
|
+
function mergeShallow(target, patch) {
|
|
439
|
+
for (const k in patch) target[k] = patch[k];
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
//#endregion
|
|
443
|
+
//#region src/store/base.ts
|
|
444
|
+
function createBaseApi(id, stateObj, notify, resetImpl) {
|
|
445
|
+
const api = { $id: id };
|
|
446
|
+
Object.defineProperty(api, "$state", {
|
|
447
|
+
get() {
|
|
448
|
+
return stateObj;
|
|
449
|
+
},
|
|
450
|
+
set(v) {
|
|
451
|
+
if (stateObj && isObject(v)) {
|
|
452
|
+
mergeShallow(stateObj, v);
|
|
453
|
+
notify("patch object");
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
});
|
|
457
|
+
api.$patch = (patch) => {
|
|
458
|
+
if (!stateObj) {
|
|
459
|
+
if (typeof patch === "function") {
|
|
460
|
+
patch(api);
|
|
461
|
+
notify("patch function");
|
|
462
|
+
} else {
|
|
463
|
+
mergeShallow(api, patch);
|
|
464
|
+
notify("patch object");
|
|
465
|
+
}
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
if (typeof patch === "function") {
|
|
469
|
+
patch(stateObj);
|
|
470
|
+
notify("patch function");
|
|
471
|
+
} else {
|
|
472
|
+
mergeShallow(stateObj, patch);
|
|
473
|
+
notify("patch object");
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
if (resetImpl) api.$reset = () => resetImpl();
|
|
477
|
+
const subs = /* @__PURE__ */ new Set();
|
|
478
|
+
api.$subscribe = (cb, _opts) => {
|
|
479
|
+
subs.add(cb);
|
|
480
|
+
return () => subs.delete(cb);
|
|
481
|
+
};
|
|
482
|
+
const actionSubs = /* @__PURE__ */ new Set();
|
|
483
|
+
api.$onAction = (cb) => {
|
|
484
|
+
actionSubs.add(cb);
|
|
485
|
+
return () => actionSubs.delete(cb);
|
|
486
|
+
};
|
|
487
|
+
return {
|
|
488
|
+
api,
|
|
489
|
+
subs,
|
|
490
|
+
actionSubs
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
//#endregion
|
|
495
|
+
//#region src/store/manager.ts
|
|
496
|
+
function createStore() {
|
|
497
|
+
const manager = {
|
|
498
|
+
_stores: /* @__PURE__ */ new Map(),
|
|
499
|
+
_plugins: [],
|
|
500
|
+
install(_app) {},
|
|
501
|
+
use(plugin) {
|
|
502
|
+
if (typeof plugin === "function") manager._plugins.push(plugin);
|
|
503
|
+
return manager;
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
createStore._instance = manager;
|
|
507
|
+
return manager;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
//#endregion
|
|
511
|
+
//#region src/store/define.ts
|
|
512
|
+
function defineStore(id, setupOrOptions) {
|
|
513
|
+
let instance;
|
|
514
|
+
let created = false;
|
|
515
|
+
const manager = createStore._instance;
|
|
516
|
+
return function useStore() {
|
|
517
|
+
if (created && instance) return instance;
|
|
518
|
+
created = true;
|
|
519
|
+
if (typeof setupOrOptions === "function") {
|
|
520
|
+
const result = setupOrOptions();
|
|
521
|
+
let notify$1 = () => {};
|
|
522
|
+
const base$1 = createBaseApi(id, void 0, (t) => notify$1(t));
|
|
523
|
+
notify$1 = (type) => {
|
|
524
|
+
base$1.subs.forEach((cb) => {
|
|
525
|
+
try {
|
|
526
|
+
cb({
|
|
527
|
+
type,
|
|
528
|
+
storeId: id
|
|
529
|
+
}, instance);
|
|
530
|
+
} catch {}
|
|
531
|
+
});
|
|
532
|
+
};
|
|
533
|
+
instance = Object.assign({}, result);
|
|
534
|
+
for (const key of Object.getOwnPropertyNames(base$1.api)) {
|
|
535
|
+
const d = Object.getOwnPropertyDescriptor(base$1.api, key);
|
|
536
|
+
if (d) Object.defineProperty(instance, key, d);
|
|
537
|
+
}
|
|
538
|
+
Object.keys(result).forEach((k) => {
|
|
539
|
+
const val = result[k];
|
|
540
|
+
if (typeof val === "function" && !k.startsWith("$")) instance[k] = wrapAction(instance, k, val, base$1.actionSubs);
|
|
541
|
+
});
|
|
542
|
+
const plugins$1 = manager?._plugins ?? [];
|
|
543
|
+
for (const plugin of plugins$1) try {
|
|
544
|
+
plugin({ store: instance });
|
|
545
|
+
} catch {}
|
|
546
|
+
return instance;
|
|
547
|
+
}
|
|
548
|
+
const options = setupOrOptions;
|
|
549
|
+
const rawState = options.state ? options.state() : {};
|
|
550
|
+
const state = reactive(rawState);
|
|
551
|
+
const initialSnapshot = { ...toRaw(rawState) };
|
|
552
|
+
let notify = () => {};
|
|
553
|
+
const base = createBaseApi(id, state, (t) => notify(t), () => {
|
|
554
|
+
mergeShallow(state, initialSnapshot);
|
|
555
|
+
notify("patch object");
|
|
556
|
+
});
|
|
557
|
+
notify = (type) => {
|
|
558
|
+
base.subs.forEach((cb) => {
|
|
559
|
+
try {
|
|
560
|
+
cb({
|
|
561
|
+
type,
|
|
562
|
+
storeId: id
|
|
563
|
+
}, state);
|
|
564
|
+
} catch {}
|
|
565
|
+
});
|
|
566
|
+
};
|
|
567
|
+
const store = {};
|
|
568
|
+
for (const key of Object.getOwnPropertyNames(base.api)) {
|
|
569
|
+
const d = Object.getOwnPropertyDescriptor(base.api, key);
|
|
570
|
+
if (d) Object.defineProperty(store, key, d);
|
|
571
|
+
}
|
|
572
|
+
const getterDefs = options.getters ?? {};
|
|
573
|
+
const computedMap = {};
|
|
574
|
+
Object.keys(getterDefs).forEach((key) => {
|
|
575
|
+
const getter = getterDefs[key];
|
|
576
|
+
if (typeof getter === "function") {
|
|
577
|
+
const c = computed(() => getter.call(store, state));
|
|
578
|
+
computedMap[key] = c;
|
|
579
|
+
Object.defineProperty(store, key, {
|
|
580
|
+
enumerable: true,
|
|
581
|
+
configurable: true,
|
|
582
|
+
get() {
|
|
583
|
+
return c.value;
|
|
584
|
+
}
|
|
585
|
+
});
|
|
586
|
+
}
|
|
587
|
+
});
|
|
588
|
+
const actionDefs = options.actions ?? {};
|
|
589
|
+
Object.keys(actionDefs).forEach((key) => {
|
|
590
|
+
const act = actionDefs[key];
|
|
591
|
+
if (typeof act === "function") store[key] = wrapAction(store, key, (...args) => {
|
|
592
|
+
return act.apply(store, args);
|
|
593
|
+
}, base.actionSubs);
|
|
594
|
+
});
|
|
595
|
+
Object.keys(state).forEach((k) => {
|
|
596
|
+
Object.defineProperty(store, k, {
|
|
597
|
+
enumerable: true,
|
|
598
|
+
configurable: true,
|
|
599
|
+
get() {
|
|
600
|
+
return state[k];
|
|
601
|
+
},
|
|
602
|
+
set(v) {
|
|
603
|
+
state[k] = v;
|
|
604
|
+
}
|
|
605
|
+
});
|
|
606
|
+
});
|
|
607
|
+
instance = store;
|
|
608
|
+
const plugins = manager?._plugins ?? [];
|
|
609
|
+
for (const plugin of plugins) try {
|
|
610
|
+
plugin({ store: instance });
|
|
611
|
+
} catch {}
|
|
612
|
+
return instance;
|
|
613
|
+
};
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
//#endregion
|
|
617
|
+
//#region src/store/storeToRefs.ts
|
|
618
|
+
function storeToRefs(store) {
|
|
619
|
+
const result = {};
|
|
620
|
+
for (const key in store) {
|
|
621
|
+
const value = store[key];
|
|
622
|
+
if (typeof value === "function") {
|
|
623
|
+
result[key] = value;
|
|
624
|
+
continue;
|
|
625
|
+
}
|
|
626
|
+
if (isRef(value)) result[key] = value;
|
|
627
|
+
else result[key] = computed({
|
|
628
|
+
get: () => store[key],
|
|
629
|
+
set: (v) => {
|
|
630
|
+
store[key] = v;
|
|
631
|
+
}
|
|
632
|
+
});
|
|
633
|
+
}
|
|
634
|
+
return result;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
//#endregion
|
|
638
|
+
export { computed as _, isRef as a, nextTick as b, isObject$1 as c, isShallowReactive as d, markRaw as f, touchReactive as g, toRaw as h, customRef as i, isRaw as l, shallowReactive as m, defineStore as n, ref as o, reactive as p, createStore as r, unref as s, storeToRefs as t, isReactive as u, effect as v, queueJob as x, stop as y };
|