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