@vue/reactivity 3.5.17 → 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.17
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,584 +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;
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)) {
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
- // 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;
98
+ if (nextSub !== void 0) {
99
+ nextSub.prevSub = prevSub;
100
+ } else {
101
+ dep.subsTail = prevSub;
519
102
  }
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;
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 */;
529
132
  } else {
530
- link.prevDep = activeSub.depsTail;
531
- activeSub.depsTail.nextDep = link;
532
- activeSub.depsTail = link;
133
+ flags = 0 /* None */;
533
134
  }
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;
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;
549
147
  }
550
148
  }
551
149
  }
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
- }
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;
568
160
  }
569
- } finally {
570
- endBatch();
571
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);
572
273
  }
274
+ return false;
573
275
  }
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;
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);
588
292
  }
589
- link.dep.subs = link;
590
293
  }
591
294
  }
592
295
  const targetMap = /* @__PURE__ */ new WeakMap();
@@ -600,33 +303,27 @@ const ARRAY_ITERATE_KEY = Symbol(
600
303
  ""
601
304
  );
602
305
  function track(target, type, key) {
603
- if (shouldTrack && activeSub) {
306
+ if (activeSub !== void 0) {
604
307
  let depsMap = targetMap.get(target);
605
308
  if (!depsMap) {
606
309
  targetMap.set(target, depsMap = /* @__PURE__ */ new Map());
607
310
  }
608
311
  let dep = depsMap.get(key);
609
312
  if (!dep) {
610
- depsMap.set(key, dep = new Dep());
611
- dep.map = depsMap;
612
- dep.key = key;
613
- }
614
- {
615
- dep.track();
313
+ depsMap.set(key, dep = new Dep(depsMap, key));
616
314
  }
315
+ link(dep, activeSub);
617
316
  }
618
317
  }
619
318
  function trigger(target, type, key, newValue, oldValue, oldTarget) {
620
319
  const depsMap = targetMap.get(target);
621
320
  if (!depsMap) {
622
- globalVersion++;
623
321
  return;
624
322
  }
625
323
  const run = (dep) => {
626
- if (dep) {
627
- {
628
- dep.trigger();
629
- }
324
+ if (dep !== void 0 && dep.subs !== void 0) {
325
+ propagate(dep.subs);
326
+ shallowPropagate(dep.subs);
630
327
  }
631
328
  };
632
329
  startBatch();
@@ -851,11 +548,11 @@ function searchProxy(self, method, args) {
851
548
  return res;
852
549
  }
853
550
  function noTracking(self, method, args = []) {
854
- pauseTracking();
855
551
  startBatch();
552
+ const prevSub = setActiveSub();
856
553
  const res = toRaw(self)[method].apply(self, args);
554
+ setActiveSub(prevSub);
857
555
  endBatch();
858
- resetTracking();
859
556
  return res;
860
557
  }
861
558
 
@@ -901,14 +598,18 @@ class BaseReactiveHandler {
901
598
  return hasOwnProperty;
902
599
  }
903
600
  }
601
+ const wasRef = isRef(target);
904
602
  const res = Reflect.get(
905
603
  target,
906
604
  key,
907
605
  // if this is a proxy wrapping a ref, return methods using the raw ref
908
606
  // as receiver so that we don't have to call `toRaw` on the ref in all
909
607
  // its class methods
910
- isRef(target) ? target : receiver
608
+ wasRef ? target : receiver
911
609
  );
610
+ if (wasRef && key !== "value") {
611
+ return res;
612
+ }
912
613
  if (shared.isSymbol(key) ? builtInSymbols.has(key) : isNonTrackableKeys(key)) {
913
614
  return res;
914
615
  }
@@ -1318,29 +1019,47 @@ function isRef(r) {
1318
1019
  return r ? r["__v_isRef"] === true : false;
1319
1020
  }
1320
1021
  function ref(value) {
1321
- return createRef(value, false);
1022
+ return createRef(value, toReactive);
1322
1023
  }
1323
1024
  function shallowRef(value) {
1324
- return createRef(value, true);
1025
+ return createRef(value);
1325
1026
  }
1326
- function createRef(rawValue, shallow) {
1027
+ function createRef(rawValue, wrap) {
1327
1028
  if (isRef(rawValue)) {
1328
1029
  return rawValue;
1329
1030
  }
1330
- return new RefImpl(rawValue, shallow);
1031
+ return new RefImpl(rawValue, wrap);
1331
1032
  }
1332
1033
  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;
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;
1340
1055
  }
1341
1056
  get value() {
1342
- {
1343
- 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
+ }
1344
1063
  }
1345
1064
  return this._value;
1346
1065
  }
@@ -1349,21 +1068,38 @@ class RefImpl {
1349
1068
  const useDirectValue = this["__v_isShallow"] || isShallow(newValue) || isReadonly(newValue);
1350
1069
  newValue = useDirectValue ? newValue : toRaw(newValue);
1351
1070
  if (shared.hasChanged(newValue, oldValue)) {
1071
+ this.flags |= ReactiveFlags$1.Dirty;
1352
1072
  this._rawValue = newValue;
1353
- this._value = useDirectValue ? newValue : toReactive(newValue);
1354
- {
1355
- 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
+ }
1356
1080
  }
1357
1081
  }
1358
1082
  }
