wevu 0.0.2-alpha.0 → 1.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { _ as computed, a as isRef, b as nextTick, c as isObject, d as isShallowReactive, f as markRaw, g as touchReactive, h as toRaw, i as customRef, l as isRaw, m as shallowReactive, n as defineStore, o as ref, p as reactive, r as createStore, s as unref, t as storeToRefs, u as isReactive, v as effect, x as queueJob, y as stop } from "./store-BcU7YVhB.mjs";
1
+ import { C as onScopeDispose, D as queueJob, E as nextTick, S as getCurrentScope, T as stop, _ as computed, a as isRef, b as effectScope, c as isObject, d as isShallowReactive, f as markRaw, g as touchReactive, h as toRaw, i as customRef, l as isRaw, m as shallowReactive, n as defineStore, o as ref, p as reactive, r as createStore, s as unref, t as storeToRefs, u as isReactive, v as batch, w as startBatch, x as endBatch, y as effect } from "./store-2q4qcdBi.mjs";
2
2
 
3
3
  //#region src/reactivity/readonly.ts
4
4
  function readonly(target) {
@@ -155,10 +155,13 @@ function watch(source, cb, options = {}) {
155
155
  });
156
156
  if (options.immediate) job();
157
157
  else oldValue = runner();
158
- return () => {
158
+ const stopHandle = () => {
159
159
  cleanup?.();
160
+ cleanup = void 0;
160
161
  stop(runner);
161
162
  };
163
+ onScopeDispose(stopHandle);
164
+ return stopHandle;
162
165
  }
