@vue/reactivity 3.5.16 → 3.6.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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @vue/reactivity v3.5.16
2
+ * @vue/reactivity v3.6.0-alpha.1
3
3
  * (c) 2018-present Yuxi (Evan) You and Vue contributors
4
4
  * @license MIT
5
5
  **/
@@ -9,579 +9,287 @@ Object.defineProperty(exports, '__esModule', { value: true });
9
9
 
10
10
  var shared = require('@vue/shared');
11
11
 
12
- let activeEffectScope;
13
- class EffectScope {
14
- constructor(detached = false) {
15
- this.detached = detached;
16
- /**
17
- * @internal
18
- */
19
- this._active = true;
20
- /**
21
- * @internal track `on` calls, allow `on` call multiple times
22
- */
23
- this._on = 0;
24
- /**
25
- * @internal
26
- */
27
- this.effects = [];
28
- /**
29
- * @internal
30
- */
31
- this.cleanups = [];
32
- this._isPaused = false;
33
- this.parent = activeEffectScope;
34
- if (!detached && activeEffectScope) {
35
- this.index = (activeEffectScope.scopes || (activeEffectScope.scopes = [])).push(
36
- this
37
- ) - 1;
38
- }
39
- }
40
- get active() {
41
- return this._active;
42
- }
43
- pause() {
44
- if (this._active) {
45
- this._isPaused = true;
46
- let i, l;
47
- if (this.scopes) {
48
- for (i = 0, l = this.scopes.length; i < l; i++) {
49
- this.scopes[i].pause();
50
- }
51
- }
52
- for (i = 0, l = this.effects.length; i < l; i++) {
53
- this.effects[i].pause();
54
- }
55
- }
56
- }
57
- /**
58
- * Resumes the effect scope, including all child scopes and effects.
59
- */
60
- resume() {
61
- if (this._active) {
62
- if (this._isPaused) {
63
- this._isPaused = false;
64
- let i, l;
65
- if (this.scopes) {
66
- for (i = 0, l = this.scopes.length; i < l; i++) {
67
- this.scopes[i].resume();
68
- }
69
- }
70
- for (i = 0, l = this.effects.length; i < l; i++) {
71
- this.effects[i].resume();
72
- }
73
- }
74
- }
75
- }
76
- run(fn) {
77
- if (this._active) {
78
- const currentEffectScope = activeEffectScope;
79
- try {
80
- activeEffectScope = this;
81
- return fn();
82
- } finally {
83
- activeEffectScope = currentEffectScope;
84
- }
85
- }
86
- }
87
- /**
88
- * This should only be called on non-detached scopes
89
- * @internal
90
- */
91
- on() {
92
- if (++this._on === 1) {
93
- this.prevScope = activeEffectScope;
94
- activeEffectScope = this;
95
- }
96
- }
97
- /**
98
- * This should only be called on non-detached scopes
99
- * @internal
100
- */
101
- off() {
102
- if (this._on > 0 && --this._on === 0) {
103
- activeEffectScope = this.prevScope;
104
- this.prevScope = void 0;
105
- }
106
- }
107
- stop(fromParent) {
108
- if (this._active) {
109
- this._active = false;
110
- let i, l;
111
- for (i = 0, l = this.effects.length; i < l; i++) {
112
- this.effects[i].stop();
113
- }
114
- this.effects.length = 0;
115
- for (i = 0, l = this.cleanups.length; i < l; i++) {
116
- this.cleanups[i]();
117
- }
118
- this.cleanups.length = 0;
119
- if (this.scopes) {
120
- for (i = 0, l = this.scopes.length; i < l; i++) {
121
- this.scopes[i].stop(true);
122
- }
123
- this.scopes.length = 0;
124
- }
125
- if (!this.detached && this.parent && !fromParent) {
126
- const last = this.parent.scopes.pop();
127
- if (last && last !== this) {
128
- this.parent.scopes[this.index] = last;
129
- last.index = this.index;
130
- }
131
- }
132
- this.parent = void 0;
133
- }
134
- }
135
- }
136
- function effectScope(detached) {
137
- return new EffectScope(detached);
138
- }
139
- function getCurrentScope() {
140
- return activeEffectScope;
141
- }
142
- function onScopeDispose(fn, failSilently = false) {
143
- if (activeEffectScope) {
144
- activeEffectScope.cleanups.push(fn);
145
- }
146
- }
147
-
148
- let activeSub;
149
- const EffectFlags = {
150
- "ACTIVE": 1,
151
- "1": "ACTIVE",
152
- "RUNNING": 2,
153
- "2": "RUNNING",
154
- "TRACKING": 4,
155
- "4": "TRACKING",
156
- "NOTIFIED": 8,
157
- "8": "NOTIFIED",
158
- "DIRTY": 16,
159
- "16": "DIRTY",
160
- "ALLOW_RECURSE": 32,
161
- "32": "ALLOW_RECURSE",
162
- "PAUSED": 64,
163
- "64": "PAUSED",
164
- "EVALUATED": 128,
165
- "128": "EVALUATED"
166
- };
167
- const pausedQueueEffects = /* @__PURE__ */ new WeakSet();
168
- class ReactiveEffect {
169
- constructor(fn) {
170
- this.fn = fn;
171
- /**
172
- * @internal
173
- */
174
- this.deps = void 0;
175
- /**
176
- * @internal
177
- */
178
- this.depsTail = void 0;
179
- /**
180
- * @internal
181
- */
182
- this.flags = 1 | 4;
183
- /**
184
- * @internal
185
- */
186
- this.next = void 0;
187
- /**
188
- * @internal
189
- */
190
- this.cleanup = void 0;
191
- this.scheduler = void 0;
192
- if (activeEffectScope && activeEffectScope.active) {
193
- activeEffectScope.effects.push(this);
194
- }
195
- }
196
- pause() {
197
- this.flags |= 64;
198
- }
199
- resume() {
200
- if (this.flags & 64) {
201
- this.flags &= -65;
202
- if (pausedQueueEffects.has(this)) {
203
- pausedQueueEffects.delete(this);
204
- this.trigger();
205
- }
206
- }
207
- }
208
- /**
209
- * @internal
210
- */
211
- notify() {
212
- if (this.flags & 2 && !(this.flags & 32)) {
213
- return;
214
- }
215
- if (!(this.flags & 8)) {
216
- batch(this);
217
- }
218
- }
219
- run() {
220
- if (!(this.flags & 1)) {
221
- return this.fn();
222
- }
223
- this.flags |= 2;
224
- cleanupEffect(this);
225
- prepareDeps(this);
226
- const prevEffect = activeSub;
227
- const prevShouldTrack = shouldTrack;
228
- activeSub = this;
229
- shouldTrack = true;
230
- try {
231
- return this.fn();
232
- } finally {
233
- cleanupDeps(this);
234
- activeSub = prevEffect;
235
- shouldTrack = prevShouldTrack;
236
- this.flags &= -3;
237
- }
238
- }
239
- stop() {
240
- if (this.flags & 1) {
241
- for (let link = this.deps; link; link = link.nextDep) {
242
- removeSub(link);
243
- }
244
- this.deps = this.depsTail = void 0;
245
- cleanupEffect(this);
246
- this.onStop && this.onStop();
247
- this.flags &= -2;
248
- }
249
- }
250
- trigger() {
251
- if (this.flags & 64) {
252
- pausedQueueEffects.add(this);
253
- } else if (this.scheduler) {
254
- this.scheduler();
255
- } else {
256
- this.runIfDirty();
257
- }
258
- }
259
- /**
260
- * @internal
261
- */
262
- runIfDirty() {
263
- if (isDirty(this)) {
264
- this.run();
265
- }
266
- }
267
- get dirty() {
268
- return isDirty(this);
269
- }
270
- }
12
+ var ReactiveFlags$1 = /* @__PURE__ */ ((ReactiveFlags2) => {
13
+ ReactiveFlags2[ReactiveFlags2["None"] = 0] = "None";
14
+ ReactiveFlags2[ReactiveFlags2["Mutable"] = 1] = "Mutable";
15
+ ReactiveFlags2[ReactiveFlags2["Watching"] = 2] = "Watching";
16
+ ReactiveFlags2[ReactiveFlags2["RecursedCheck"] = 4] = "RecursedCheck";
17
+ ReactiveFlags2[ReactiveFlags2["Recursed"] = 8] = "Recursed";
18
+ ReactiveFlags2[ReactiveFlags2["Dirty"] = 16] = "Dirty";
19
+ ReactiveFlags2[ReactiveFlags2["Pending"] = 32] = "Pending";
20
+ return ReactiveFlags2;
21
+ })(ReactiveFlags$1 || {});
22
+ const notifyBuffer = [];
271
23
  let batchDepth = 0;