1083
+ update() {
1084
+ this.flags &= ~ReactiveFlags$1.Dirty;
1085
+ return shared.hasChanged(this._oldValue, this._oldValue = this._rawValue);
1086
+ }
1359
1087
  }
1360
1088
  function triggerRef(ref2) {
1361
- if (ref2.dep) {
1362
- {
1363
- 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();
1364
1095
  }
1365
1096
  }
1366
1097
  }
1098
+ function trackRef(dep) {
1099
+ if (activeSub !== void 0) {
1100
+ link(dep, activeSub);
1101
+ }
1102
+ }
1367
1103
  function unref(ref2) {
1368
1104
  return isRef(ref2) ? ref2.value : ref2;
1369
1105
  }
@@ -1387,13 +1123,21 @@ function proxyRefs(objectWithRefs) {
1387
1123
  }
1388
1124
  class CustomRefImpl {
1389
1125
  constructor(factory) {
1126
+ this.subs = void 0;
1127
+ this.subsTail = void 0;
1128
+ this.flags = ReactiveFlags$1.None;
1390
1129
  this["__v_isRef"] = true;
1391
1130
  this._value = void 0;
1392
- const dep = this.dep = new Dep();
1393
- const { get, set } = factory(dep.track.bind(dep), dep.trigger.bind(dep));
1131
+ const { get, set } = factory(
1132
+ () => trackRef(this),
1133
+ () => triggerRef(this)
1134
+ );
1394
1135
  this._get = get;
1395
1136
  this._set = set;
1396
1137
  }
1138
+ get dep() {
1139
+ return this;
1140
+ }
1397
1141
  get value() {
1398
1142
  return this._value = this._get();
1399
1143
  }
@@ -1457,65 +1201,366 @@ function propertyToRef(source, key, defaultValue) {
1457
1201
  return isRef(val) ? val : new ObjectRefImpl(source, key, defaultValue);
1458
1202
  }
1459
1203
 
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);
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;
1472
1219
  /**
1473
1220
  * @internal
1474
1221
  */
1475
- this.__v_isRef = true;
1476
- // TODO isolatedDeclarations "__v_isReadonly"
1477
- // A computed is also a subscriber that tracks other deps
1222
+ this.cleanups = [];
1478
1223
  /**
1479
1224
  * @internal
1480
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) {
1481
1392
  this.deps = void 0;
1393
+ this.depsTail = void 0;
1394
+ this.subs = void 0;
1395
+ this.subsTail = void 0;
1396
+ this.flags = 0;
1482
1397
  /**
1483
1398
  * @internal
1484
1399
  */
1485
- this.depsTail = void 0;
1400
+ this.cleanups = [];
1486
1401
  /**
1487
1402
  * @internal
1488
1403
  */
1489
- 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;
1490
1494
  /**
1491
1495
  * @internal
1492
1496
  */
1493
- 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;
1494
1503
  /**
1495
1504
  * @internal
1496
1505
  */
1497
- this.next = void 0;
1498
- // for backwards compat
1499
- this.effect = this;
1506
+ this.__v_isRef = true;
1500
1507
  this["__v_isReadonly"] = !setter;
1501
- 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;
1502
1517
  }
1503
1518
  /**
1504
1519
  * @internal
1520
+ * for backwards compat
1505
1521
  */
1506
- notify() {
1507
- this.flags |= 16;
1508
- if (!(this.flags & 8) && // avoid infinite self recursion
1509
- activeSub !== this) {
1510
- batch(this, true);
1522
+ get _dirty() {
1523
+ const flags = this.flags;
1524
+ if (flags & ReactiveFlags$1.Dirty) {
1511
1525
  return true;
1512
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
+ }
1513
1547
  }
1514
1548
  get value() {
1515
- const link = this.dep.track();
1516
- refreshComputed(this);
1517
- if (link) {
1518
- 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);
1519
1564
  }
1520
1565
  return this._value;
1521
1566
  }
@@ -1524,6 +1569,20 @@ class ComputedRefImpl {
1524
1569
  this.setter(newValue);
1525
1570
  }
1526
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
+ }
1527
1586
  }
