wevu 6.7.2 → 6.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,504 @@
1
+ import { A as track, C as effect, d as reactive, g as touchReactive, j as trigger, l as isReactive, n as isRef, r as markAsRef, y as toRaw } from "./ref-BeA_Is-2.mjs";
2
+ //#region src/reactivity/computed.ts
3
+ function computed(getterOrOptions) {
4
+ let getter;
5
+ let setter;
6
+ const onlyGetter = typeof getterOrOptions === "function";
7
+ if (onlyGetter) {
8
+ getter = getterOrOptions;
9
+ setter = () => {
10
+ throw new Error("计算属性是只读的");
11
+ };
12
+ } else {
13
+ getter = getterOrOptions.get;
14
+ setter = getterOrOptions.set;
15
+ }
16
+ let value;
17
+ let dirty = true;
18
+ let runner;
19
+ const obj = {
20
+ get value() {
21
+ if (dirty) {
22
+ value = runner();
23
+ dirty = false;
24
+ }
25
+ track(obj, "value");
26
+ return value;
27
+ },
28
+ set value(newValue) {
29
+ setter(newValue);
30
+ }
31
+ };
32
+ markAsRef(obj);
33
+ runner = effect(getter, {
34
+ lazy: true,
35
+ scheduler: () => {
36
+ if (!dirty) {
37
+ dirty = true;
38
+ trigger(obj, "value");
39
+ }
40
+ }
41
+ });
42
+ return onlyGetter ? obj : obj;
43
+ }
44
+ //#endregion
45
+ //#region src/store/actions.ts
46
+ function wrapAction(store, name, action, actionSubs) {
47
+ return function wrapped(...args) {
48
+ const afterCbs = [];
49
+ const errorCbs = [];
50
+ const after = (cb) => afterCbs.push(cb);
51
+ const onError = (cb) => errorCbs.push(cb);
52
+ actionSubs.forEach((sub) => {
53
+ try {
54
+ sub({
55
+ name,
56
+ store,
57
+ args,
58
+ after,
59
+ onError
60
+ });
61
+ } catch (_unused) {}
62
+ });
63
+ let res;
64
+ try {
65
+ res = action.apply(store, args);
66
+ } catch (e) {
67
+ errorCbs.forEach((cb) => cb(e));
68
+ throw e;
69
+ }
70
+ const finalize = (r) => {
71
+ afterCbs.forEach((cb) => cb(r));
72
+ return r;
73
+ };
74
+ if (res && typeof res.then === "function") return res.then((r) => finalize(r), (e) => {
75
+ errorCbs.forEach((cb) => cb(e));
76
+ return Promise.reject(e);
77
+ });
78
+ return finalize(res);
79
+ };
80
+ }
81
+ //#endregion
82
+ //#region src/store/utils.ts
83
+ function isObject(val) {
84
+ return typeof val === "object" && val !== null;
85
+ }
86
+ function isPlainObject(val) {
87
+ if (!isObject(val)) return false;
88
+ const proto = Object.getPrototypeOf(val);
89
+ return proto === Object.prototype || proto === null;
90
+ }
91
+ function mergeShallow(target, patch) {
92
+ for (const k in patch) target[k] = patch[k];
93
+ }
94
+ function cloneDeep(value) {
95
+ if (Array.isArray(value)) return value.map((item) => cloneDeep(item));
96
+ if (isPlainObject(value)) {
97
+ const result = {};
98
+ for (const key in value) result[key] = cloneDeep(value[key]);
99
+ return result;
100
+ }
101
+ return value;
102
+ }
103
+ function resetObject(target, snapshot) {
104
+ if (Array.isArray(target) && Array.isArray(snapshot)) {
105
+ target.length = 0;
106
+ snapshot.forEach((item, index) => {
107
+ target[index] = cloneDeep(item);
108
+ });
109
+ return;
110
+ }
111
+ if (!isObject(snapshot)) return;
112
+ for (const key in target) if (!(key in snapshot)) delete target[key];
113
+ for (const key in snapshot) {
114
+ const snapValue = snapshot[key];
115
+ const currentValue = target[key];
116
+ if (Array.isArray(snapValue) && Array.isArray(currentValue)) {
117
+ resetObject(currentValue, snapValue);
118
+ continue;
119
+ }
120
+ if (isPlainObject(snapValue) && isPlainObject(currentValue)) {
121
+ resetObject(currentValue, snapValue);
122
+ continue;
123
+ }
124
+ target[key] = cloneDeep(snapValue);
125
+ }
126
+ }
127
+ //#endregion
128
+ //#region src/store/base.ts
129
+ function createBaseApi(id, stateObj, notify, resetImpl) {
130
+ const api = { $id: id };
131
+ Object.defineProperty(api, "$state", {
132
+ get() {
133
+ return stateObj;
134
+ },
135
+ set(v) {
136
+ if (stateObj && isObject(v)) {
137
+ mergeShallow(stateObj, v);
138
+ notify("patch object");
139
+ }
140
+ }
141
+ });
142
+ api.$patch = (patch) => {
143
+ if (!stateObj) {
144
+ if (typeof patch === "function") {
145
+ patch(api);
146
+ notify("patch function");
147
+ } else {
148
+ mergeShallow(api, patch);
149
+ notify("patch object");
150
+ }
151
+ return;
152
+ }
153
+ if (typeof patch === "function") {
154
+ patch(stateObj);
155
+ notify("patch function");
156
+ } else {
157
+ mergeShallow(stateObj, patch);
158
+ notify("patch object");
159
+ }
160
+ };
161
+ if (resetImpl) api.$reset = () => resetImpl();
162
+ const subs = /* @__PURE__ */ new Set();
163
+ api.$subscribe = (cb, _opts) => {
164
+ subs.add(cb);
165
+ return () => subs.delete(cb);
166
+ };
167
+ const actionSubs = /* @__PURE__ */ new Set();
168
+ api.$onAction = (cb) => {
169
+ actionSubs.add(cb);
170
+ return () => actionSubs.delete(cb);
171
+ };
172
+ return {
173
+ api,
174
+ subs,
175
+ actionSubs
176
+ };
177
+ }
178
+ //#endregion
179
+ //#region src/store/define/shared.ts
180
+ const hasOwn = Object.prototype.hasOwnProperty;
181
+ function isTrackableRef(value) {
182
+ return isRef(value) && hasOwn.call(value, "dep");
183
+ }
184
+ function snapshotValue(value) {
185
+ if (isReactive(value)) return cloneDeep(toRaw(value));
186
+ if (isTrackableRef(value)) return cloneDeep(value.value);
187
+ return cloneDeep(value);
188
+ }
189
+ function createSafeNotifier(storeId, subs, getState) {
190
+ let notifying = false;
191
+ return (type) => {
192
+ if (notifying) return;
193
+ notifying = true;
194
+ try {
195
+ const state = getState();
196
+ subs.forEach((cb) => {
197
+ try {
198
+ cb({
199
+ type,
200
+ storeId
201
+ }, state);
202
+ } catch (_unused) {}
203
+ });
204
+ } finally {
205
+ notifying = false;
206
+ }
207
+ };
208
+ }
209
+ //#endregion
210
+ //#region src/store/define/optionsStyle.ts
211
+ function createOptionsStyleStore(id, options, manager) {
212
+ var _options$getters, _options$actions, _manager$_plugins;
213
+ const rawState = options.state ? options.state() : {};
214
+ const state = reactive(rawState);
215
+ const initialSnapshot = cloneDeep(toRaw(rawState));
216
+ let notify = () => {};
217
+ const base = createBaseApi(id, state, (t) => notify(t), () => {
218
+ resetObject(state, initialSnapshot);
219
+ notify("patch object");
220
+ });
221
+ let isPatching = false;
222
+ const rawPatch = base.api.$patch;
223
+ base.api.$patch = (patch) => {
224
+ isPatching = true;
225
+ try {
226
+ rawPatch(patch);
227
+ } finally {
228
+ isPatching = false;
229
+ }
230
+ };
231
+ if (typeof base.api.$reset === "function") {
232
+ const rawReset = base.api.$reset;
233
+ base.api.$reset = () => {
234
+ isPatching = true;
235
+ try {
236
+ rawReset();
237
+ } finally {
238
+ isPatching = false;
239
+ }
240
+ };
241
+ }
242
+ notify = createSafeNotifier(id, base.subs, () => state);
243
+ const store = {};
244
+ for (const key of Object.getOwnPropertyNames(base.api)) {
245
+ const d = Object.getOwnPropertyDescriptor(base.api, key);
246
+ if (d) if (key === "$state") Object.defineProperty(store, key, {
247
+ enumerable: d.enumerable,
248
+ configurable: d.configurable,
249
+ get() {
250
+ return base.api.$state;
251
+ },
252
+ set(v) {
253
+ isPatching = true;
254
+ try {
255
+ base.api.$state = v;
256
+ } finally {
257
+ isPatching = false;
258
+ }
259
+ }
260
+ });
261
+ else Object.defineProperty(store, key, d);
262
+ }
263
+ const getterDefs = (_options$getters = options.getters) !== null && _options$getters !== void 0 ? _options$getters : {};
264
+ Object.keys(getterDefs).forEach((key) => {
265
+ const getter = getterDefs[key];
266
+ if (typeof getter === "function") {
267
+ const c = computed(() => getter.call(store, state));
268
+ Object.defineProperty(store, key, {
269
+ enumerable: true,
270
+ configurable: true,
271
+ get() {
272
+ return c.value;
273
+ }
274
+ });
275
+ }
276
+ });
277
+ const actionDefs = (_options$actions = options.actions) !== null && _options$actions !== void 0 ? _options$actions : {};
278
+ Object.keys(actionDefs).forEach((key) => {
279
+ const act = actionDefs[key];
280
+ if (typeof act === "function") store[key] = wrapAction(store, key, (...args) => {
281
+ return act.apply(store, args);
282
+ }, base.actionSubs);
283
+ });
284
+ Object.keys(state).forEach((k) => {
285
+ Object.defineProperty(store, k, {
286
+ enumerable: true,
287
+ configurable: true,
288
+ get() {
289
+ return state[k];
290
+ },
291
+ set(v) {
292
+ state[k] = v;
293
+ }
294
+ });
295
+ });
296
+ let initialized = false;
297
+ let dispatchingDirect = false;
298
+ effect(() => {
299
+ touchReactive(state);
300
+ if (!initialized) {
301
+ initialized = true;
302
+ return;
303
+ }
304
+ if (isPatching || dispatchingDirect) return;
305
+ dispatchingDirect = true;
306
+ try {
307
+ notify("direct");
308
+ } finally {
309
+ dispatchingDirect = false;
310
+ }
311
+ });
312
+ const plugins = (_manager$_plugins = manager === null || manager === void 0 ? void 0 : manager._plugins) !== null && _manager$_plugins !== void 0 ? _manager$_plugins : [];
313
+ for (const plugin of plugins) try {
314
+ plugin({ store });
315
+ } catch (_unused) {}
316
+ return store;
317
+ }
318
+ //#endregion
319
+ //#region src/store/define/setupStyle.ts
320
+ function createSetupStyleStore(id, setupFactory, manager) {
321
+ var _manager$_plugins;
322
+ const result = setupFactory();
323
+ let notify = () => {};
324
+ const initialSnapshot = /* @__PURE__ */ new Map();
325
+ Object.keys(result).forEach((k) => {
326
+ const val = result[k];
327
+ if (typeof val === "function" || k.startsWith("$")) return;
328
+ if (isRef(val) && !isTrackableRef(val)) return;
329
+ initialSnapshot.set(k, snapshotValue(val));
330
+ });
331
+ let instance = {};
332
+ const resetImpl = () => {
333
+ initialSnapshot.forEach((snapValue, key) => {
334
+ const current = instance[key];
335
+ if (isTrackableRef(current)) {
336
+ current.value = cloneDeep(snapValue);
337
+ return;
338
+ }
339
+ if (isReactive(current)) {
340
+ resetObject(current, snapValue);
341
+ return;
342
+ }
343
+ if (isRef(current)) return;
344
+ instance[key] = cloneDeep(snapValue);
345
+ });
346
+ notify("patch object");
347
+ };
348
+ const base = createBaseApi(id, void 0, (t) => notify(t), resetImpl);
349
+ let isPatching = false;
350
+ const rawPatch = base.api.$patch;
351
+ base.api.$patch = (patch) => {
352
+ isPatching = true;
353
+ try {
354
+ rawPatch(patch);
355
+ } finally {
356
+ isPatching = false;
357
+ }
358
+ };
359
+ if (typeof base.api.$reset === "function") {
360
+ const rawReset = base.api.$reset;
361
+ base.api.$reset = () => {
362
+ isPatching = true;
363
+ try {
364
+ rawReset();
365
+ } finally {
366
+ isPatching = false;
367
+ }
368
+ };
369
+ }
370
+ instance = Object.assign({}, result);
371
+ notify = createSafeNotifier(id, base.subs, () => instance);
372
+ for (const key of Object.getOwnPropertyNames(base.api)) {
373
+ const d = Object.getOwnPropertyDescriptor(base.api, key);
374
+ if (d) if (key === "$state") Object.defineProperty(instance, key, {
375
+ enumerable: d.enumerable,
376
+ configurable: d.configurable,
377
+ get() {
378
+ return base.api.$state;
379
+ },
380
+ set(v) {
381
+ isPatching = true;
382
+ try {
383
+ base.api.$state = v;
384
+ } finally {
385
+ isPatching = false;
386
+ }
387
+ }
388
+ });
389
+ else Object.defineProperty(instance, key, d);
390
+ }
391
+ const directSources = [];
392
+ Object.keys(result).forEach((k) => {
393
+ const val = result[k];
394
+ if (typeof val === "function" && !k.startsWith("$")) {
395
+ instance[k] = wrapAction(instance, k, val, base.actionSubs);
396
+ return;
397
+ }
398
+ if (isTrackableRef(val)) {
399
+ directSources.push(val);
400
+ return;
401
+ }
402
+ if (isReactive(val)) {
403
+ directSources.push(val);
404
+ return;
405
+ }
406
+ if (isRef(val)) return;
407
+ if (!k.startsWith("$")) {
408
+ let innerValue = val;
409
+ Object.defineProperty(instance, k, {
410
+ enumerable: true,
411
+ configurable: true,
412
+ get() {
413
+ return innerValue;
414
+ },
415
+ set(next) {
416
+ innerValue = next;
417
+ if (!isPatching) notify("direct");
418
+ }
419
+ });
420
+ }
421
+ });
422
+ let dispatchingDirect = false;
423
+ if (directSources.length > 0) {
424
+ let initialized = false;
425
+ effect(() => {
426
+ directSources.forEach((source) => {
427
+ if (isTrackableRef(source)) source.value;
428
+ else touchReactive(source);
429
+ });
430
+ if (!initialized) {
431
+ initialized = true;
432
+ return;
433
+ }
434
+ if (isPatching || dispatchingDirect) return;
435
+ dispatchingDirect = true;
436
+ try {
437
+ notify("direct");
438
+ } finally {
439
+ dispatchingDirect = false;
440
+ }
441
+ });
442
+ }
443
+ const plugins = (_manager$_plugins = manager === null || manager === void 0 ? void 0 : manager._plugins) !== null && _manager$_plugins !== void 0 ? _manager$_plugins : [];
444
+ for (const plugin of plugins) try {
445
+ plugin({ store: instance });
446
+ } catch (_unused) {}
447
+ return instance;
448
+ }
449
+ //#endregion
450
+ //#region src/store/manager.ts
451
+ /**
452
+ * @description 创建 store 管理器(插件与共享实例入口)
453
+ */
454
+ function createStore() {
455
+ const manager = {
456
+ _stores: /* @__PURE__ */ new Map(),
457
+ _plugins: [],
458
+ install(_app) {},
459
+ use(plugin) {
460
+ if (typeof plugin === "function") manager._plugins.push(plugin);
461
+ return manager;
462
+ }
463
+ };
464
+ createStore._instance = manager;
465
+ return manager;
466
+ }
467
+ //#endregion
468
+ //#region src/store/define.ts
469
+ function defineStore(id, setupOrOptions) {
470
+ let instance;
471
+ let created = false;
472
+ const manager = createStore._instance;
473
+ return function useStore() {
474
+ if (created && instance) return instance;
475
+ created = true;
476
+ instance = typeof setupOrOptions === "function" ? createSetupStyleStore(id, setupOrOptions, manager) : createOptionsStyleStore(id, setupOrOptions, manager);
477
+ return instance;
478
+ };
479
+ }
480
+ //#endregion
481
+ //#region src/store/storeToRefs.ts
482
+ /**
483
+ * @description 将 store 状态转换为 Ref
484
+ */
485
+ function storeToRefs(store) {
486
+ const result = {};
487
+ for (const key in store) {
488
+ const value = store[key];
489
+ if (typeof value === "function") {
490
+ result[key] = value;
491
+ continue;
492
+ }
493
+ if (isRef(value)) result[key] = value;
494
+ else result[key] = computed({
495
+ get: () => store[key],
496
+ set: (v) => {
497
+ store[key] = v;
498
+ }
499
+ });
500
+ }
501
+ return result;
502
+ }
503
+ //#endregion
504
+ export { computed as i, defineStore as n, createStore as r, storeToRefs as t };
@@ -0,0 +1,2 @@
1
+ import { a as ActionContext, c as MutationType, d as SubscriptionCallback, i as defineStore, l as StoreManager, n as storeToRefs, o as ActionSubscriber, r as createStore, s as DefineStoreOptions, t as StoreToRefsResult, u as StoreSubscribeOptions } from "./index-Ciz2Ye1L.mjs";
2
+ export { ActionContext, ActionSubscriber, DefineStoreOptions, MutationType, StoreManager, StoreSubscribeOptions, StoreToRefsResult, SubscriptionCallback, createStore, defineStore, storeToRefs };
package/dist/store.mjs ADDED
@@ -0,0 +1,2 @@
1
+ import { n as defineStore, r as createStore, t as storeToRefs } from "./store-DFP_p2kt.mjs";
2
+ export { createStore, defineStore, storeToRefs };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "wevu",
3
3
  "type": "module",