272
- let batchedSub;
273
- let batchedComputed;
274
- function batch(sub, isComputed = false) {
275
- sub.flags |= 8;
276
- if (isComputed) {
277
- sub.next = batchedComputed;
278
- batchedComputed = sub;
279
- return;
24
+ let activeSub = void 0;
25
+ let notifyIndex = 0;
26
+ let notifyBufferLength = 0;
27
+ function setActiveSub(sub) {
28
+ try {
29
+ return activeSub;
30
+ } finally {
31
+ activeSub = sub;
280
32
  }
281
- sub.next = batchedSub;
282
- batchedSub = sub;
283
33
  }
284
34
  function startBatch() {
285
- batchDepth++;
35
+ ++batchDepth;
286
36
  }
287
37
  function endBatch() {
288
- if (--batchDepth > 0) {
289
- return;
290
- }
291
- if (batchedComputed) {
292
- let e = batchedComputed;
293
- batchedComputed = void 0;
294
- while (e) {
295
- const next = e.next;
296
- e.next = void 0;
297
- e.flags &= -9;
298
- e = next;
299
- }
300
- }
301
- let error;
302
- while (batchedSub) {
303
- let e = batchedSub;
304
- batchedSub = void 0;
305
- while (e) {
306
- const next = e.next;
307
- e.next = void 0;
308
- e.flags &= -9;
309
- if (e.flags & 1) {
310
- try {
311
- ;
312
- e.trigger();
313
- } catch (err) {
314
- if (!error) error = err;
315
- }
316
- }
317
- e = next;
318
- }
319
- }
320
- if (error) throw error;
321
- }
322
- function prepareDeps(sub) {
323
- for (let link = sub.deps; link; link = link.nextDep) {
324
- link.version = -1;
325
- link.prevActiveLink = link.dep.activeLink;
326
- link.dep.activeLink = link;
327
- }
328
- }
329
- function cleanupDeps(sub) {
330
- let head;
331
- let tail = sub.depsTail;
332
- let link = tail;
333
- while (link) {
334
- const prev = link.prevDep;
335
- if (link.version === -1) {
336
- if (link === tail) tail = prev;
337
- removeSub(link);
338
- removeDep(link);
339
- } else {
340
- head = link;
341
- }
342
- link.dep.activeLink = link.prevActiveLink;
343
- link.prevActiveLink = void 0;
344
- link = prev;
345
- }
346
- sub.deps = head;
347
- sub.depsTail = tail;
348
- }
349
- function isDirty(sub) {
350
- for (let link = sub.deps; link; link = link.nextDep) {
351
- if (link.dep.version !== link.version || link.dep.computed && (refreshComputed(link.dep.computed) || link.dep.version !== link.version)) {
352
- return true;
353
- }
354
- }
355
- if (sub._dirty) {
356
- return true;
38
+ if (!--batchDepth && notifyBufferLength) {
39
+ flush();
357
40
  }
358
- return false;
359
41
  }
360
- function refreshComputed(computed) {
361
- if (computed.flags & 4 && !(computed.flags & 16)) {
42
+ function link(dep, sub) {
43
+ const prevDep = sub.depsTail;
44
+ if (prevDep !== void 0 && prevDep.dep === dep) {
362
45
  return;
363
46
  }
364
- computed.flags &= -17;
365
- if (computed.globalVersion === globalVersion) {
366
- return;
47
+ let nextDep = void 0;
48
+ const recursedCheck = sub.flags & 4 /* RecursedCheck */;
49
+ if (recursedCheck) {
50
+ nextDep = prevDep !== void 0 ? prevDep.nextDep : sub.deps;
51
+ if (nextDep !== void 0 && nextDep.dep === dep) {
52
+ sub.depsTail = nextDep;
53
+ return;
54
+ }
367
55
  }
368
- computed.globalVersion = globalVersion;
369
- if (!computed.isSSR && computed.flags & 128 && (!computed.deps && !computed._dirty || !isDirty(computed))) {
56
+ const prevSub = dep.subsTail;
57
+ if (prevSub !== void 0 && prevSub.sub === sub && (!recursedCheck || isValidLink(prevSub, sub))) {
370
58
  return;
371
59
  }
372
- computed.flags |= 2;
373
- const dep = computed.dep;
374
- const prevSub = activeSub;
375
- const prevShouldTrack = shouldTrack;
376
- activeSub = computed;
377
- shouldTrack = true;
378
- try {
379
- prepareDeps(computed);
380
- const value = computed.fn(computed._value);
381
- if (dep.version === 0 || shared.hasChanged(value, computed._value)) {
382
- computed.flags |= 128;
383
- computed._value = value;
384
- dep.version++;
385
- }
386
- } catch (err) {
387
- dep.version++;
388
- throw err;
389
- } finally {
390
- activeSub = prevSub;
391
- shouldTrack = prevShouldTrack;
392
- cleanupDeps(computed);
393
- computed.flags &= -3;
394
- }
395
- }
396
- function removeSub(link, soft = false) {
397
- const { dep, prevSub, nextSub } = link;
398
- if (prevSub) {
399
- prevSub.nextSub = nextSub;
400
- link.prevSub = void 0;
60
+ const newLink = sub.depsTail = dep.subsTail = {
61
+ dep,
62
+ sub,
63
+ prevDep,
64
+ nextDep,
65
+ prevSub,
66
+ nextSub: void 0
67
+ };
68
+ if (nextDep !== void 0) {
69
+ nextDep.prevDep = newLink;
401
70
  }
402
- if (nextSub) {
403
- nextSub.prevSub = prevSub;
404
- link.nextSub = void 0;
405
- }
406
- if (dep.subs === link) {
407
- dep.subs = prevSub;
408
- if (!prevSub && dep.computed) {
409
- dep.computed.flags &= -5;
410
- for (let l = dep.computed.deps; l; l = l.nextDep) {
411
- removeSub(l, true);
412
- }
413
- }
71
+ if (prevDep !== void 0) {
72
+ prevDep.nextDep = newLink;
73
+ } else {
74
+ sub.deps = newLink;
414
75
  }
415
- if (!soft && !--dep.sc && dep.map) {
416
- dep.map.delete(dep.key);
76
+ if (prevSub !== void 0) {
77
+ prevSub.nextSub = newLink;
78
+ } else {
79
+ dep.subs = newLink;
417
80
  }
418
81
  }
419
- function removeDep(link) {
420
- const { prevDep, nextDep } = link;
421
- if (prevDep) {
422
- prevDep.nextDep = nextDep;
423
- link.prevDep = void 0;
424
- }
425
- if (nextDep) {
82
+ function unlink(link2, sub = link2.sub) {
83
+ const dep = link2.dep;
84
+ const prevDep = link2.prevDep;
85
+ const nextDep = link2.nextDep;
86
+ const nextSub = link2.nextSub;
87
+ const prevSub = link2.prevSub;
88
+ if (nextDep !== void 0) {
426
89
  nextDep.prevDep = prevDep;
427
- link.nextDep = void 0;
428
- }
429
- }
430
- function effect(fn, options) {
431
- if (fn.effect instanceof ReactiveEffect) {
432
- fn = fn.effect.fn;
433
- }
434
- const e = new ReactiveEffect(fn);
435
- if (options) {
436
- shared.extend(e, options);
437
- }
438
- try {
439
- e.run();
440
- } catch (err) {
441
- e.stop();
442
- throw err;
443
- }
444
- const runner = e.run.bind(e);
445
- runner.effect = e;
446
- return runner;
447
- }
448
- function stop(runner) {
449
- runner.effect.stop();
450
- }
451
- let shouldTrack = true;
452
- const trackStack = [];
453
- function pauseTracking() {
454
- trackStack.push(shouldTrack);
455
- shouldTrack = false;
456
- }
457
- function enableTracking() {
458
- trackStack.push(shouldTrack);
459
- shouldTrack = true;
460
- }
461
- function resetTracking() {
462
- const last = trackStack.pop();
463
- shouldTrack = last === void 0 ? true : last;
464
- }
465
- function onEffectCleanup(fn, failSilently = false) {
466
- if (activeSub instanceof ReactiveEffect) {
467
- activeSub.cleanup = fn;
468
- }
469
- }
470
- function cleanupEffect(e) {
471
- const { cleanup } = e;
472
- e.cleanup = void 0;
473
- if (cleanup) {
474
- const prevSub = activeSub;
475
- activeSub = void 0;
476
- try {
477
- cleanup();
478
- } finally {
479
- activeSub = prevSub;
480
- }
90
+ } else {
91
+ sub.depsTail = prevDep;
481
92
  }
482
- }
483
-
484
- let globalVersion = 0;
485
- class Link {
486
- constructor(sub, dep) {
487
- this.sub = sub;
488
- this.dep = dep;
489
- this.version = dep.version;
490
- this.nextDep = this.prevDep = this.nextSub = this.prevSub = this.prevActiveLink = void 0;
93
+ if (prevDep !== void 0) {
94
+ prevDep.nextDep = nextDep;
95
+ } else {
96
+ sub.deps = nextDep;
491
97
  }
492
- }
493
- class Dep {
494
- constructor(computed) {
495
- this.computed = computed;
496
- this.version = 0;
497
- /**
498
- * Link between this dep and the current active effect
499
- */
500
- this.activeLink = void 0;
501
- /**
502
- * Doubly linked list representing the subscribing effects (tail)
503
- */
504
- this.subs = void 0;
505
- /**
506
- * For object property deps cleanup
507
- */
508
- this.map = void 0;
509
- this.key = void 0;
510
- /**
511
- * Subscriber counter
512
- */
513
- this.sc = 0;
98
+ if (nextSub !== void 0) {
99
+ nextSub.prevSub = prevSub;
100
+ } else {
101
+ dep.subsTail = prevSub;
514
102
  }
515
- track(debugInfo) {
516
- if (!activeSub || !shouldTrack || activeSub === this.computed) {
517
- return;
518
- }
519
- let link = this.activeLink;
520
- if (link === void 0 || link.sub !== activeSub) {
521
- link = this.activeLink = new Link(activeSub, this);
522
- if (!activeSub.deps) {
523
- activeSub.deps = activeSub.depsTail = link;
103
+ if (prevSub !== void 0) {
104
+ prevSub.nextSub = nextSub;
105
+ } else if ((dep.subs = nextSub) === void 0) {
106
+ let toRemove = dep.deps;
107
+ if (toRemove !== void 0) {
108
+ do {
109
+ toRemove = unlink(toRemove, dep);
110
+ } while (toRemove !== void 0);
111
+ dep.flags |= 16 /* Dirty */;
112
+ }
113
+ }
114
+ return nextDep;
115
+ }
116
+ function propagate(link2) {
117
+ let next = link2.nextSub;
118
+ let stack;
119
+ top: do {
120
+ const sub = link2.sub;
121
+ let flags = sub.flags;
122
+ if (flags & (1 /* Mutable */ | 2 /* Watching */)) {
123
+ if (!(flags & (4 /* RecursedCheck */ | 8 /* Recursed */ | 16 /* Dirty */ | 32 /* Pending */))) {
124
+ sub.flags = flags | 32 /* Pending */;
125
+ } else if (!(flags & (4 /* RecursedCheck */ | 8 /* Recursed */))) {
126
+ flags = 0 /* None */;
127
+ } else if (!(flags & 4 /* RecursedCheck */)) {
128
+ sub.flags = flags & -9 /* Recursed */ | 32 /* Pending */;
129
+ } else if (!(flags & (16 /* Dirty */ | 32 /* Pending */)) && isValidLink(link2, sub)) {
130
+ sub.flags = flags | 8 /* Recursed */ | 32 /* Pending */;
131
+ flags &= 1 /* Mutable */;
524
132
  } else {
525
- link.prevDep = activeSub.depsTail;
526
- activeSub.depsTail.nextDep = link;
527
- activeSub.depsTail = link;
133
+ flags = 0 /* None */;
528
134
  }
529
- addSub(link);
530
- } else if (link.version === -1) {
531
- link.version = this.version;
532
- if (link.nextDep) {
533
- const next = link.nextDep;
534
- next.prevDep = link.prevDep;
535
- if (link.prevDep) {
536
- link.prevDep.nextDep = next;
537
- }
538
- link.prevDep = activeSub.depsTail;
539
- link.nextDep = void 0;
540
- activeSub.depsTail.nextDep = link;
541
- activeSub.depsTail = link;
542
- if (activeSub.deps === link) {
543
- activeSub.deps = next;
135
+ if (flags & 2 /* Watching */) {
136
+ notifyBuffer[notifyBufferLength++] = sub;
137
+ }
138
+ if (flags & 1 /* Mutable */) {
139
+ const subSubs = sub.subs;
140
+ if (subSubs !== void 0) {
141
+ link2 = subSubs;
142
+ if (subSubs.nextSub !== void 0) {
143
+ stack = { value: next, prev: stack };
144
+ next = link2.nextSub;
145
+ }
146
+ continue;
544
147
  }
545
148
  }
546
149
  }
547
- return link;
548
- }
549
- trigger(debugInfo) {
550
- this.version++;
551
- globalVersion++;
552
- this.notify(debugInfo);
553
- }
554
- notify(debugInfo) {
555
- startBatch();
556
- try {
557
- if (false) ;
558
- for (let link = this.subs; link; link = link.prevSub) {
559
- if (link.sub.notify()) {
560
- ;
561
- link.sub.dep.notify();
562
- }
150
+ if ((link2 = next) !== void 0) {
151
+ next = link2.nextSub;
152
+ continue;
153
+ }
154
+ while (stack !== void 0) {
155
+ link2 = stack.value;
156
+ stack = stack.prev;
157
+ if (link2 !== void 0) {
158
+ next = link2.nextSub;
159
+ continue top;
563
160
  }
564
- } finally {
565
- endBatch();
566
161
  }
162
+ break;
163
+ } while (true);
164
+ }
165
+ function startTracking(sub) {
166
+ sub.depsTail = void 0;
167
+ sub.flags = sub.flags & -57 | 4 /* RecursedCheck */;
168
+ return setActiveSub(sub);
169
+ }
170
+ function endTracking(sub, prevSub) {
171
+ activeSub = prevSub;
172
+ const depsTail = sub.depsTail;
173
+ let toRemove = depsTail !== void 0 ? depsTail.nextDep : sub.deps;
174
+ while (toRemove !== void 0) {
175
+ toRemove = unlink(toRemove, sub);
176
+ }
177
+ sub.flags &= -5 /* RecursedCheck */;
178
+ }
179
+ function flush() {
180
+ while (notifyIndex < notifyBufferLength) {
181
+ const effect = notifyBuffer[notifyIndex];
182
+ notifyBuffer[notifyIndex++] = void 0;
183
+ effect.notify();
184
+ }
185
+ notifyIndex = 0;
186
+ notifyBufferLength = 0;
187
+ }
188
+ function checkDirty(link2, sub) {
189
+ let stack;
190
+ let checkDepth = 0;
191
+ top: do {
192
+ const dep = link2.dep;
193
+ const depFlags = dep.flags;
194
+ let dirty = false;
195
+ if (sub.flags & 16 /* Dirty */) {
196
+ dirty = true;
197
+ } else if ((depFlags & (1 /* Mutable */ | 16 /* Dirty */)) === (1 /* Mutable */ | 16 /* Dirty */)) {
198
+ if (dep.update()) {
199
+ const subs = dep.subs;
200
+ if (subs.nextSub !== void 0) {
201
+ shallowPropagate(subs);
202
+ }
203
+ dirty = true;
204
+ }
205
+ } else if ((depFlags & (1 /* Mutable */ | 32 /* Pending */)) === (1 /* Mutable */ | 32 /* Pending */)) {
206
+ if (link2.nextSub !== void 0 || link2.prevSub !== void 0) {
207
+ stack = { value: link2, prev: stack };
208
+ }
209
+ link2 = dep.deps;
210
+ sub = dep;
211
+ ++checkDepth;
212
+ continue;
213
+ }
214
+ if (!dirty && link2.nextDep !== void 0) {
215
+ link2 = link2.nextDep;
216
+ continue;
217
+ }
218
+ while (checkDepth) {
219
+ --checkDepth;
220
+ const firstSub = sub.subs;
221
+ const hasMultipleSubs = firstSub.nextSub !== void 0;
222
+ if (hasMultipleSubs) {
223
+ link2 = stack.value;
224
+ stack = stack.prev;
225
+ } else {
226
+ link2 = firstSub;
227
+ }
228
+ if (dirty) {
229
+ if (sub.update()) {
230
+ if (hasMultipleSubs) {
231
+ shallowPropagate(firstSub);
232
+ }
233
+ sub = link2.sub;
234
+ continue;
235
+ }
236
+ } else {
237
+ sub.flags &= -33 /* Pending */;
238
+ }
239
+ sub = link2.sub;
240
+ if (link2.nextDep !== void 0) {
241
+ link2 = link2.nextDep;
242
+ continue top;
243
+ }
244
+ dirty = false;
245
+ }
246
+ return dirty;
247
+ } while (true);
248
+ }
249
+ function shallowPropagate(link2) {
250
+ do {
251
+ const sub = link2.sub;
252
+ const nextSub = link2.nextSub;
253
+ const subFlags = sub.flags;
254
+ if ((subFlags & (32 /* Pending */ | 16 /* Dirty */)) === 32 /* Pending */) {
255
+ sub.flags = subFlags | 16 /* Dirty */;
256
+ }
257
+ link2 = nextSub;
258
+ } while (link2 !== void 0);
259
+ }
260
+ function isValidLink(checkLink, sub) {
261
+ const depsTail = sub.depsTail;
262
+ if (depsTail !== void 0) {
263
+ let link2 = sub.deps;
264
+ do {
265
+ if (link2 === checkLink) {
266
+ return true;
267
+ }
268
+ if (link2 === depsTail) {
269
+ break;
270
+ }
271
+ link2 = link2.nextDep;
272
+ } while (link2 !== void 0);
567
273
  }
274
+ return false;
568
275
  }
569
- function addSub(link) {
570
- link.dep.sc++;
571
- if (link.sub.flags & 4) {
572
- const computed = link.dep.computed;
573
- if (computed && !link.dep.subs) {
574
- computed.flags |= 4 | 16;
575
- for (let l = computed.deps; l; l = l.nextDep) {
576
- addSub(l);
577
- }
578
- }
579
- const currentTail = link.dep.subs;
580
- if (currentTail !== link) {
581
- link.prevSub = currentTail;
582
- if (currentTail) currentTail.nextSub = link;
276
+
277
+ class Dep {
278
+ constructor(map, key) {
279
+ this.map = map;
280
+ this.key = key;
281
+ this._subs = void 0;
282
+ this.subsTail = void 0;
283
+ this.flags = ReactiveFlags$1.None;
284
+ }
285
+ get subs() {
286
+ return this._subs;
287
+ }
288
+ set subs(value) {
289
+ this._subs = value;
290
+ if (value === void 0) {
291
+ this.map.delete(this.key);
583
292
  }
584
- link.dep.subs = link;
585
293
  }
586
294
  }
587
295
  const targetMap = /* @__PURE__ */ new WeakMap();
@@ -595,33 +303,27 @@ const ARRAY_ITERATE_KEY = Symbol(
595
303
  ""
596
304
  );
597
305
  function track(target, type, key) {
598
- if (shouldTrack && activeSub) {
306
+ if (activeSub !== void 0) {
599
307
  let depsMap = targetMap.get(target);
600
308
  if (!depsMap) {
601
309
  targetMap.set(target, depsMap = /* @__PURE__ */ new Map());
602
310
  }
603
311
  let dep = depsMap.get(key);
604
312
  if (!dep) {
605
- depsMap.set(key, dep = new Dep());
606
- dep.map = depsMap;
607
- dep.key = key;
608
- }
609
- {
610
- dep.track();
313
+ depsMap.set(key, dep = new Dep(depsMap, key));
611
314
  }
315
+ link(dep, activeSub);
612
316
  }
613
317
  }
614
318
  function trigger(target, type, key, newValue, oldValue, oldTarget) {
615
319
  const depsMap = targetMap.get(target);
616
320
  if (!depsMap) {
617
- globalVersion++;
618
321
  return;
619
322
  }
620
323
  const run = (dep) => {
621
- if (dep) {
622
- {
623
- dep.trigger();
624
- }
324
+ if (dep !== void 0 && dep.subs !== void 0) {
325
+ propagate(dep.subs);
326
+ shallowPropagate(dep.subs);
625
327
  }
626
328
  };
627
329
  startBatch();
@@ -846,11 +548,11 @@ function searchProxy(self, method, args) {
846
548
  return res;
847
549
  }
848
550
  function noTracking(self, method, args = []) {
849
- pauseTracking();
850
551
  startBatch();
552
+ const prevSub = setActiveSub();
851
553
  const res = toRaw(self)[method].apply(self, args);
554
+ setActiveSub(prevSub);
852
555
  endBatch();
853
- resetTracking();
854
556
  return res;
855
557
  }
856
558
 
@@ -896,14 +598,18 @@ class BaseReactiveHandler {
896
598
  return hasOwnProperty;
897
599
  }
898
600
  }
601
+ const wasRef = isRef(target);
899
602
  const res = Reflect.get(
900
603
  target,
901
604
  key,
902
605
  // if this is a proxy wrapping a ref, return methods using the raw ref
903
606
  // as receiver so that we don't have to call `toRaw` on the ref in all
904
607
  // its class methods
905
- isRef(target) ? target : receiver
608
+ wasRef ? target : receiver
906
609
  );
610
+ if (wasRef && key !== "value") {
611
+ return res;
612
+ }
907
613
  if (shared.isSymbol(key) ? builtInSymbols.has(key) : isNonTrackableKeys(key)) {
908
614
  return res;
909
615
  }
@@ -1313,29 +1019,47 @@ function isRef(r) {
1313
1019
  return r ? r["__v_isRef"] === true : false;
1314
1020
  }
1315
1021
  function ref(value) {
1316
- return createRef(value, false);
1022
+ return createRef(value, toReactive);
1317
1023
  }
1318
1024
  function shallowRef(value) {
1319
- return createRef(value, true);
1025
+ return createRef(value);
1320
1026
  }
1321
- function createRef(rawValue, shallow) {
1027
+ function createRef(rawValue, wrap) {
1322
1028
  if (isRef(rawValue)) {
1323
1029
  return rawValue;
1324
1030
  }
1325
- return new RefImpl(rawValue, shallow);
1031
+ return new RefImpl(rawValue, wrap);
1326
1032
  }
1327
1033
  class RefImpl {
1328
- constructor(value, isShallow2) {
1329
- this.dep = new Dep();
1330
- this["__v_isRef"] = true;
1331
- this["__v_isShallow"] = false;
1332
- this._rawValue = isShallow2 ? value : toRaw(value);
1333
- this._value = isShallow2 ? value : toReactive(value);
1334
- this["__v_isShallow"] = isShallow2;
1034
+ // TODO isolatedDeclarations "__v_isShallow"
1035
+ constructor(value, wrap) {
1036
+ this.subs = void 0;
1037
+ this.subsTail = void 0;
1038
+ this.flags = ReactiveFlags$1.Mutable;
1039
+ /**
1040
+ * @internal
1041
+ */
1042
+ this.__v_isRef = true;
1043
+ // TODO isolatedDeclarations "__v_isRef"
1044
+ /**
1045
+ * @internal
1046
+ */
1047
+ this.__v_isShallow = false;
1048
+ this._oldValue = this._rawValue = wrap ? toRaw(value) : value;
1049
+ this._value = wrap ? wrap(value) : value;
1050
+ this._wrap = wrap;
1051
+ this["__v_isShallow"] = !wrap;
1052
+ }
1053
+ get dep() {
1054
+ return this;
1335
1055
  }
1336
1056
  get value() {
1337
- {
1338
- this.dep.track();
1057
+ trackRef(this);
1058
+ if (this.flags & ReactiveFlags$1.Dirty && this.update()) {
1059
+ const subs = this.subs;
1060
+ if (subs !== void 0) {
1061
+ shallowPropagate(subs);
1062
+ }
1339
1063
  }
1340
1064
  return this._value;
1341
1065
  }
@@ -1344,21 +1068,38 @@ class RefImpl {
1344
1068
  const useDirectValue = this["__v_isShallow"] || isShallow(newValue) || isReadonly(newValue);
1345
1069
  newValue = useDirectValue ? newValue : toRaw(newValue);
1346
1070
  if (shared.hasChanged(newValue, oldValue)) {
1071
+ this.flags |= ReactiveFlags$1.Dirty;
1347
1072
  this._rawValue = newValue;
1348
- this._value = useDirectValue ? newValue : toReactive(newValue);
1349
- {
1350
- this.dep.trigger();
1073
+ this._value = !useDirectValue && this._wrap ? this._wrap(newValue) : newValue;
1074
+ const subs = this.subs;
1075
+ if (subs !== void 0) {
1076
+ propagate(subs);
1077
+ if (!batchDepth) {
1078
+ flush();
1079
+ }
1351
1080
  }
1352
1081
  }
1353
1082
  }
1083
+ update() {
1084
+ this.flags &= ~ReactiveFlags$1.Dirty;
1085
+ return shared.hasChanged(this._oldValue, this._oldValue = this._rawValue);
1086
+ }
1354
1087
  }
1355
1088
  function triggerRef(ref2) {
1356
- if (ref2.dep) {
1357
- {
1358
- ref2.dep.trigger();
1089
+ const dep = ref2.dep;
1090
+ if (dep !== void 0 && dep.subs !== void 0) {
1091
+ propagate(dep.subs);
1092
+ shallowPropagate(dep.subs);
1093
+ if (!batchDepth) {
1094
+ flush();
1359
1095
  }
1360
1096
  }
1361
1097
  }
1098
+ function trackRef(dep) {
1099
+ if (activeSub !== void 0) {
1100
+ link(dep, activeSub);
1101
+ }
1102
+ }
1362
1103
  function unref(ref2) {
1363
1104
  return isRef(ref2) ? ref2.value : ref2;
1364
1105
  }
@@ -1382,13 +1123,21 @@ function proxyRefs(objectWithRefs) {
1382
1123
  }
1383
1124
  class CustomRefImpl {
1384
1125
  constructor(factory) {
1126
+ this.subs = void 0;
1127
+ this.subsTail = void 0;
1128
+ this.flags = ReactiveFlags$1.None;
1385
1129
  this["__v_isRef"] = true;
1386
1130
  this._value = void 0;
1387
- const dep = this.dep = new Dep();
1388
- const { get, set } = factory(dep.track.bind(dep), dep.trigger.bind(dep));
1131
+ const { get, set } = factory(
1132
+ () => trackRef(this),
1133
+ () => triggerRef(this)
1134
+ );
1389
1135
  this._get = get;
1390
1136
  this._set = set;
1391
1137
  }
1138
+ get dep() {
1139
+ return this;
1140
+ }
1392
1141
  get value() {
1393
1142
  return this._value = this._get();
1394
1143
  }
@@ -1452,65 +1201,366 @@ function propertyToRef(source, key, defaultValue) {
1452
1201
  return isRef(val) ? val : new ObjectRefImpl(source, key, defaultValue);
1453
1202
  }
1454
1203
 
1455
- class ComputedRefImpl {
1456
- constructor(fn, setter, isSSR) {
1457
- this.fn = fn;
1458
- this.setter = setter;
1459
- /**
1460
- * @internal
1461
- */
1462
- this._value = void 0;
1463
- /**
1464
- * @internal
1465
- */
1466
- this.dep = new Dep(this);
1204
+ const EffectFlags = {
1205
+ "ALLOW_RECURSE": 128,
1206
+ "128": "ALLOW_RECURSE",
1207
+ "PAUSED": 256,
1208
+ "256": "PAUSED",
1209
+ "STOP": 1024,
1210
+ "1024": "STOP"
1211
+ };
1212
+ class ReactiveEffect {
1213
+ constructor(fn) {
1214
+ this.deps = void 0;
1215
+ this.depsTail = void 0;
1216
+ this.subs = void 0;
1217
+ this.subsTail = void 0;
1218
+ this.flags = ReactiveFlags$1.Watching | ReactiveFlags$1.Dirty;
1467
1219
  /**
1468
1220
  * @internal
1469
1221
  */
1470
- this.__v_isRef = true;
1471
- // TODO isolatedDeclarations "__v_isReadonly"
1472
- // A computed is also a subscriber that tracks other deps
1222
+ this.cleanups = [];
1473
1223
  /**
1474
1224
  * @internal
1475
1225
  */
1226
+ this.cleanupsLength = 0;
1227
+ if (fn !== void 0) {
1228
+ this.fn = fn;
1229
+ }
1230
+ if (activeEffectScope) {
1231
+ link(this, activeEffectScope);
1232
+ }
1233
+ }
1234
+ // @ts-expect-error
1235
+ fn() {
1236
+ }
1237
+ get active() {
1238
+ return !(this.flags & 1024);
1239
+ }
1240
+ pause() {
1241
+ this.flags |= 256;
1242
+ }
1243
+ resume() {
1244
+ const flags = this.flags &= -257;
1245
+ if (flags & (ReactiveFlags$1.Dirty | ReactiveFlags$1.Pending)) {
1246
+ this.notify();
1247
+ }
1248
+ }
1249
+ notify() {
1250
+ if (!(this.flags & 256) && this.dirty) {
1251
+ this.run();
1252
+ }
1253
+ }
1254
+ run() {
1255
+ if (!this.active) {
1256
+ return this.fn();
1257
+ }
1258
+ cleanup(this);
1259
+ const prevSub = startTracking(this);
1260
+ try {
1261
+ return this.fn();
1262
+ } finally {
1263
+ endTracking(this, prevSub);
1264
+ const flags = this.flags;
1265
+ if ((flags & (ReactiveFlags$1.Recursed | 128)) === (ReactiveFlags$1.Recursed | 128)) {
1266
+ this.flags = flags & ~ReactiveFlags$1.Recursed;
1267
+ this.notify();
1268
+ }
1269
+ }
1270
+ }
1271
+ stop() {
1272
+ if (!this.active) {
1273
+ return;
1274
+ }
1275
+ this.flags = 1024;
1276
+ let dep = this.deps;
1277
+ while (dep !== void 0) {
1278
+ dep = unlink(dep, this);
1279
+ }
1280
+ const sub = this.subs;
1281
+ if (sub !== void 0) {
1282
+ unlink(sub);
1283
+ }
1284
+ cleanup(this);
1285
+ }
1286
+ get dirty() {
1287
+ const flags = this.flags;
1288
+ if (flags & ReactiveFlags$1.Dirty) {
1289
+ return true;
1290
+ }
1291
+ if (flags & ReactiveFlags$1.Pending) {
1292
+ if (checkDirty(this.deps, this)) {
1293
+ this.flags = flags | ReactiveFlags$1.Dirty;
1294
+ return true;
1295
+ } else {
1296
+ this.flags = flags & ~ReactiveFlags$1.Pending;
1297
+ }
1298
+ }
1299
+ return false;
1300
+ }
1301
+ }
1302
+ function effect(fn, options) {
1303
+ if (fn.effect instanceof ReactiveEffect) {
1304
+ fn = fn.effect.fn;
1305
+ }
1306
+ const e = new ReactiveEffect(fn);
1307
+ if (options) {
1308
+ const { onStop, scheduler } = options;
1309
+ if (onStop) {
1310
+ options.onStop = void 0;
1311
+ const stop2 = e.stop.bind(e);
1312
+ e.stop = () => {
1313
+ stop2();
1314
+ onStop();
1315
+ };
1316
+ }
1317
+ if (scheduler) {
1318
+ options.scheduler = void 0;
1319
+ e.notify = () => {
1320
+ if (!(e.flags & 256)) {
1321
+ scheduler();
1322
+ }
1323
+ };
1324
+ }
1325
+ shared.extend(e, options);
1326
+ }
1327
+ try {
1328
+ e.run();
1329
+ } catch (err) {
1330
+ e.stop();
1331
+ throw err;
1332
+ }
1333
+ const runner = e.run.bind(e);
1334
+ runner.effect = e;
1335
+ return runner;
1336
+ }
1337
+ function stop(runner) {
1338
+ runner.effect.stop();
1339
+ }
1340
+ const resetTrackingStack = [];
1341
+ function pauseTracking() {
1342
+ resetTrackingStack.push(activeSub);
1343
+ setActiveSub();
1344
+ }
1345
+ function enableTracking() {
1346
+ const isPaused = activeSub === void 0;
1347
+ if (!isPaused) {
1348
+ resetTrackingStack.push(activeSub);
1349
+ } else {
1350
+ resetTrackingStack.push(void 0);
1351
+ for (let i = resetTrackingStack.length - 1; i >= 0; i--) {
1352
+ if (resetTrackingStack[i] !== void 0) {
1353
+ setActiveSub(resetTrackingStack[i]);
1354
+ break;
1355
+ }
1356
+ }
1357
+ }
1358
+ }
1359
+ function resetTracking() {
1360
+ if (resetTrackingStack.length) {
1361
+ setActiveSub(resetTrackingStack.pop());
1362
+ } else {
1363
+ setActiveSub();
1364
+ }
1365
+ }
1366
+ function cleanup(sub) {
1367
+ const l = sub.cleanupsLength;
1368
+ if (l) {
1369
+ for (let i = 0; i < l; i++) {
1370
+ sub.cleanups[i]();
1371
+ }
1372
+ sub.cleanupsLength = 0;
1373
+ }
1374
+ }
1375
+ function onEffectCleanup(fn, failSilently = false) {
1376
+ if (activeSub instanceof ReactiveEffect) {
1377
+ activeSub.cleanups[activeSub.cleanupsLength++] = () => cleanupEffect(fn);
1378
+ }
1379
+ }
1380
+ function cleanupEffect(fn) {
1381
+ const prevSub = setActiveSub();
1382
+ try {
1383
+ fn();
1384
+ } finally {
1385
+ setActiveSub(prevSub);
1386
+ }
1387
+ }
1388
+
1389
+ let activeEffectScope;
1390
+ class EffectScope {
1391
+ constructor(detached = false) {
1476
1392
  this.deps = void 0;
1393
+ this.depsTail = void 0;
1394
+ this.subs = void 0;
1395
+ this.subsTail = void 0;
1396
+ this.flags = 0;
1477
1397
  /**
1478
1398
  * @internal
1479
1399
  */
1480
- this.depsTail = void 0;
1400
+ this.cleanups = [];
1481
1401
  /**
1482
1402
  * @internal
1483
1403
  */
1484
- this.flags = 16;
1404
+ this.cleanupsLength = 0;
1405
+ if (!detached && activeEffectScope) {
1406
+ link(this, activeEffectScope);
1407
+ }
1408
+ }
1409
+ get active() {
1410
+ return !(this.flags & 1024);
1411
+ }
1412
+ pause() {
1413
+ if (!(this.flags & 256)) {
1414
+ this.flags |= 256;
1415
+ for (let link2 = this.deps; link2 !== void 0; link2 = link2.nextDep) {
1416
+ const dep = link2.dep;
1417
+ if ("pause" in dep) {
1418
+ dep.pause();
1419
+ }
1420
+ }
1421
+ }
1422
+ }
1423
+ /**
1424
+ * Resumes the effect scope, including all child scopes and effects.
1425
+ */
1426
+ resume() {
1427
+ const flags = this.flags;
1428
+ if (flags & 256) {
1429
+ this.flags = flags & -257;
1430
+ for (let link2 = this.deps; link2 !== void 0; link2 = link2.nextDep) {
1431
+ const dep = link2.dep;
1432
+ if ("resume" in dep) {
1433
+ dep.resume();
1434
+ }
1435
+ }
1436
+ }
1437
+ }
1438
+ run(fn) {
1439
+ const prevSub = setActiveSub();
1440
+ const prevScope = activeEffectScope;
1441
+ try {
1442
+ activeEffectScope = this;
1443
+ return fn();
1444
+ } finally {
1445
+ activeEffectScope = prevScope;
1446
+ setActiveSub(prevSub);
1447
+ }
1448
+ }
1449
+ stop() {
1450
+ if (!this.active) {
1451
+ return;
1452
+ }
1453
+ this.flags = 1024;
1454
+ let dep = this.deps;
1455
+ while (dep !== void 0) {
1456
+ const node = dep.dep;
1457
+ if ("stop" in node) {
1458
+ dep = dep.nextDep;
1459
+ node.stop();
1460
+ } else {
1461
+ dep = unlink(dep, this);
1462
+ }
1463
+ }
1464
+ const sub = this.subs;
1465
+ if (sub !== void 0) {
1466
+ unlink(sub);
1467
+ }
1468
+ cleanup(this);
1469
+ }
1470
+ }
1471
+ function effectScope(detached) {
1472
+ return new EffectScope(detached);
1473
+ }
1474
+ function getCurrentScope() {
1475
+ return activeEffectScope;
1476
+ }
1477
+ function setCurrentScope(scope) {
1478
+ try {
1479
+ return activeEffectScope;
1480
+ } finally {
1481
+ activeEffectScope = scope;
1482
+ }
1483
+ }
1484
+ function onScopeDispose(fn, failSilently = false) {
1485
+ if (activeEffectScope !== void 0) {
1486
+ activeEffectScope.cleanups[activeEffectScope.cleanupsLength++] = fn;
1487
+ }
1488
+ }
1489
+
1490
+ class ComputedRefImpl {
1491
+ constructor(fn, setter) {
1492
+ this.fn = fn;
1493
+ this.setter = setter;
1485
1494
  /**
1486
1495
  * @internal
1487
1496
  */
1488
- this.globalVersion = globalVersion - 1;
1497
+ this._value = void 0;
1498
+ this.subs = void 0;
1499
+ this.subsTail = void 0;
1500
+ this.deps = void 0;
1501
+ this.depsTail = void 0;
1502
+ this.flags = ReactiveFlags$1.Mutable | ReactiveFlags$1.Dirty;
1489
1503
  /**
1490
1504
  * @internal
1491
1505
  */
1492
- this.next = void 0;
1493
- // for backwards compat
1494
- this.effect = this;
1506
+ this.__v_isRef = true;
1495
1507
  this["__v_isReadonly"] = !setter;
1496
- this.isSSR = isSSR;
1508
+ }
1509
+ // TODO isolatedDeclarations "__v_isReadonly"
1510
+ // for backwards compat
1511
+ get effect() {
1512
+ return this;
1513
+ }
1514
+ // for backwards compat
1515
+ get dep() {
1516
+ return this;
1497
1517
  }
1498
1518
  /**
1499
1519
  * @internal
1520
+ * for backwards compat
1500
1521
  */
1501
- notify() {
1502
- this.flags |= 16;
1503
- if (!(this.flags & 8) && // avoid infinite self recursion
1504
- activeSub !== this) {
1505
- batch(this, true);
1522
+ get _dirty() {
1523
+ const flags = this.flags;
1524
+ if (flags & ReactiveFlags$1.Dirty) {
1506
1525
  return true;
1507
1526
  }
1527
+ if (flags & ReactiveFlags$1.Pending) {
1528
+ if (checkDirty(this.deps, this)) {
1529
+ this.flags = flags | ReactiveFlags$1.Dirty;
1530
+ return true;
1531
+ } else {
1532
+ this.flags = flags & ~ReactiveFlags$1.Pending;
1533
+ }
1534
+ }
1535
+ return false;
1536
+ }
1537
+ /**
1538
+ * @internal
1539
+ * for backwards compat
1540
+ */
1541
+ set _dirty(v) {
1542
+ if (v) {
1543
+ this.flags |= ReactiveFlags$1.Dirty;
1544
+ } else {
1545
+ this.flags &= ~(ReactiveFlags$1.Dirty | ReactiveFlags$1.Pending);
1546
+ }
1508
1547
  }
1509
1548
  get value() {
1510
- const link = this.dep.track();
1511
- refreshComputed(this);
1512
- if (link) {
1513
- link.version = this.dep.version;
1549
+ const flags = this.flags;
1550
+ if (flags & ReactiveFlags$1.Dirty || flags & ReactiveFlags$1.Pending && checkDirty(this.deps, this)) {
1551
+ if (this.update()) {
1552
+ const subs = this.subs;
1553
+ if (subs !== void 0) {
1554
+ shallowPropagate(subs);
1555
+ }
1556
+ }
1557
+ } else if (flags & ReactiveFlags$1.Pending) {
1558
+ this.flags = flags & ~ReactiveFlags$1.Pending;
1559
+ }
1560
+ if (activeSub !== void 0) {
1561
+ link(this, activeSub);
1562
+ } else if (activeEffectScope !== void 0) {
1563
+ link(this, activeEffectScope);
1514
1564
  }
1515
1565
  return this._value;
1516
1566
  }
@@ -1519,6 +1569,20 @@ class ComputedRefImpl {
1519
1569
  this.setter(newValue);
1520
1570
  }
1521
1571
  }
1572
+ update() {
1573
+ const prevSub = startTracking(this);
1574
+ try {
1575
+ const oldValue = this._value;
1576
+ const newValue = this.fn(oldValue);
1577
+ if (shared.hasChanged(oldValue, newValue)) {
1578
+ this._value = newValue;
1579
+ return true;
1580
+ }
1581
+ return false;
1582
+ } finally {
1583
+ endTracking(this, prevSub);
1584
+ }
1585
+ }
1522
1586
  }
1523
1587
  function computed(getterOrOptions, debugOptions, isSSR = false) {
1524
1588
  let getter;
@@ -1529,7 +1593,7 @@ function computed(getterOrOptions, debugOptions, isSSR = false) {
1529
1593
  getter = getterOrOptions.get;
1530
1594
  setter = getterOrOptions.set;
1531
1595
  }
1532
- const cRef = new ComputedRefImpl(getter, setter, isSSR);
1596
+ const cRef = new ComputedRefImpl(getter, setter);
1533
1597
  return cRef;
1534
1598
  }
1535
1599
 
@@ -1562,159 +1626,137 @@ const WatchErrorCodes = {
1562
1626
  "4": "WATCH_CLEANUP"
1563
1627
  };
1564
1628
  const INITIAL_WATCHER_VALUE = {};
1565
- const cleanupMap = /* @__PURE__ */ new WeakMap();
1566
1629
  let activeWatcher = void 0;
1567
1630
  function getCurrentWatcher() {
1568
1631
  return activeWatcher;
1569
1632
  }
1570
1633
  function onWatcherCleanup(cleanupFn, failSilently = false, owner = activeWatcher) {
1571
1634
  if (owner) {
1572
- let cleanups = cleanupMap.get(owner);
1573
- if (!cleanups) cleanupMap.set(owner, cleanups = []);
1574
- cleanups.push(cleanupFn);
1575
- }
1576
- }
1577
- function watch(source, cb, options = shared.EMPTY_OBJ) {
1578
- const { immediate, deep, once, scheduler, augmentJob, call } = options;
1579
- const reactiveGetter = (source2) => {
1580
- if (deep) return source2;
1581
- if (isShallow(source2) || deep === false || deep === 0)
1582
- return traverse(source2, 1);
1583
- return traverse(source2);
1584
- };
1585
- let effect;
1586
- let getter;
1587
- let cleanup;
1588
- let boundCleanup;
1589
- let forceTrigger = false;
1590
- let isMultiSource = false;
1591
- if (isRef(source)) {
1592
- getter = () => source.value;
1593
- forceTrigger = isShallow(source);
1594
- } else if (isReactive(source)) {
1595
- getter = () => reactiveGetter(source);
1596
- forceTrigger = true;
1597
- } else if (shared.isArray(source)) {
1598
- isMultiSource = true;
1599
- forceTrigger = source.some((s) => isReactive(s) || isShallow(s));
1600
- getter = () => source.map((s) => {
1601
- if (isRef(s)) {
1602
- return s.value;
1603
- } else if (isReactive(s)) {
1604
- return reactiveGetter(s);
1605
- } else if (shared.isFunction(s)) {
1606
- return call ? call(s, 2) : s();
1607
- } else ;
1608
- });
1609
- } else if (shared.isFunction(source)) {
1610
- if (cb) {
1611
- getter = call ? () => call(source, 2) : source;
1635
+ const { call } = owner.options;
1636
+ if (call) {
1637
+ owner.cleanups[owner.cleanupsLength++] = () => call(cleanupFn, 4);
1612
1638
  } else {
1613
- getter = () => {
1614
- if (cleanup) {
1615
- pauseTracking();
1639
+ owner.cleanups[owner.cleanupsLength++] = cleanupFn;
1640
+ }
1641
+ }
1642
+ }
1643
+ class WatcherEffect extends ReactiveEffect {
1644
+ constructor(source, cb, options = shared.EMPTY_OBJ) {
1645
+ const { deep, once, call, onWarn } = options;
1646
+ let getter;
1647
+ let forceTrigger = false;
1648
+ let isMultiSource = false;
1649
+ if (isRef(source)) {
1650
+ getter = () => source.value;
1651
+ forceTrigger = isShallow(source);
1652
+ } else if (isReactive(source)) {
1653
+ getter = () => reactiveGetter(source, deep);
1654
+ forceTrigger = true;
1655
+ } else if (shared.isArray(source)) {
1656
+ isMultiSource = true;
1657
+ forceTrigger = source.some((s) => isReactive(s) || isShallow(s));
1658
+ getter = () => source.map((s) => {
1659
+ if (isRef(s)) {
1660
+ return s.value;
1661
+ } else if (isReactive(s)) {
1662
+ return reactiveGetter(s, deep);
1663
+ } else if (shared.isFunction(s)) {
1664
+ return call ? call(s, 2) : s();
1665
+ } else ;
1666
+ });
1667
+ } else if (shared.isFunction(source)) {
1668
+ if (cb) {
1669
+ getter = call ? () => call(source, 2) : source;
1670
+ } else {
1671
+ getter = () => {
1672
+ if (this.cleanupsLength) {
1673
+ const prevSub = setActiveSub();
1674
+ try {
1675
+ cleanup(this);
1676
+ } finally {
1677
+ setActiveSub(prevSub);
1678
+ }
1679
+ }
1680
+ const currentEffect = activeWatcher;
1681
+ activeWatcher = this;
1616
1682
  try {
1617
- cleanup();
1683
+ return call ? call(source, 3, [
1684
+ this.boundCleanup
1685
+ ]) : source(this.boundCleanup);
1618
1686
  } finally {
1619
- resetTracking();
1687
+ activeWatcher = currentEffect;
1620
1688
  }
1621
- }
1622
- const currentEffect = activeWatcher;
1623
- activeWatcher = effect;
1624
- try {
1625
- return call ? call(source, 3, [boundCleanup]) : source(boundCleanup);
1626
- } finally {
1627
- activeWatcher = currentEffect;
1628
- }
1689
+ };
1690
+ }
1691
+ } else {
1692
+ getter = shared.NOOP;
1693
+ }
1694
+ if (cb && deep) {
1695
+ const baseGetter = getter;
1696
+ const depth = deep === true ? Infinity : deep;
1697
+ getter = () => traverse(baseGetter(), depth);
1698
+ }
1699
+ super(getter);
1700
+ this.cb = cb;
1701
+ this.options = options;
1702
+ this.boundCleanup = (fn) => onWatcherCleanup(fn, false, this);
1703
+ this.forceTrigger = forceTrigger;
1704
+ this.isMultiSource = isMultiSource;
1705
+ if (once && cb) {
1706
+ const _cb = cb;
1707
+ cb = (...args) => {
1708
+ _cb(...args);
1709
+ this.stop();
1629
1710
  };
1630
1711
  }
1631
- } else {
1632
- getter = shared.NOOP;
1633
- }
1634
- if (cb && deep) {
1635
- const baseGetter = getter;
1636
- const depth = deep === true ? Infinity : deep;
1637
- getter = () => traverse(baseGetter(), depth);
1638
- }
1639
- const scope = getCurrentScope();
1640
- const watchHandle = () => {
1641
- effect.stop();
1642
- if (scope && scope.active) {
1643
- shared.remove(scope.effects, effect);
1644
- }
1645
- };
1646
- if (once && cb) {
1647
- const _cb = cb;
1648
- cb = (...args) => {
1649
- _cb(...args);
1650
- watchHandle();
1651
- };
1712
+ this.cb = cb;
1713
+ this.oldValue = isMultiSource ? new Array(source.length).fill(INITIAL_WATCHER_VALUE) : INITIAL_WATCHER_VALUE;
1652
1714
  }
1653
- let oldValue = isMultiSource ? new Array(source.length).fill(INITIAL_WATCHER_VALUE) : INITIAL_WATCHER_VALUE;
1654
- const job = (immediateFirstRun) => {
1655
- if (!(effect.flags & 1) || !effect.dirty && !immediateFirstRun) {
1715
+ run(initialRun = false) {
1716
+ const oldValue = this.oldValue;
1717
+ const newValue = this.oldValue = super.run();
1718
+ if (!this.cb) {
1656
1719
  return;
1657
1720
  }
1658
- if (cb) {
1659
- const newValue = effect.run();
1660
- if (deep || forceTrigger || (isMultiSource ? newValue.some((v, i) => shared.hasChanged(v, oldValue[i])) : shared.hasChanged(newValue, oldValue))) {
1661
- if (cleanup) {
1662
- cleanup();
1663
- }
1664
- const currentWatcher = activeWatcher;
1665
- activeWatcher = effect;
1666
- try {
1667
- const args = [
1668
- newValue,
1669
- // pass undefined as the old value when it's changed for the first time
1670
- oldValue === INITIAL_WATCHER_VALUE ? void 0 : isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE ? [] : oldValue,
1671
- boundCleanup
1672
- ];
1673
- oldValue = newValue;
1674
- call ? call(cb, 3, args) : (
1675
- // @ts-expect-error
1676
- cb(...args)
1677
- );
1678
- } finally {
1679
- activeWatcher = currentWatcher;
1680
- }
1681
- }
1682
- } else {
1683
- effect.run();
1721
+ const { immediate, deep, call } = this.options;
1722
+ if (initialRun && !immediate) {
1723
+ return;
1684
1724
  }
1685
- };
1686
- if (augmentJob) {
1687
- augmentJob(job);
1688
- }
1689
- effect = new ReactiveEffect(getter);
1690
- effect.scheduler = scheduler ? () => scheduler(job, false) : job;
1691
- boundCleanup = (fn) => onWatcherCleanup(fn, false, effect);
1692
- cleanup = effect.onStop = () => {
1693
- const cleanups = cleanupMap.get(effect);
1694
- if (cleanups) {
1695
- if (call) {
1696
- call(cleanups, 4);
1697
- } else {
1698
- for (const cleanup2 of cleanups) cleanup2();
1725
+ if (deep || this.forceTrigger || (this.isMultiSource ? newValue.some((v, i) => shared.hasChanged(v, oldValue[i])) : shared.hasChanged(newValue, oldValue))) {
1726
+ cleanup(this);
1727
+ const currentWatcher = activeWatcher;
1728
+ activeWatcher = this;
1729
+ try {
1730
+ const args = [
1731
+ newValue,
1732
+ // pass undefined as the old value when it's changed for the first time
1733
+ oldValue === INITIAL_WATCHER_VALUE ? void 0 : this.isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE ? [] : oldValue,
1734
+ this.boundCleanup
1735
+ ];
1736
+ call ? call(this.cb, 3, args) : (
1737
+ // @ts-expect-error
1738
+ this.cb(...args)
1739
+ );
1740
+ } finally {
1741
+ activeWatcher = currentWatcher;
1699
1742
  }
1700
- cleanupMap.delete(effect);
1701
1743
  }
1702
- };
1703
- if (cb) {
1704
- if (immediate) {
1705
- job(true);
1706
- } else {
1707
- oldValue = effect.run();
1708
- }
1709
- } else if (scheduler) {
1710
- scheduler(job.bind(null, true), true);
1711
- } else {
1712
- effect.run();
1713
1744
  }
1714
- watchHandle.pause = effect.pause.bind(effect);
1715
- watchHandle.resume = effect.resume.bind(effect);
1716
- watchHandle.stop = watchHandle;
1717
- return watchHandle;
1745
+ }
1746
+ function reactiveGetter(source, deep) {
1747
+ if (deep) return source;
1748
+ if (isShallow(source) || deep === false || deep === 0)
1749
+ return traverse(source, 1);
1750
+ return traverse(source);
1751
+ }
1752
+ function watch(source, cb, options = shared.EMPTY_OBJ) {
1753
+ const effect = new WatcherEffect(source, cb, options);
1754
+ effect.run(true);
1755
+ const stop = effect.stop.bind(effect);
1756
+ stop.pause = effect.pause.bind(effect);
1757
+ stop.resume = effect.resume.bind(effect);
1758
+ stop.stop = stop;
1759
+ return stop;
1718
1760
  }
1719
1761
  function traverse(value, depth = Infinity, seen) {
1720
1762
  if (depth <= 0 || !shared.isObject(value) || value["__v_skip"]) {
@@ -1759,6 +1801,7 @@ exports.ReactiveFlags = ReactiveFlags;
1759
1801
  exports.TrackOpTypes = TrackOpTypes;
1760
1802
  exports.TriggerOpTypes = TriggerOpTypes;
1761
1803
  exports.WatchErrorCodes = WatchErrorCodes;
1804
+ exports.WatcherEffect = WatcherEffect;
1762
1805
  exports.computed = computed;
1763
1806
  exports.customRef = customRef;
1764
1807
  exports.effect = effect;
@@ -1782,6 +1825,8 @@ exports.reactiveReadArray = reactiveReadArray;
1782
1825
  exports.readonly = readonly;
1783
1826
  exports.ref = ref;
1784
1827
  exports.resetTracking = resetTracking;
1828
+ exports.setActiveSub = setActiveSub;
1829
+ exports.setCurrentScope = setCurrentScope;
1785
1830
  exports.shallowReactive = shallowReactive;
1786
1831
  exports.shallowReadArray = shallowReadArray;
1787
1832
  exports.shallowReadonly = shallowReadonly;