wevu 1.0.0-alpha.1 → 1.0.0-alpha.3

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.
@@ -25,6 +25,85 @@ function nextTick(fn) {
25
25
  const targetMap = /* @__PURE__ */ new WeakMap();
26
26
  let activeEffect = null;
27
27
  const effectStack = [];
28
+ let batchDepth = 0;
29
+ const batchedEffects = /* @__PURE__ */ new Set();
30
+ function startBatch() {
31
+ batchDepth++;
32
+ }
33
+ function endBatch() {
34
+ if (batchDepth === 0) return;
35
+ batchDepth--;
36
+ if (batchDepth === 0) flushBatchedEffects();
37
+ }
38
+ function batch(fn) {
39
+ startBatch();
40
+ try {
41
+ return fn();
42
+ } finally {
43
+ endBatch();
44
+ }
45
+ }
46
+ function flushBatchedEffects() {
47
+ while (batchedEffects.size) {
48
+ const effects = Array.from(batchedEffects);
49
+ batchedEffects.clear();
50
+ for (const ef of effects) ef();
51
+ }
52
+ }
53
+ let activeEffectScope;
54
+ var EffectScopeImpl = class {
55
+ active = true;
56
+ effects = [];
57
+ cleanups = [];
58
+ parent;
59
+ scopes;
60
+ constructor(detached = false) {
61
+ this.detached = detached;
62
+ if (!detached && activeEffectScope) {
63
+ this.parent = activeEffectScope;
64
+ (activeEffectScope.scopes ||= []).push(this);
65
+ }
66
+ }
67
+ run(fn) {
68
+ if (!this.active) return;
69
+ const prev = activeEffectScope;
70
+ activeEffectScope = this;
71
+ try {
72
+ return fn();
73
+ } finally {
74
+ activeEffectScope = prev;
75
+ }
76
+ }
77
+ stop() {
78
+ if (!this.active) return;
79
+ this.active = false;
80
+ for (const effect$1 of this.effects) stop(effect$1);
81
+ this.effects.length = 0;
82
+ for (const cleanup of this.cleanups) cleanup();
83
+ this.cleanups.length = 0;
84
+ if (this.scopes) {
85
+ for (const scope of this.scopes) scope.stop();
86
+ this.scopes.length = 0;
87
+ }
88
+ if (this.parent?.scopes) {
89
+ const index = this.parent.scopes.indexOf(this);
90
+ if (index >= 0) this.parent.scopes.splice(index, 1);
91
+ }
92
+ this.parent = void 0;
93
+ }
94
+ };
95
+ function effectScope(detached = false) {
96
+ return new EffectScopeImpl(detached);
97
+ }
98
+ function getCurrentScope() {
99
+ return activeEffectScope;
100
+ }
101
+ function onScopeDispose(fn) {
102
+ if (activeEffectScope?.active) activeEffectScope.cleanups.push(fn);
103
+ }
104
+ function recordEffectScope(effect$1) {
105
+ if (activeEffectScope?.active) activeEffectScope.effects.push(effect$1);
106
+ }
28
107
  function cleanupEffect(effect$1) {
29
108
  const { deps } = effect$1;
30
109
  for (let i = 0; i < deps.length; i++) deps[i].delete(effect$1);
@@ -33,26 +112,30 @@ function cleanupEffect(effect$1) {
33
112
  function createReactiveEffect(fn, options = {}) {
34
113
  const effect$1 = function reactiveEffect() {
35
114
  if (!effect$1.active) return fn();
36
- if (effectStack.includes(effect$1)) return fn();
115
+ if (effect$1._running) return fn();
37
116
  cleanupEffect(effect$1);
38
117
  try {
118
+ effect$1._running = true;
39
119
  effectStack.push(effect$1);
40
120
  activeEffect = effect$1;
41
121
  return fn();
42
122
  } finally {
43
123
  effectStack.pop();
44
124
  activeEffect = effectStack[effectStack.length - 1] ?? null;
125
+ effect$1._running = false;
45
126
  }
46
127
  };
47
128
  effect$1.deps = [];
48
129
  effect$1.scheduler = options.scheduler;
49
130
  effect$1.onStop = options.onStop;
50
131
  effect$1.active = true;
132
+ effect$1._running = false;
51
133
  effect$1._fn = fn;
52
134
  return effect$1;
53
135
  }
54
136
  function effect(fn, options = {}) {
55
137
  const _effect = createReactiveEffect(fn, options);
138
+ recordEffectScope(_effect);
56
139
  if (!options.lazy) _effect();
57
140
  return _effect;
58
141
  }
@@ -88,10 +171,7 @@ function trigger(target, key) {
88
171
  effects.forEach((ef) => {
89
172
  if (ef !== activeEffect) effectsToRun.add(ef);
90
173
  });
91
- effectsToRun.forEach((ef) => {
92
- if (ef.scheduler) ef.scheduler();
93
- else ef();
94
- });
174
+ effectsToRun.forEach(scheduleEffect);
95
175
  }
96
176
  function trackEffects(dep) {
97
177
  if (!activeEffect) return;
@@ -101,10 +181,18 @@ function trackEffects(dep) {
101
181
  }
102
182
  }
103
183
  function triggerEffects(dep) {
104
- dep.forEach((ef) => {
105
- if (ef.scheduler) ef.scheduler();
106
- else ef();
107
- });
184
+ dep.forEach(scheduleEffect);
185
+ }
186
+ function scheduleEffect(ef) {
187
+ if (ef.scheduler) {
188
+ ef.scheduler();
189
+ return;
190
+ }
191
+ if (batchDepth > 0) {
192
+ batchedEffects.add(ef);
193
+ return;
194
+ }
195
+ ef();
108
196
  }
109
197
 
110
198
  //#endregion
@@ -229,7 +317,6 @@ function convertToReactive(value) {
229
317
  return isObject$1(value) ? reactive(value) : value;
230
318
  }
231
319
  /**
232
- * Establish a dependency on the whole reactive object "version".
233
320
  * 让 effect 订阅整个对象的“版本号”,无需深度遍历即可对任何字段变化做出响应。
234
321
  */
235
322
  function touchReactive(target) {
@@ -293,7 +380,7 @@ function shallowReactive(target) {
293
380
  return proxy;
294
381
  }
295
382
  /**
296
- * Checks if a value is a shallow reactive object
383
+ * 判断一个值是否为 shallowReactive 创建的浅层响应式对象
297
384
  */
298
385
  function isShallowReactive(value) {
299
386
  const raw = toRaw(value);
@@ -366,7 +453,7 @@ var RefImpl = class {
366
453
  };
367
454
  function ref(value) {
368
455
  if (isRef(value)) return value;
369
- return new RefImpl(value);
456
+ return markRaw(new RefImpl(value));
370
457
  }
371
458
  function unref(value) {
372
459
  return isRef(value) ? value.value : value;
@@ -390,7 +477,7 @@ var CustomRefImpl = class {
390
477
  }
391
478
  };
392
479
  function customRef(factory, defaultValue) {
393
- return new CustomRefImpl(factory, defaultValue);
480
+ return markRaw(new CustomRefImpl(factory, defaultValue));
394
481
  }
395
482
 
396
483
  //#endregion
@@ -636,6 +723,12 @@ function storeToRefs(store) {
636
723
  }
637
724
 
638
725
  //#endregion
726
+ Object.defineProperty(exports, 'batch', {
727
+ enumerable: true,
728
+ get: function () {
729
+ return batch;
730
+ }
731
+ });
639
732
  Object.defineProperty(exports, 'computed', {
640
733
  enumerable: true,
641
734
  get: function () {
@@ -666,6 +759,24 @@ Object.defineProperty(exports, 'effect', {
666
759
  return effect;
667
760
  }
668
761
  });
762
+ Object.defineProperty(exports, 'effectScope', {
763
+ enumerable: true,
764
+ get: function () {
765
+ return effectScope;
766
+ }
767
+ });
768
+ Object.defineProperty(exports, 'endBatch', {
769
+ enumerable: true,
770
+ get: function () {
771
+ return endBatch;
772
+ }
773
+ });
774
+ Object.defineProperty(exports, 'getCurrentScope', {
775
+ enumerable: true,
776
+ get: function () {
777
+ return getCurrentScope;
778
+ }
779
+ });
669
780
  Object.defineProperty(exports, 'isObject', {
670
781
  enumerable: true,
671
782
  get: function () {
@@ -708,6 +819,12 @@ Object.defineProperty(exports, 'nextTick', {
708
819
  return nextTick;
709
820
  }
710
821
  });
822
+ Object.defineProperty(exports, 'onScopeDispose', {
823
+ enumerable: true,
824
+ get: function () {
825
+ return onScopeDispose;
826
+ }
827
+ });
711
828
  Object.defineProperty(exports, 'queueJob', {
712
829
  enumerable: true,
713
830
  get: function () {
@@ -732,6 +849,12 @@ Object.defineProperty(exports, 'shallowReactive', {
732
849
  return shallowReactive;
733
850
  }
734
851
  });
852
+ Object.defineProperty(exports, 'startBatch', {
853
+ enumerable: true,
854
+ get: function () {
855
+ return startBatch;
856
+ }
857
+ });
735
858
  Object.defineProperty(exports, 'stop', {
736
859
  enumerable: true,
737
860
  get: function () {
@@ -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
@@ -228,7 +316,6 @@ function convertToReactive(value) {
228
316
  return isObject$1(value) ? reactive(value) : value;
229
317
  }
230
318
  /**
231
- * Establish a dependency on the whole reactive object "version".
232
319
  * 让 effect 订阅整个对象的“版本号”,无需深度遍历即可对任何字段变化做出响应。
233
320
  */
234
321
  function touchReactive(target) {
@@ -292,7 +379,7 @@ function shallowReactive(target) {
292
379
  return proxy;
293
380
  }
294
381
  /**
295
- * Checks if a value is a shallow reactive object
382
+ * 判断一个值是否为 shallowReactive 创建的浅层响应式对象
296
383
  */
297
384
  function isShallowReactive(value) {
298
385
  const raw = toRaw(value);
@@ -365,7 +452,7 @@ var RefImpl = class {
365
452
  };
366
453
  function ref(value) {
367
454
  if (isRef(value)) return value;
368
- return new RefImpl(value);
455
+ return markRaw(new RefImpl(value));
369
456
  }
370
457
  function unref(value) {
371
458
  return isRef(value) ? value.value : value;
@@ -389,7 +476,7 @@ var CustomRefImpl = class {
389
476
  }
390
477
  };
391
478
  function customRef(factory, defaultValue) {
392
- return new CustomRefImpl(factory, defaultValue);
479
+ return markRaw(new CustomRefImpl(factory, defaultValue));
393
480
  }
394
481
 
395
482
  //#endregion
@@ -635,4 +722,4 @@ function storeToRefs(store) {
635
722
  }
636
723
 
637
724
  //#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 };
725
+ 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 };
package/dist/store.cjs CHANGED
@@ -1,4 +1,4 @@
1
- const require_store = require('./store-Cmw9vWBT.cjs');
1
+ const require_store = require('./store-DTqmKv0w.cjs');
2
2
 
3
3
  exports.createStore = require_store.createStore;
4
4
  exports.defineStore = require_store.defineStore;
package/dist/store.d.cts CHANGED
@@ -1,2 +1,2 @@
1
- import { a as DefineStoreOptions, c as SubscriptionCallback, i as ActionSubscriber, n as createStore, o as MutationType, r as defineStore, s as StoreManager, t as storeToRefs } from "./index-DVEFI-Uo.cjs";
1
+ import { a as DefineStoreOptions, c as SubscriptionCallback, i as ActionSubscriber, n as createStore, o as MutationType, r as defineStore, s as StoreManager, t as storeToRefs } from "./index-B63NgJPS.cjs";
2
2
  export { ActionSubscriber, DefineStoreOptions, MutationType, StoreManager, SubscriptionCallback, createStore, defineStore, storeToRefs };
package/dist/store.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- import { a as DefineStoreOptions, c as SubscriptionCallback, i as ActionSubscriber, n as createStore, o as MutationType, r as defineStore, s as StoreManager, t as storeToRefs } from "./index-0dF4y5p6.mjs";
1
+ import { a as DefineStoreOptions, c as SubscriptionCallback, i as ActionSubscriber, n as createStore, o as MutationType, r as defineStore, s as StoreManager, t as storeToRefs } from "./index-Bq83Mwxo.mjs";
2
2
  export { ActionSubscriber, DefineStoreOptions, MutationType, StoreManager, SubscriptionCallback, createStore, defineStore, storeToRefs };
package/dist/store.mjs CHANGED
@@ -1,3 +1,3 @@
1
- import { n as defineStore, r as createStore, t as storeToRefs } from "./store-BcU7YVhB.mjs";
1
+ import { n as defineStore, r as createStore, t as storeToRefs } from "./store-YHZDsE3y.mjs";
2
2
 
3
3
  export { createStore, defineStore, storeToRefs };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "wevu",
3
3
  "type": "module",
4
- "version": "1.0.0-alpha.1",
4
+ "version": "1.0.0-alpha.3",
5
5
  "description": "Vue 3 风格的小程序运行时,包含响应式、diff+setData 与轻量状态管理",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -37,6 +37,17 @@
37
37
  "types": "./dist/index.d.cts",
38
38
  "default": "./dist/index.cjs"
39
39
  }
40
+ },
41
+ "./compiler": {
42
+ "types": "./dist/compiler.d.mts",
43
+ "import": {
44
+ "types": "./dist/compiler.d.mts",
45
+ "default": "./dist/compiler.mjs"
46
+ },
47
+ "require": {
48
+ "types": "./dist/compiler.d.cts",
49
+ "default": "./dist/compiler.cjs"
50
+ }
40
51
  }
41
52
  },
42
53
  "main": "./dist/index.cjs",
@@ -52,6 +63,9 @@
52
63
  "engines": {
53
64
  "node": ">=20.19.0"
54
65
  },
66
+ "peerDependencies": {
67
+ "miniprogram-api-typings": "^4.1.2"
68
+ },
55
69
  "scripts": {
56
70
  "dev": "tsdown -w",
57
71
  "build": "tsdown",