4
- "version": "6.7.2",
4
+ "version": "6.7.4",
5
5
  "description": "Vue 3 风格的小程序运行时,包含响应式、diff+setData 与轻量状态管理",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -47,6 +47,34 @@
47
47
  "types": "./dist/jsx-runtime.d.mts",
48
48
  "default": "./dist/jsx-runtime.mjs"
49
49
  }
50
+ },
51
+ "./store": {
52
+ "types": "./dist/store.d.mts",
53
+ "import": {
54
+ "types": "./dist/store.d.mts",
55
+ "default": "./dist/store.mjs"
56
+ }
57
+ },
58
+ "./api": {
59
+ "types": "./dist/api.d.mts",
60
+ "import": {
61
+ "types": "./dist/api.d.mts",
62
+ "default": "./dist/api.mjs"
63
+ }
64
+ },
65
+ "./fetch": {
66
+ "types": "./dist/fetch.d.mts",
67
+ "import": {
68
+ "types": "./dist/fetch.d.mts",
69
+ "default": "./dist/fetch.mjs"
70
+ }
71
+ },
72
+ "./router": {
73
+ "types": "./dist/router.d.mts",
74
+ "import": {
75
+ "types": "./dist/router.d.mts",
76
+ "default": "./dist/router.mjs"
77
+ }
50
78
  }
51
79
  },
52
80
  "main": "./dist/index.mjs",
@@ -67,7 +95,8 @@
67
95
  },
68
96
  "dependencies": {
69
97
  "vue": "^3.5.29",
70
- "@wevu/compiler": "6.7.2"
98
+ "@wevu/api": "0.2.0",
99
+ "@wevu/compiler": "6.7.4"
71
100
  },
72
101
  "scripts": {
73
102
  "dev": "tsdown -w",