1528
1587
  function computed(getterOrOptions, debugOptions, isSSR = false) {
1529
1588
  let getter;
@@ -1534,7 +1593,7 @@ function computed(getterOrOptions, debugOptions, isSSR = false) {
1534
1593
  getter = getterOrOptions.get;
1535
1594
  setter = getterOrOptions.set;
1536
1595
  }
1537
- const cRef = new ComputedRefImpl(getter, setter, isSSR);
1596
+ const cRef = new ComputedRefImpl(getter, setter);
1538
1597
  return cRef;
1539
1598
  }
1540
1599
 
@@ -1567,159 +1626,137 @@ const WatchErrorCodes = {
1567
1626
  "4": "WATCH_CLEANUP"
1568
1627
  };
1569
1628
  const INITIAL_WATCHER_VALUE = {};
1570
- const cleanupMap = /* @__PURE__ */ new WeakMap();
1571
1629
  let activeWatcher = void 0;
1572
1630
  function getCurrentWatcher() {
1573
1631
  return activeWatcher;
1574
1632
  }
1575
1633
  function onWatcherCleanup(cleanupFn, failSilently = false, owner = activeWatcher) {
1576
1634
  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;
1635
+ const { call } = owner.options;
1636
+ if (call) {
1637
+ owner.cleanups[owner.cleanupsLength++] = () => call(cleanupFn, 4);
1617
1638
  } else {
1618
- getter = () => {
1619
- if (cleanup) {
1620
- 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;
1621
1682
  try {
1622
- cleanup();
1683
+ return call ? call(source, 3, [
1684
+ this.boundCleanup
1685
+ ]) : source(this.boundCleanup);
1623
1686
  } finally {
1624
- resetTracking();
1687
+ activeWatcher = currentEffect;
1625
1688
  }
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
- }
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();
1634
1710
  };
1635
1711
  }
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
- };
1712
+ this.cb = cb;
1713
+ this.oldValue = isMultiSource ? new Array(source.length).fill(INITIAL_WATCHER_VALUE) : INITIAL_WATCHER_VALUE;
1657
1714
  }
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) {
1715
+ run(initialRun = false) {
1716
+ const oldValue = this.oldValue;
1717
+ const newValue = this.oldValue = super.run();
1718
+ if (!this.cb) {
1661
1719
  return;
1662
1720
  }
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();
1721
+ const { immediate, deep, call } = this.options;
1722
+ if (initialRun && !immediate) {
1723
+ return;
1689
1724
  }
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();
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;
1704
1742
  }
1705
- cleanupMap.delete(effect);
1706
- }
1707
- };
1708
- if (cb) {
1709
- if (immediate) {
1710
- job(true);
1711
- } else {
1712
- oldValue = effect.run();
1713
1743
  }
1714
- } else if (scheduler) {
1715
- scheduler(job.bind(null, true), true);
1716
- } else {
1717
- effect.run();
1718
1744
  }
1719
- watchHandle.pause = effect.pause.bind(effect);
1720
- watchHandle.resume = effect.resume.bind(effect);
1721
- watchHandle.stop = watchHandle;
1722
- 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;
1723
1760
  }
1724
1761
  function traverse(value, depth = Infinity, seen) {
1725
1762
  if (depth <= 0 || !shared.isObject(value) || value["__v_skip"]) {
@@ -1764,6 +1801,7 @@ exports.ReactiveFlags = ReactiveFlags;
1764
1801
  exports.TrackOpTypes = TrackOpTypes;
1765
1802
  exports.TriggerOpTypes = TriggerOpTypes;
1766
1803
  exports.WatchErrorCodes = WatchErrorCodes;
1804
+ exports.WatcherEffect = WatcherEffect;
1767
1805
  exports.computed = computed;
1768
1806
  exports.customRef = customRef;
1769
1807
  exports.effect = effect;
@@ -1787,6 +1825,8 @@ exports.reactiveReadArray = reactiveReadArray;
1787
1825
  exports.readonly = readonly;
1788
1826
  exports.ref = ref;
1789
1827
  exports.resetTracking = resetTracking;
1828
+ exports.setActiveSub = setActiveSub;
1829
+ exports.setCurrentScope = setCurrentScope;
1790
1830
  exports.shallowReactive = shallowReactive;
1791
1831
  exports.shallowReadArray = shallowReadArray;
1792
1832
  exports.shallowReadonly = shallowReadonly;