163
166
  /**
164
167
  * watchEffect 注册一个响应式副作用,可选清理函数。
@@ -173,14 +176,20 @@ function watchEffect(effectFn) {
173
176
  cleanup?.();
174
177
  cleanup = void 0;
175
178
  effectFn(onCleanup);
176
- }, { scheduler: () => queueJob(() => {
177
- if (runner.active) runner();
178
- }) });
179
+ }, {
180
+ lazy: true,
181
+ scheduler: () => queueJob(() => {
182
+ if (runner.active) runner();
183
+ })
184
+ });
179
185
  runner();
180
- return () => {
186
+ const stopHandle = () => {
181
187
  cleanup?.();
188
+ cleanup = void 0;
182
189
  stop(runner);
183
190
  };
191
+ onScopeDispose(stopHandle);
192
+ return stopHandle;
184
193
  }
185
194
 
186
195
  //#endregion
@@ -283,7 +292,7 @@ function createBindModel(publicInstance, state, computedRefs, computedSetters) {
283
292
 
284
293
  //#endregion
285
294
  //#region src/runtime/diff.ts
286
- function isPlainObject(value) {
295
+ function isPlainObject$1(value) {
287
296
  if (Object.prototype.toString.call(value) !== "[object Object]") return false;
288
297
  const proto = Object.getPrototypeOf(value);
289
298
  return proto === null || proto === Object.prototype;
@@ -311,7 +320,7 @@ function toPlain(value, seen = /* @__PURE__ */ new WeakMap()) {
311
320
  function isDeepEqual(a, b) {
312
321
  if (Object.is(a, b)) return true;
313
322
  if (Array.isArray(a) && Array.isArray(b)) return isArrayEqual(a, b);
314
- if (isPlainObject(a) && isPlainObject(b)) return isPlainObjectEqual(a, b);
323
+ if (isPlainObject$1(a) && isPlainObject$1(b)) return isPlainObjectEqual(a, b);
315
324
  return false;
316
325
  }
317
326
  function isArrayEqual(a, b) {
@@ -334,7 +343,7 @@ function normalizeSetDataValue(value) {
334
343
  }
335
344
  function assignNestedDiff(prev, next, path, output) {
336
345
  if (isDeepEqual(prev, next)) return;
337
- if (isPlainObject(prev) && isPlainObject(next)) {
346
+ if (isPlainObject$1(prev) && isPlainObject$1(next)) {
338
347
  new Set([...Object.keys(prev), ...Object.keys(next)]).forEach((key) => {
339
348
  if (!Object.prototype.hasOwnProperty.call(next, key)) {
340
349
  output[`${path}.${key}`] = null;
@@ -546,6 +555,20 @@ function callUpdateHooks(target, phase) {
546
555
 
547
556
  //#endregion
548
557
  //#region src/runtime/register.ts
558
+ function runInlineExpression(ctx, expr, event) {
559
+ const handlerName = typeof expr === "string" ? expr : void 0;
560
+ if (!handlerName) return;
561
+ const argsRaw = (event?.currentTarget)?.dataset?.wvArgs ?? (event?.target)?.dataset?.wvArgs;
562
+ let args = [];
563
+ if (typeof argsRaw === "string") try {
564
+ args = JSON.parse(argsRaw);
565
+ } catch {
566
+ args = [];
567
+ }
568
+ const resolvedArgs = args.map((item) => item === "$event" ? event : item);
569
+ const handler = ctx?.[handlerName];
570
+ if (typeof handler === "function") return handler.apply(ctx, resolvedArgs);
571
+ }
549
572
  function runSetupFunction(setup, props, context) {
550
573
  if (typeof setup !== "function") return;
551
574
  const runtimeContext = context?.runtime ?? {
@@ -611,6 +634,7 @@ function registerWatches(runtime, watchMap, instance) {
611
634
  return stops;
612
635
  }
613
636
  function mountRuntimeInstance(target, runtimeApp, watchMap, setup) {
637
+ if (target.__wevu) return target.__wevu;
614
638
  const runtime = runtimeApp.mount({ setData(payload) {
615
639
  if (typeof target.setData === "function") target.setData(payload);
616
640
  } });
@@ -682,6 +706,7 @@ function mountRuntimeInstance(target, runtimeApp, watchMap, setup) {
682
706
  }
683
707
  function teardownRuntimeInstance(target) {
684
708
  const runtime = target.__wevu;
709
+ if (runtime && target.__wevuHooks) callHookList(target, "onUnload", []);
685
710
  if (target.__wevuHooks) target.__wevuHooks = void 0;
686
711
  const stops = target.__wevuWatchStops;
687
712
  if (Array.isArray(stops)) for (const stop$1 of stops) try {
@@ -697,6 +722,10 @@ function registerApp(runtimeApp, methods, watch$1, setup, mpOptions) {
697
722
  const methodNames = Object.keys(methods ?? {});
698
723
  const appOptions = { ...mpOptions };
699
724
  appOptions.globalData = appOptions.globalData ?? {};
725
+ if (!appOptions.__weapp_vite_inline) appOptions.__weapp_vite_inline = function __weapp_vite_inline(event) {
726
+ const expr = event?.currentTarget?.dataset?.wvHandler ?? event?.target?.dataset?.wvHandler;
727
+ return runInlineExpression(this.__wevu?.proxy ?? this, expr, event);
728
+ };
700
729
  const userOnLaunch = appOptions.onLaunch;
701
730
  appOptions.onLaunch = function onLaunch(...args) {
702
731
  mountRuntimeInstance(this, runtimeApp, watch$1, setup);
@@ -731,89 +760,38 @@ function registerApp(runtimeApp, methods, watch$1, setup, mpOptions) {
731
760
  }
732
761
  App(appOptions);
733
762
  }
734
- function registerPage(runtimeApp, methods, watch$1, setup, mpOptions, features) {
735
- if (typeof Page !== "function") throw new TypeError("definePage requires the global Page constructor to be available");
736
- const methodNames = Object.keys(methods ?? {});
737
- const pageOptions = { ...mpOptions };
738
- const userOnLoad = mpOptions.onLoad;
739
- pageOptions.onLoad = function onLoad(...args) {
740
- mountRuntimeInstance(this, runtimeApp, watch$1, setup);
741
- callHookList(this, "onShow", args);
742
- if (typeof userOnLoad === "function") userOnLoad.apply(this, args);
743
- };
744
- const userOnUnload = mpOptions.onUnload;
745
- pageOptions.onUnload = function onUnload$1(...args) {
746
- teardownRuntimeInstance(this);
747
- if (typeof userOnUnload === "function") userOnUnload.apply(this, args);
763
+ function registerComponent(runtimeApp, methods, watch$1, setup, mpOptions, features) {
764
+ const { methods: userMethods = {}, lifetimes: userLifetimes = {}, pageLifetimes: userPageLifetimes = {}, options: userOptions = {}, ...rest } = mpOptions;
765
+ const userOnLoad = rest.onLoad;
766
+ const userOnUnload = rest.onUnload;
767
+ const userOnShow = rest.onShow;
768
+ const userOnHide = rest.onHide;
769
+ const userOnReady = rest.onReady;
770
+ const userOnSaveExitState = rest.onSaveExitState;
771
+ const userOnPageScroll = rest.onPageScroll;
772
+ const userOnShareAppMessage = rest.onShareAppMessage;
773
+ const userOnShareTimeline = rest.onShareTimeline;
774
+ const userOnAddToFavorites = rest.onAddToFavorites;
775
+ const restOptions = { ...rest };
776
+ delete restOptions.onLoad;
777
+ delete restOptions.onUnload;
778
+ delete restOptions.onShow;
779
+ delete restOptions.onHide;
780
+ delete restOptions.onReady;
781
+ delete restOptions.onSaveExitState;
782
+ delete restOptions.onPageScroll;
783
+ delete restOptions.onShareAppMessage;
784
+ delete restOptions.onShareTimeline;
785
+ delete restOptions.onAddToFavorites;
786
+ const finalOptions = {
787
+ multipleSlots: userOptions.multipleSlots ?? true,
788
+ ...userOptions
748
789
  };
749
- const userOnShow = mpOptions.onShow;
750
- pageOptions.onShow = function onShow$1(...args) {
751
- callHookList(this, "onShow", args);
752
- if (typeof userOnShow === "function") userOnShow.apply(this, args);
753
- };
754
- const userOnHide = mpOptions.onHide;
755
- pageOptions.onHide = function onHide$1(...args) {
756
- callHookList(this, "onHide", args);
757
- if (typeof userOnHide === "function") userOnHide.apply(this, args);
758
- };
759
- const userOnReady = mpOptions.onReady;
760
- pageOptions.onReady = function onReady$1(...args) {
761
- callHookList(this, "onReady", args);
762
- if (typeof userOnReady === "function") return userOnReady.apply(this, args);
763
- };
764
- const userOnSaveExitState = mpOptions.onSaveExitState;
765
- pageOptions.onSaveExitState = function onSaveExitState$1(...args) {
766
- const ret = callHookReturn(this, "onSaveExitState", args);
767
- if (ret !== void 0) return ret;
768
- if (typeof userOnSaveExitState === "function") return userOnSaveExitState.apply(this, args);
769
- };
770
- if (features?.listenPageScroll) {
771
- const userOnPageScroll = mpOptions.onPageScroll;
772
- pageOptions.onPageScroll = function onPageScroll$1(...args) {
773
- callHookList(this, "onPageScroll", args);
774
- if (typeof userOnPageScroll === "function") return userOnPageScroll.apply(this, args);
775
- };
776
- }
777
- if (features?.enableShareAppMessage) {
778
- const userOnShare = mpOptions.onShareAppMessage;
779
- pageOptions.onShareAppMessage = function pageOnShareAppMessage(...args) {
780
- const ret = callHookReturn(this, "onShareAppMessage", args);
781
- if (ret !== void 0) return ret;
782
- if (typeof userOnShare === "function") return userOnShare.apply(this, args);
783
- };
784
- }
785
- if (features?.enableShareTimeline) {
786
- const userOnShareTimeline = mpOptions.onShareTimeline;
787
- pageOptions.onShareTimeline = function pageOnShareTimeline(...args) {
788
- const ret = callHookReturn(this, "onShareTimeline", args);
789
- if (ret !== void 0) return ret;
790
- if (typeof userOnShareTimeline === "function") return userOnShareTimeline.apply(this, args);
791
- };
792
- }
793
- if (features?.enableAddToFavorites) {
794
- const userOnAddToFavorites = mpOptions.onAddToFavorites;
795
- pageOptions.onAddToFavorites = function pageOnAddToFavorites(...args) {
796
- const ret = callHookReturn(this, "onAddToFavorites", args);
797
- if (ret !== void 0) return ret;
798
- if (typeof userOnAddToFavorites === "function") return userOnAddToFavorites.apply(this, args);
799
- };
800
- }
801
- for (const methodName of methodNames) {
802
- const userMethod = mpOptions[methodName];
803
- pageOptions[methodName] = function runtimeMethod(...args) {
804
- const runtime = this.__wevu;
805
- let result;
806
- const bound = runtime?.methods?.[methodName];
807
- if (bound) result = bound.apply(runtime.proxy, args);
808
- if (typeof userMethod === "function") return userMethod.apply(this, args);
809
- return result;
810
- };
811
- }
812
- Page(pageOptions);
813
- }
814
- function registerComponent(runtimeApp, methods, watch$1, setup, mpOptions) {
815
- const { methods: userMethods = {}, lifetimes: userLifetimes = {}, pageLifetimes: userPageLifetimes = {}, ...rest } = mpOptions;
816
790
  const finalMethods = { ...userMethods };
791
+ if (!finalMethods.__weapp_vite_inline) finalMethods.__weapp_vite_inline = function __weapp_vite_inline(event) {
792
+ const expr = event?.currentTarget?.dataset?.wvHandler ?? event?.target?.dataset?.wvHandler;
793
+ return runInlineExpression(this.__wevu?.proxy ?? this, expr, event);
794
+ };
817
795
  const methodNames = Object.keys(methods ?? {});
818
796
  for (const methodName of methodNames) {
819
797
  const userMethod = finalMethods[methodName];
@@ -835,8 +813,58 @@ function registerComponent(runtimeApp, methods, watch$1, setup, mpOptions) {
835
813
  };
836
814
  wrapSpecial("onTabItemTap");
837
815
  wrapSpecial("onRouteDone");
816
+ const pageLifecycleHooks = {
817
+ onLoad(...args) {
818
+ mountRuntimeInstance(this, runtimeApp, watch$1, setup);
819
+ if (typeof userOnLoad === "function") return userOnLoad.apply(this, args);
820
+ },
821
+ onUnload(...args) {
822
+ teardownRuntimeInstance(this);
823
+ if (typeof userOnUnload === "function") return userOnUnload.apply(this, args);
824
+ },
825
+ onShow(...args) {
826
+ callHookList(this, "onShow", args);
827
+ if (typeof userOnShow === "function") return userOnShow.apply(this, args);
828
+ },
829
+ onHide(...args) {
830
+ callHookList(this, "onHide", args);
831
+ if (typeof userOnHide === "function") return userOnHide.apply(this, args);
832
+ },
833
+ onReady(...args) {
834
+ if (!this.__wevuReadyCalled) {
835
+ this.__wevuReadyCalled = true;
836
+ callHookList(this, "onReady", args);
837
+ }
838
+ if (typeof userOnReady === "function") return userOnReady.apply(this, args);
839
+ },
840
+ onSaveExitState(...args) {
841
+ const ret = callHookReturn(this, "onSaveExitState", args);
842
+ if (ret !== void 0) return ret;
843
+ if (typeof userOnSaveExitState === "function") return userOnSaveExitState.apply(this, args);
844
+ }
845
+ };
846
+ if (features?.listenPageScroll) pageLifecycleHooks.onPageScroll = function onPageScroll$1(...args) {
847
+ callHookList(this, "onPageScroll", args);
848
+ if (typeof userOnPageScroll === "function") return userOnPageScroll.apply(this, args);
849
+ };
850
+ if (features?.enableShareAppMessage) pageLifecycleHooks.onShareAppMessage = function onShareAppMessage$1(...args) {
851
+ const ret = callHookReturn(this, "onShareAppMessage", args);
852
+ if (ret !== void 0) return ret;
853
+ if (typeof userOnShareAppMessage === "function") return userOnShareAppMessage.apply(this, args);
854
+ };
855
+ if (features?.enableShareTimeline) pageLifecycleHooks.onShareTimeline = function onShareTimeline$1(...args) {
856
+ const ret = callHookReturn(this, "onShareTimeline", args);
857
+ if (ret !== void 0) return ret;
858
+ if (typeof userOnShareTimeline === "function") return userOnShareTimeline.apply(this, args);
859
+ };
860
+ if (features?.enableAddToFavorites) pageLifecycleHooks.onAddToFavorites = function onAddToFavorites$1(...args) {
861
+ const ret = callHookReturn(this, "onAddToFavorites", args);
862
+ if (ret !== void 0) return ret;
863
+ if (typeof userOnAddToFavorites === "function") return userOnAddToFavorites.apply(this, args);
864
+ };
838
865
  Component({
839
- ...rest,
866
+ ...restOptions,
867
+ ...pageLifecycleHooks,
840
868
  lifetimes: {
841
869
  ...userLifetimes,
842
870
  attached: function attached(...args) {
@@ -844,7 +872,10 @@ function registerComponent(runtimeApp, methods, watch$1, setup, mpOptions) {
844
872
  if (typeof userLifetimes.attached === "function") userLifetimes.attached.apply(this, args);
845
873
  },
846
874
  ready: function ready(...args) {
847
- callHookList(this, "onReady", args);
875
+ if (!this.__wevuReadyCalled) {
876
+ this.__wevuReadyCalled = true;
877
+ callHookList(this, "onReady", args);
878
+ }
848
879
  if (typeof userLifetimes.ready === "function") userLifetimes.ready.apply(this, args);
849
880
  },
850
881
  detached: function detached(...args) {
@@ -863,7 +894,8 @@ function registerComponent(runtimeApp, methods, watch$1, setup, mpOptions) {
863
894
  if (typeof userPageLifetimes.hide === "function") userPageLifetimes.hide.apply(this, args);
864
895
  }
865
896
  },
866
- methods: finalMethods
897
+ methods: finalMethods,
898
+ options: finalOptions
867
899
  });
868
900
  }
869
901
 
@@ -986,6 +1018,7 @@ function createApp(options) {
986
1018
  };
987
1019
  const job = () => {
988
1020
  if (!mounted) return;
1021
+ tracker();
989
1022
  const snapshot = collectSnapshot();
990
1023
  const diff = diffSnapshots(latestSnapshot, snapshot);
991
1024
  latestSnapshot = snapshot;
@@ -995,10 +1028,18 @@ function createApp(options) {
995
1028
  if (result && typeof result.then === "function") result.catch(() => {});
996
1029
  }
997
1030
  };
998
- const tracker = effect(() => {
1031
+ let tracker;
1032
+ tracker = effect(() => {
999
1033
  touchReactive(state);
1034
+ Object.keys(state).forEach((key) => {
1035
+ const v = state[key];
1036
+ if (isRef(v)) v.value;
1037
+ });
1000
1038
  Object.keys(computedRefs).forEach((key) => computedRefs[key].value);
1001
- }, { scheduler: () => queueJob(job) });
1039
+ }, {
1040
+ lazy: true,
1041
+ scheduler: () => queueJob(job)
1042
+ });
1002
1043
  job();
1003
1044
  stopHandles.push(() => stop(tracker));
1004
1045
  function registerWatch(source, cb, watchOptions) {
@@ -1065,7 +1106,9 @@ function setComputedValue(setters, key, value) {
1065
1106
  //#endregion
1066
1107
  //#region src/runtime/define.ts
1067
1108
  /**
1068
- * 按 Vue 3 风格定义一个小程序组件(始终注册为 Component,页面请用 definePage)。
1109
+ * 按 Vue 3 风格定义一个小程序组件/页面。
1110
+ *
1111
+ * - 统一注册为 `Component()`
1069
1112
  *
1070
1113
  * @param options 组件定义项
1071
1114
  * @returns 可手动注册的组件定义
@@ -1079,57 +1122,19 @@ function setComputedValue(setters, key, value) {
1079
1122
  * }
1080
1123
  * })
1081
1124
  * ```
1082
- */
1083
- function defineComponent(options) {
1084
- const { data, computed: computed$1, methods, watch: watch$1, setup, props, ...mpOptions } = options;
1085
- const mpOptionsWithProps = normalizeProps(mpOptions, props);
1086
- const runtimeApp = createApp({
1087
- data,
1088
- computed: computed$1,
1089
- methods
1090
- });
1091
- const setupWrapper = (ctx) => {
1092
- const result = runSetupFunction(setup, ctx?.props ?? {}, ctx);
1093
- if (result) applySetupResult(ctx.runtime, ctx.instance, result);
1094
- };
1095
- const componentOptions = {
1096
- data,
1097
- computed: computed$1,
1098
- methods,
1099
- watch: watch$1,
1100
- setup: setupWrapper,
1101
- mpOptions: mpOptionsWithProps
1102
- };
1103
- registerComponent(runtimeApp, methods ?? {}, watch$1, setupWrapper, mpOptionsWithProps);
1104
- return {
1105
- __wevu_runtime: runtimeApp,
1106
- __wevu_options: componentOptions
1107
- };
1108
- }
1109
- /**
1110
- * 按 Vue 3 风格定义一个小程序页面
1111
- *
1112
- * @param options 页面定义
1113
- * @param features 页面特性(例如 listenPageScroll、enableShareAppMessage 等)
1114
- * @returns 页面定义
1115
1125
  *
1116
1126
  * @example
1117
1127
  * ```ts
1118
- * definePage({
1119
- * data: () => ({ count: 0 }),
1128
+ * defineComponent({
1129
+ * features: { listenPageScroll: true },
1120
1130
  * setup() {
1121
- * const count = ref(0)
1122
- * onMounted(() => console.log('page mounted'))
1123
- * return { count }
1131
+ * onPageScroll(() => {})
1124
1132
  * }
1125
- * }, {
1126
- * listenPageScroll: true,
1127
- * enableShareAppMessage: true
1128
1133
  * })
1129
1134
  * ```
1130
1135
  */
1131
- function definePage(options, features) {
1132
- const { data, computed: computed$1, methods, watch: watch$1, setup, props: _props, ...mpOptions } = options;
1136
+ function defineComponent(options) {
1137
+ const { features, data, computed: computed$1, methods, watch: watch$1, setup, props, ...mpOptions } = options;
1133
1138
  const runtimeApp = createApp({
1134
1139
  data,
1135
1140
  computed: computed$1,
@@ -1139,19 +1144,18 @@ function definePage(options, features) {
1139
1144
  const result = runSetupFunction(setup, ctx?.props ?? {}, ctx);
1140
1145
  if (result) applySetupResult(ctx.runtime, ctx.instance, result);
1141
1146
  };
1147
+ const mpOptionsWithProps = normalizeProps(mpOptions, props);
1142
1148
  const componentOptions = {
1143
- type: "page",
1144
1149
  data,
1145
1150
  computed: computed$1,
1146
1151
  methods,
1147
1152
  watch: watch$1,
1148
1153
  setup: setupWrapper,
1149
- mpOptions,
1154
+ mpOptions: mpOptionsWithProps,
1150
1155
  features
1151
1156
  };
1152
- registerPage(runtimeApp, methods ?? {}, watch$1, setupWrapper, mpOptions, features);
1157
+ registerComponent(runtimeApp, methods ?? {}, watch$1, setupWrapper, mpOptionsWithProps, features);
1153
1158
  return {
1154
- mount: () => {},
1155
1159
  __wevu_runtime: runtimeApp,
1156
1160
  __wevu_options: componentOptions
1157
1161
  };
@@ -1159,6 +1163,7 @@ function definePage(options, features) {
1159
1163
  function applySetupResult(runtime, _target, result) {
1160
1164
  const methods = runtime?.methods ?? Object.create(null);
1161
1165
  const state = runtime?.state ?? Object.create(null);
1166
+ const rawState = isReactive(state) ? toRaw(state) : state;
1162
1167
  if (runtime && !runtime.methods) try {
1163
1168
  runtime.methods = methods;
1164
1169
  } catch {}
@@ -1168,6 +1173,16 @@ function applySetupResult(runtime, _target, result) {
1168
1173
  Object.keys(result).forEach((key) => {
1169
1174
  const val = result[key];
1170
1175
  if (typeof val === "function") methods[key] = (...args) => val.apply(runtime?.proxy ?? runtime, args);
1176
+ else if (val === _target || !shouldExposeInSnapshot(val)) try {
1177
+ Object.defineProperty(rawState, key, {
1178
+ value: val,
1179
+ configurable: true,
1180
+ enumerable: false,
1181
+ writable: true
1182
+ });
1183
+ } catch {
1184
+ state[key] = val;
1185
+ }
1171
1186
  else state[key] = val;
1172
1187
  });
1173
1188
  if (runtime) {
@@ -1175,6 +1190,18 @@ function applySetupResult(runtime, _target, result) {
1175
1190
  runtime.state = runtime.state ?? state;
1176
1191
  }
1177
1192
  }
1193
+ function isPlainObject(value) {
1194
+ if (Object.prototype.toString.call(value) !== "[object Object]") return false;
1195
+ const proto = Object.getPrototypeOf(value);
1196
+ return proto === null || proto === Object.prototype;
1197
+ }
1198
+ function shouldExposeInSnapshot(value) {
1199
+ if (value == null) return true;
1200
+ if (typeof value !== "object") return true;
1201
+ if (isRef(value) || isReactive(value)) return true;
1202
+ if (Array.isArray(value)) return true;
1203
+ return isPlainObject(value);
1204
+ }
1178
1205
  /**
1179
1206
  * 从 Vue SFC 选项创建 wevu 组件,供 weapp-vite 编译产物直接调用的兼容入口。
1180
1207
  *
@@ -1287,4 +1314,4 @@ function injectGlobal(key, defaultValue) {
1287
1314
  }
1288
1315
 
1289
1316
  //#endregion
1290
- export { callHookList, callHookReturn, callUpdateHooks, computed, createApp, createStore, createWevuComponent, defineComponent, definePage, defineStore, effect, getCurrentInstance, getDeepWatchStrategy, inject, injectGlobal, isRaw, isReactive, isRef, isShallowReactive, isShallowRef, markRaw, mountRuntimeInstance, nextTick, onActivated, onAddToFavorites, onAppError, onAppHide, onAppShow, onBeforeMount, onBeforeUnmount, onBeforeUpdate, onDeactivated, onErrorCaptured, onHide, onMounted, onPageScroll, onReady, onRouteDone, onSaveExitState, onServerPrefetch, onShareAppMessage, onShareTimeline, onShow, onTabItemTap, onUnload, onUnmounted, onUpdated, provide, provideGlobal, reactive, readonly, ref, registerApp, registerComponent, registerPage, runSetupFunction, setCurrentInstance, setDeepWatchStrategy, shallowReactive, shallowRef, stop, storeToRefs, teardownRuntimeInstance, toRaw, toRef, toRefs, touchReactive, traverse, triggerRef, unref, watch, watchEffect };
1317
+ export { batch, callHookList, callHookReturn, callUpdateHooks, computed, createApp, createStore, createWevuComponent, defineComponent, defineStore, effect, effectScope, endBatch, getCurrentInstance, getCurrentScope, getDeepWatchStrategy, inject, injectGlobal, isRaw, isReactive, isRef, isShallowReactive, isShallowRef, markRaw, mountRuntimeInstance, nextTick, onActivated, onAddToFavorites, onAppError, onAppHide, onAppShow, onBeforeMount, onBeforeUnmount, onBeforeUpdate, onDeactivated, onErrorCaptured, onHide, onMounted, onPageScroll, onReady, onRouteDone, onSaveExitState, onScopeDispose, onServerPrefetch, onShareAppMessage, onShareTimeline, onShow, onTabItemTap, onUnload, onUnmounted, onUpdated, provide, provideGlobal, reactive, readonly, ref, registerApp, registerComponent, runSetupFunction, setCurrentInstance, setDeepWatchStrategy, shallowReactive, shallowRef, startBatch, stop, storeToRefs, teardownRuntimeInstance, toRaw, toRef, toRefs, touchReactive, traverse, triggerRef, unref, watch, watchEffect };
@@ -24,6 +24,85 @@ function nextTick(fn) {
24
24
  const targetMap = /* @__PURE__ */ new WeakMap();
25
25
  let activeEffect = null;
26
26
  const effectStack = [];
27
+ let batchDepth = 0;
28
+ const batchedEffects = /* @__PURE__ */ new Set();
29
+ function startBatch() {
30
+ batchDepth++;
31
+ }
32
+ function endBatch() {
33
+ if (batchDepth === 0) return;
34
+ batchDepth--;
35
+ if (batchDepth === 0) flushBatchedEffects();
36
+ }
37
+ function batch(fn) {
38
+ startBatch();
39
+ try {
40
+ return fn();
41
+ } finally {
42
+ endBatch();
43
+ }
44
+ }
45
+ function flushBatchedEffects() {
46
+ while (batchedEffects.size) {
47
+ const effects = Array.from(batchedEffects);
48
+ batchedEffects.clear();
49
+ for (const ef of effects) ef();
50
+ }
51
+ }
52
+ let activeEffectScope;
53
+ var EffectScopeImpl = class {
54
+ active = true;
55
+ effects = [];
56
+ cleanups = [];
57
+ parent;
58
+ scopes;
59
+ constructor(detached = false) {
60
+ this.detached = detached;
61
+ if (!detached && activeEffectScope) {
62
+ this.parent = activeEffectScope;
63
+ (activeEffectScope.scopes ||= []).push(this);
64
+ }
65
+ }
66
+ run(fn) {
67
+ if (!this.active) return;
68
+ const prev = activeEffectScope;
69
+ activeEffectScope = this;
70
+ try {
71
+ return fn();
72
+ } finally {
73
+ activeEffectScope = prev;
74
+ }
75
+ }
76
+ stop() {
77
+ if (!this.active) return;
78
+ this.active = false;
79
+ for (const effect$1 of this.effects) stop(effect$1);
80
+ this.effects.length = 0;
81
+ for (const cleanup of this.cleanups) cleanup();
82
+ this.cleanups.length = 0;
83
+ if (this.scopes) {
84
+ for (const scope of this.scopes) scope.stop();
85
+ this.scopes.length = 0;
86
+ }
87
+ if (this.parent?.scopes) {
88
+ const index = this.parent.scopes.indexOf(this);
89
+ if (index >= 0) this.parent.scopes.splice(index, 1);
90
+ }
91
+ this.parent = void 0;
92
+ }
93
+ };
94
+ function effectScope(detached = false) {
95
+ return new EffectScopeImpl(detached);
96
+ }
97
+ function getCurrentScope() {
98
+ return activeEffectScope;
99
+ }
100
+ function onScopeDispose(fn) {
101
+ if (activeEffectScope?.active) activeEffectScope.cleanups.push(fn);
102
+ }
103
+ function recordEffectScope(effect$1) {
104
+ if (activeEffectScope?.active) activeEffectScope.effects.push(effect$1);
105
+ }
27
106
  function cleanupEffect(effect$1) {
28
107
  const { deps } = effect$1;
29
108
  for (let i = 0; i < deps.length; i++) deps[i].delete(effect$1);
@@ -32,26 +111,30 @@ function cleanupEffect(effect$1) {
32
111
  function createReactiveEffect(fn, options = {}) {
33
112
  const effect$1 = function reactiveEffect() {
34
113
  if (!effect$1.active) return fn();
35
- if (effectStack.includes(effect$1)) return fn();
114
+ if (effect$1._running) return fn();
36
115
  cleanupEffect(effect$1);
37
116
  try {
117
+ effect$1._running = true;
38
118
  effectStack.push(effect$1);
39
119
  activeEffect = effect$1;
40
120
  return fn();
41
121
  } finally {
42
122
  effectStack.pop();
43
123
  activeEffect = effectStack[effectStack.length - 1] ?? null;
124
+ effect$1._running = false;
44
125
  }
45
126
  };
46
127
  effect$1.deps = [];
47
128
  effect$1.scheduler = options.scheduler;
48
129
  effect$1.onStop = options.onStop;
49
130
  effect$1.active = true;
131
+ effect$1._running = false;
50
132
  effect$1._fn = fn;
51
133
  return effect$1;
52
134
  }
53
135
  function effect(fn, options = {}) {
54
136
  const _effect = createReactiveEffect(fn, options);
137
+ recordEffectScope(_effect);
55
138
  if (!options.lazy) _effect();
56
139
  return _effect;
57
140
  }
@@ -87,10 +170,7 @@ function trigger(target, key) {
87
170
  effects.forEach((ef) => {
88
171
  if (ef !== activeEffect) effectsToRun.add(ef);
89
172
  });
90
- effectsToRun.forEach((ef) => {
91
- if (ef.scheduler) ef.scheduler();
92
- else ef();
93
- });
173
+ effectsToRun.forEach(scheduleEffect);
94
174
  }
95
175
  function trackEffects(dep) {
96
176
  if (!activeEffect) return;
@@ -100,10 +180,18 @@ function trackEffects(dep) {
100
180
  }
101
181
  }
102
182
  function triggerEffects(dep) {
103
- dep.forEach((ef) => {
104
- if (ef.scheduler) ef.scheduler();
105
- else ef();
106
- });
183
+ dep.forEach(scheduleEffect);
184
+ }
185
+ function scheduleEffect(ef) {
186
+ if (ef.scheduler) {
187
+ ef.scheduler();
188
+ return;
189
+ }
190
+ if (batchDepth > 0) {
191
+ batchedEffects.add(ef);
192
+ return;
193
+ }
194
+ ef();
107
195
  }
108
196
 
109
197
  //#endregion
@@ -365,7 +453,7 @@ var RefImpl = class {
365
453
  };
366
454
  function ref(value) {
367
455
  if (isRef(value)) return value;
368
- return new RefImpl(value);
456
+ return markRaw(new RefImpl(value));
369
457
  }
370
458
  function unref(value) {
371
459
  return isRef(value) ? value.value : value;
@@ -389,7 +477,7 @@ var CustomRefImpl = class {
389
477
  }
390
478
  };
391
479
  function customRef(factory, defaultValue) {
392
- return new CustomRefImpl(factory, defaultValue);
480
+ return markRaw(new CustomRefImpl(factory, defaultValue));
393
481
  }
394
482
 
395
483
  //#endregion
@@ -635,4 +723,4 @@ function storeToRefs(store) {
635
723
  }
636
724
 
637
725
  //#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 };
726
+ export { onScopeDispose as C, queueJob as D, nextTick as E, getCurrentScope as S, stop as T, computed as _, isRef as a, effectScope 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, batch as v, startBatch as w, endBatch as x, effect as y };