@vue/reactivity 3.4.26 → 3.5.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.4.26
2
+ * @vue/reactivity v3.5.0-alpha.1
3
3
  * (c) 2018-present Yuxi (Evan) You and Vue contributors
4
4
  * @license MIT
5
5
  **/
@@ -89,152 +89,287 @@ class EffectScope {
89
89
  function effectScope(detached) {
90
90
  return new EffectScope(detached);
91
91
  }
92
- function recordEffectScope(effect, scope = activeEffectScope) {
93
- if (scope && scope.active) {
94
- scope.effects.push(effect);
95
- }
96
- }
97
92
  function getCurrentScope() {
98
93
  return activeEffectScope;
99
94
  }
100
- function onScopeDispose(fn) {
95
+ function onScopeDispose(fn, failSilently = false) {
101
96
  if (activeEffectScope) {
102
97
  activeEffectScope.cleanups.push(fn);
103
98
  }
104
99
  }
105
100
 
106
- let activeEffect;
101
+ let activeSub;
102
+ const EffectFlags = {
103
+ "ACTIVE": 1,
104
+ "1": "ACTIVE",
105
+ "RUNNING": 2,
106
+ "2": "RUNNING",
107
+ "TRACKING": 4,
108
+ "4": "TRACKING",
109
+ "NOTIFIED": 8,
110
+ "8": "NOTIFIED",
111
+ "DIRTY": 16,
112
+ "16": "DIRTY",
113
+ "ALLOW_RECURSE": 32,
114
+ "32": "ALLOW_RECURSE",
115
+ "NO_BATCH": 64,
116
+ "64": "NO_BATCH"
117
+ };
107
118
  class ReactiveEffect {
108
- constructor(fn, trigger, scheduler, scope) {
119
+ constructor(fn) {
109
120
  this.fn = fn;
110
- this.trigger = trigger;
111
- this.scheduler = scheduler;
112
- this.active = true;
113
- this.deps = [];
114
121
  /**
115
122
  * @internal
116
123
  */
117
- this._dirtyLevel = 4;
124
+ this.deps = void 0;
118
125
  /**
119
126
  * @internal
120
127
  */
121
- this._trackId = 0;
128
+ this.depsTail = void 0;
122
129
  /**
123
130
  * @internal
124
131
  */
125
- this._runnings = 0;
132
+ this.flags = 1 | 4;
126
133
  /**
127
134
  * @internal
128
135
  */
129
- this._shouldSchedule = false;
136
+ this.nextEffect = void 0;
130
137
  /**
131
138
  * @internal
132
139
  */
133
- this._depsLength = 0;
134
- recordEffectScope(this, scope);
135
- }
136
- get dirty() {
137
- if (this._dirtyLevel === 2 || this._dirtyLevel === 3) {
138
- this._dirtyLevel = 1;
139
- pauseTracking();
140
- for (let i = 0; i < this._depsLength; i++) {
141
- const dep = this.deps[i];
142
- if (dep.computed) {
143
- triggerComputed(dep.computed);
144
- if (this._dirtyLevel >= 4) {
145
- break;
146
- }
147
- }
148
- }
149
- if (this._dirtyLevel === 1) {
150
- this._dirtyLevel = 0;
151
- }
152
- resetTracking();
140
+ this.cleanup = void 0;
141
+ this.scheduler = void 0;
142
+ if (activeEffectScope && activeEffectScope.active) {
143
+ activeEffectScope.effects.push(this);
153
144
  }
154
- return this._dirtyLevel >= 4;
155
145
  }
156
- set dirty(v) {
157
- this._dirtyLevel = v ? 4 : 0;
146
+ /**
147
+ * @internal
148
+ */
149
+ notify() {
150
+ if (this.flags & 2 && !(this.flags & 32)) {
151
+ return;
152
+ }
153
+ if (this.flags & 64) {
154
+ return this.trigger();
155
+ }
156
+ if (!(this.flags & 8)) {
157
+ this.flags |= 8;
158
+ this.nextEffect = batchedEffect;
159
+ batchedEffect = this;
160
+ }
158
161
  }
159
162
  run() {
160
- this._dirtyLevel = 0;
161
- if (!this.active) {
163
+ if (!(this.flags & 1)) {
162
164
  return this.fn();
163
165
  }
164
- let lastShouldTrack = shouldTrack;
165
- let lastEffect = activeEffect;
166
+ this.flags |= 2;
167
+ cleanupEffect(this);
168
+ prepareDeps(this);
169
+ const prevEffect = activeSub;
170
+ const prevShouldTrack = shouldTrack;
171
+ activeSub = this;
172
+ shouldTrack = true;
166
173
  try {
167
- shouldTrack = true;
168
- activeEffect = this;
169
- this._runnings++;
170
- preCleanupEffect(this);
171
174
  return this.fn();
172
175
  } finally {
173
- postCleanupEffect(this);
174
- this._runnings--;
175
- activeEffect = lastEffect;
176
- shouldTrack = lastShouldTrack;
176
+ cleanupDeps(this);
177
+ activeSub = prevEffect;
178
+ shouldTrack = prevShouldTrack;
179
+ this.flags &= ~2;
177
180
  }
178
181
  }
179
182
  stop() {
180
- if (this.active) {
181
- preCleanupEffect(this);
182
- postCleanupEffect(this);
183
+ if (this.flags & 1) {
184
+ for (let link = this.deps; link; link = link.nextDep) {
185
+ removeSub(link);
186
+ }
187
+ this.deps = this.depsTail = void 0;
188
+ cleanupEffect(this);
183
189
  this.onStop && this.onStop();
184
- this.active = false;
190
+ this.flags &= ~1;
185
191
  }
186
192
  }
193
+ trigger() {
194
+ if (this.scheduler) {
195
+ this.scheduler();
196
+ } else {
197
+ this.runIfDirty();
198
+ }
199
+ }
200
+ /**
201
+ * @internal
202
+ */
203
+ runIfDirty() {
204
+ if (isDirty(this)) {
205
+ this.run();
206
+ }
207
+ }
208
+ get dirty() {
209
+ return isDirty(this);
210
+ }
187
211
  }
188
- function triggerComputed(computed) {
189
- return computed.value;
212
+ let batchDepth = 0;
213
+ let batchedEffect;
214
+ function startBatch() {
215
+ batchDepth++;
190
216
  }
191
- function preCleanupEffect(effect2) {
192
- effect2._trackId++;
193
- effect2._depsLength = 0;
217
+ function endBatch() {
218
+ if (batchDepth > 1) {
219
+ batchDepth--;
220
+ return;
221
+ }
222
+ let error;
223
+ while (batchedEffect) {
224
+ let e = batchedEffect;
225
+ batchedEffect = void 0;
226
+ while (e) {
227
+ const next = e.nextEffect;
228
+ e.nextEffect = void 0;
229
+ e.flags &= ~8;
230
+ if (e.flags & 1) {
231
+ try {
232
+ e.trigger();
233
+ } catch (err) {
234
+ if (!error)
235
+ error = err;
236
+ }
237
+ }
238
+ e = next;
239
+ }
240
+ }
241
+ batchDepth--;
242
+ if (error)
243
+ throw error;
244
+ }
245
+ function prepareDeps(sub) {
246
+ for (let link = sub.deps; link; link = link.nextDep) {
247
+ link.version = -1;
248
+ link.prevActiveLink = link.dep.activeLink;
249
+ link.dep.activeLink = link;
250
+ }
251
+ }
252
+ function cleanupDeps(sub) {
253
+ let head;
254
+ let tail = sub.depsTail;
255
+ for (let link = tail; link; link = link.prevDep) {
256
+ if (link.version === -1) {
257
+ if (link === tail)
258
+ tail = link.prevDep;
259
+ removeSub(link);
260
+ removeDep(link);
261
+ } else {
262
+ head = link;
263
+ }
264
+ link.dep.activeLink = link.prevActiveLink;
265
+ link.prevActiveLink = void 0;
266
+ }
267
+ sub.deps = head;
268
+ sub.depsTail = tail;
194
269
  }
195
- function postCleanupEffect(effect2) {
196
- if (effect2.deps.length > effect2._depsLength) {
197
- for (let i = effect2._depsLength; i < effect2.deps.length; i++) {
198
- cleanupDepEffect(effect2.deps[i], effect2);
270
+ function isDirty(sub) {
271
+ for (let link = sub.deps; link; link = link.nextDep) {
272
+ if (link.dep.version !== link.version || link.dep.computed && refreshComputed(link.dep.computed) === false || link.dep.version !== link.version) {
273
+ return true;
199
274
  }
200
- effect2.deps.length = effect2._depsLength;
201
275
  }
276
+ if (sub._dirty) {
277
+ return true;
278
+ }
279
+ return false;
202
280
  }
203
- function cleanupDepEffect(dep, effect2) {
204
- const trackId = dep.get(effect2);
205
- if (trackId !== void 0 && effect2._trackId !== trackId) {
206
- dep.delete(effect2);
207
- if (dep.size === 0) {
208
- dep.cleanup();
281
+ function refreshComputed(computed) {
282
+ if (computed.flags & 2) {
283
+ return false;
284
+ }
285
+ if (computed.flags & 4 && !(computed.flags & 16)) {
286
+ return;
287
+ }
288
+ computed.flags &= ~16;
289
+ if (computed.globalVersion === globalVersion) {
290
+ return;
291
+ }
292
+ computed.globalVersion = globalVersion;
293
+ const dep = computed.dep;
294
+ computed.flags |= 2;
295
+ if (dep.version > 0 && !computed.isSSR && !isDirty(computed)) {
296
+ computed.flags &= ~2;
297
+ return;
298
+ }
299
+ const prevSub = activeSub;
300
+ const prevShouldTrack = shouldTrack;
301
+ activeSub = computed;
302
+ shouldTrack = true;
303
+ try {
304
+ prepareDeps(computed);
305
+ const value = computed.fn();
306
+ if (dep.version === 0 || shared.hasChanged(value, computed._value)) {
307
+ computed._value = value;
308
+ dep.version++;
209
309
  }
310
+ } catch (err) {
311
+ dep.version++;
312
+ throw err;
313
+ } finally {
314
+ activeSub = prevSub;
315
+ shouldTrack = prevShouldTrack;
316
+ cleanupDeps(computed);
317
+ computed.flags &= ~2;
318
+ }
319
+ }
320
+ function removeSub(link) {
321
+ const { dep, prevSub, nextSub } = link;
322
+ if (prevSub) {
323
+ prevSub.nextSub = nextSub;
324
+ link.prevSub = void 0;
325
+ }
326
+ if (nextSub) {
327
+ nextSub.prevSub = prevSub;
328
+ link.nextSub = void 0;
329
+ }
330
+ if (dep.subs === link) {
331
+ dep.subs = prevSub;
332
+ }
333
+ if (!dep.subs && dep.computed) {
334
+ dep.computed.flags &= ~4;
335
+ for (let l = dep.computed.deps; l; l = l.nextDep) {
336
+ removeSub(l);
337
+ }
338
+ }
339
+ }
340
+ function removeDep(link) {
341
+ const { prevDep, nextDep } = link;
342
+ if (prevDep) {
343
+ prevDep.nextDep = nextDep;
344
+ link.prevDep = void 0;
345
+ }
346
+ if (nextDep) {
347
+ nextDep.prevDep = prevDep;
348
+ link.nextDep = void 0;
210
349
  }
211
350
  }
212
351
  function effect(fn, options) {
213
352
  if (fn.effect instanceof ReactiveEffect) {
214
353
  fn = fn.effect.fn;
215
354
  }
216
- const _effect = new ReactiveEffect(fn, shared.NOOP, () => {
217
- if (_effect.dirty) {
218
- _effect.run();
219
- }
220
- });
355
+ const e = new ReactiveEffect(fn);
221
356
  if (options) {
222
- shared.extend(_effect, options);
223
- if (options.scope)
224
- recordEffectScope(_effect, options.scope);
357
+ shared.extend(e, options);
225
358
  }
226
- if (!options || !options.lazy) {
227
- _effect.run();
359
+ try {
360
+ e.run();
361
+ } catch (err) {
362
+ e.stop();
363
+ throw err;
228
364
  }
229
- const runner = _effect.run.bind(_effect);
230
- runner.effect = _effect;
365
+ const runner = e.run.bind(e);
366
+ runner.effect = e;
231
367
  return runner;
232
368
  }
233
369
  function stop(runner) {
234
370
  runner.effect.stop();
235
371
  }
236
372
  let shouldTrack = true;
237
- let pauseScheduleStack = 0;
238
373
  const trackStack = [];
239
374
  function pauseTracking() {
240
375
  trackStack.push(shouldTrack);
@@ -248,169 +383,379 @@ function resetTracking() {
248
383
  const last = trackStack.pop();
249
384
  shouldTrack = last === void 0 ? true : last;
250
385
  }
251
- function pauseScheduling() {
252
- pauseScheduleStack++;
253
- }
254
- function resetScheduling() {
255
- pauseScheduleStack--;
256
- while (!pauseScheduleStack && queueEffectSchedulers.length) {
257
- queueEffectSchedulers.shift()();
386
+ function onEffectCleanup(fn, failSilently = false) {
387
+ if (activeSub instanceof ReactiveEffect) {
388
+ activeSub.cleanup = fn;
258
389
  }
259
390
  }
260
- function trackEffect(effect2, dep, debuggerEventExtraInfo) {
261
- if (dep.get(effect2) !== effect2._trackId) {
262
- dep.set(effect2, effect2._trackId);
263
- const oldDep = effect2.deps[effect2._depsLength];
264
- if (oldDep !== dep) {
265
- if (oldDep) {
266
- cleanupDepEffect(oldDep, effect2);
267
- }
268
- effect2.deps[effect2._depsLength++] = dep;
269
- } else {
270
- effect2._depsLength++;
391
+ function cleanupEffect(e) {
392
+ const { cleanup } = e;
393
+ e.cleanup = void 0;
394
+ if (cleanup) {
395
+ const prevSub = activeSub;
396
+ activeSub = void 0;
397
+ try {
398
+ cleanup();
399
+ } finally {
400
+ activeSub = prevSub;
271
401
  }
272
402
  }
273
403
  }
274
- const queueEffectSchedulers = [];
275
- function triggerEffects(dep, dirtyLevel, debuggerEventExtraInfo) {
276
- pauseScheduling();
277
- for (const effect2 of dep.keys()) {
278
- let tracking;
279
- if (effect2._dirtyLevel < dirtyLevel && (tracking != null ? tracking : tracking = dep.get(effect2) === effect2._trackId)) {
280
- effect2._shouldSchedule || (effect2._shouldSchedule = effect2._dirtyLevel === 0);
281
- effect2._dirtyLevel = dirtyLevel;
404
+
405
+ let globalVersion = 0;
406
+ class Dep {
407
+ constructor(computed) {
408
+ this.computed = computed;
409
+ this.version = 0;
410
+ /**
411
+ * Link between this dep and the current active effect
412
+ */
413
+ this.activeLink = void 0;
414
+ /**
415
+ * Doubly linked list representing the subscribing effects (tail)
416
+ */
417
+ this.subs = void 0;
418
+ }
419
+ track(debugInfo) {
420
+ if (!activeSub || !shouldTrack) {
421
+ return;
282
422
  }
283
- if (effect2._shouldSchedule && (tracking != null ? tracking : tracking = dep.get(effect2) === effect2._trackId)) {
284
- effect2.trigger();
285
- if ((!effect2._runnings || effect2.allowRecurse) && effect2._dirtyLevel !== 2) {
286
- effect2._shouldSchedule = false;
287
- if (effect2.scheduler) {
288
- queueEffectSchedulers.push(effect2.scheduler);
423
+ let link = this.activeLink;
424
+ if (link === void 0 || link.sub !== activeSub) {
425
+ link = this.activeLink = {
426
+ dep: this,
427
+ sub: activeSub,
428
+ version: this.version,
429
+ nextDep: void 0,
430
+ prevDep: void 0,
431
+ nextSub: void 0,
432
+ prevSub: void 0,
433
+ prevActiveLink: void 0
434
+ };
435
+ if (!activeSub.deps) {
436
+ activeSub.deps = activeSub.depsTail = link;
437
+ } else {
438
+ link.prevDep = activeSub.depsTail;
439
+ activeSub.depsTail.nextDep = link;
440
+ activeSub.depsTail = link;
441
+ }
442
+ if (activeSub.flags & 4) {
443
+ addSub(link);
444
+ }
445
+ } else if (link.version === -1) {
446
+ link.version = this.version;
447
+ if (link.nextDep) {
448
+ const next = link.nextDep;
449
+ next.prevDep = link.prevDep;
450
+ if (link.prevDep) {
451
+ link.prevDep.nextDep = next;
452
+ }
453
+ link.prevDep = activeSub.depsTail;
454
+ link.nextDep = void 0;
455
+ activeSub.depsTail.nextDep = link;
456
+ activeSub.depsTail = link;
457
+ if (activeSub.deps === link) {
458
+ activeSub.deps = next;
289
459
  }
290
460
  }
291
461
  }
462
+ return link;
463
+ }
464
+ trigger(debugInfo) {
465
+ this.version++;
466
+ globalVersion++;
467
+ this.notify(debugInfo);
468
+ }
469
+ notify(debugInfo) {
470
+ startBatch();
471
+ try {
472
+ if (false) ;
473
+ for (let link = this.subs; link; link = link.prevSub) {
474
+ link.sub.notify();
475
+ }
476
+ } finally {
477
+ endBatch();
478
+ }
292
479
  }
293
- resetScheduling();
294
480
  }
295
-
296
- const createDep = (cleanup, computed) => {
297
- const dep = /* @__PURE__ */ new Map();
298
- dep.cleanup = cleanup;
299
- dep.computed = computed;
300
- return dep;
301
- };
302
-
481
+ function addSub(link) {
482
+ const computed = link.dep.computed;
483
+ if (computed && !link.dep.subs) {
484
+ computed.flags |= 4 | 16;
485
+ for (let l = computed.deps; l; l = l.nextDep) {
486
+ addSub(l);
487
+ }
488
+ }
489
+ const currentTail = link.dep.subs;
490
+ if (currentTail !== link) {
491
+ link.prevSub = currentTail;
492
+ if (currentTail)
493
+ currentTail.nextSub = link;
494
+ }
495
+ link.dep.subs = link;
496
+ }
303
497
  const targetMap = /* @__PURE__ */ new WeakMap();
304
498
  const ITERATE_KEY = Symbol("");
305
499
  const MAP_KEY_ITERATE_KEY = Symbol("");
500
+ const ARRAY_ITERATE_KEY = Symbol("");
306
501
  function track(target, type, key) {
307
- if (shouldTrack && activeEffect) {
502
+ if (shouldTrack && activeSub) {
308
503
  let depsMap = targetMap.get(target);
309
504
  if (!depsMap) {
310
505
  targetMap.set(target, depsMap = /* @__PURE__ */ new Map());
311
506
  }
312
507
  let dep = depsMap.get(key);
313
508
  if (!dep) {
314
- depsMap.set(key, dep = createDep(() => depsMap.delete(key)));
509
+ depsMap.set(key, dep = new Dep());
510
+ }
511
+ {
512
+ dep.track();
315
513
  }
316
- trackEffect(
317
- activeEffect,
318
- dep);
319
514
  }
320
515
  }
321
516
  function trigger(target, type, key, newValue, oldValue, oldTarget) {
322
517
  const depsMap = targetMap.get(target);
323
518
  if (!depsMap) {
519
+ globalVersion++;
324
520
  return;
325
521
  }
326
522
  let deps = [];
327
523
  if (type === "clear") {
328
524
  deps = [...depsMap.values()];
329
- } else if (key === "length" && shared.isArray(target)) {
330
- const newLength = Number(newValue);
331
- depsMap.forEach((dep, key2) => {
332
- if (key2 === "length" || !shared.isSymbol(key2) && key2 >= newLength) {
333
- deps.push(dep);
334
- }
335
- });
336
525
  } else {
337
- if (key !== void 0) {
338
- deps.push(depsMap.get(key));
339
- }
340
- switch (type) {
341
- case "add":
342
- if (!shared.isArray(target)) {
343
- deps.push(depsMap.get(ITERATE_KEY));
344
- if (shared.isMap(target)) {
345
- deps.push(depsMap.get(MAP_KEY_ITERATE_KEY));
346
- }
347
- } else if (shared.isIntegerKey(key)) {
348
- deps.push(depsMap.get("length"));
526
+ const targetIsArray = shared.isArray(target);
527
+ const isArrayIndex = targetIsArray && shared.isIntegerKey(key);
528
+ if (targetIsArray && key === "length") {
529
+ const newLength = Number(newValue);
530
+ depsMap.forEach((dep, key2) => {
531
+ if (key2 === "length" || key2 === ARRAY_ITERATE_KEY || !shared.isSymbol(key2) && key2 >= newLength) {
532
+ deps.push(dep);
349
533
  }
350
- break;
351
- case "delete":
352
- if (!shared.isArray(target)) {
353
- deps.push(depsMap.get(ITERATE_KEY));
534
+ });
535
+ } else {
536
+ const push = (dep) => dep && deps.push(dep);
537
+ if (key !== void 0) {
538
+ push(depsMap.get(key));
539
+ }
540
+ if (isArrayIndex) {
541
+ push(depsMap.get(ARRAY_ITERATE_KEY));
542
+ }
543
+ switch (type) {
544
+ case "add":
545
+ if (!targetIsArray) {
546
+ push(depsMap.get(ITERATE_KEY));
547
+ if (shared.isMap(target)) {
548
+ push(depsMap.get(MAP_KEY_ITERATE_KEY));
549
+ }
550
+ } else if (isArrayIndex) {
551
+ push(depsMap.get("length"));
552
+ }
553
+ break;
554
+ case "delete":
555
+ if (!targetIsArray) {
556
+ push(depsMap.get(ITERATE_KEY));
557
+ if (shared.isMap(target)) {
558
+ push(depsMap.get(MAP_KEY_ITERATE_KEY));
559
+ }
560
+ }
561
+ break;
562
+ case "set":
354
563
  if (shared.isMap(target)) {
355
- deps.push(depsMap.get(MAP_KEY_ITERATE_KEY));
564
+ push(depsMap.get(ITERATE_KEY));
356
565
  }
357
- }
358
- break;
359
- case "set":
360
- if (shared.isMap(target)) {
361
- deps.push(depsMap.get(ITERATE_KEY));
362
- }
363
- break;
566
+ break;
567
+ }
364
568
  }
365
569
  }
366
- pauseScheduling();
570
+ startBatch();
367
571
  for (const dep of deps) {
368
- if (dep) {
369
- triggerEffects(
370
- dep,
371
- 4);
572
+ {
573
+ dep.trigger();
372
574
  }
373
575
  }
374
- resetScheduling();
576
+ endBatch();
375
577
  }
376
578
  function getDepFromReactive(object, key) {
377
- const depsMap = targetMap.get(object);
378
- return depsMap && depsMap.get(key);
579
+ var _a;
580
+ return (_a = targetMap.get(object)) == null ? void 0 : _a.get(key);
581
+ }
582
+
583
+ function reactiveReadArray(array) {
584
+ const raw = toRaw(array);
585
+ if (raw === array)
586
+ return raw;
587
+ track(raw, "iterate", ARRAY_ITERATE_KEY);
588
+ return isShallow(array) ? raw : raw.map(toReactive);
589
+ }
590
+ function shallowReadArray(arr) {
591
+ track(arr = toRaw(arr), "iterate", ARRAY_ITERATE_KEY);
592
+ return arr;
593
+ }
594
+ const arrayInstrumentations = {
595
+ __proto__: null,
596
+ [Symbol.iterator]() {
597
+ return iterator(this, Symbol.iterator, toReactive);
598
+ },
599
+ concat(...args) {
600
+ return reactiveReadArray(this).concat(
601
+ ...args.map((x) => reactiveReadArray(x))
602
+ );
603
+ },
604
+ entries() {
605
+ return iterator(this, "entries", (value) => {
606
+ value[1] = toReactive(value[1]);
607
+ return value;
608
+ });
609
+ },
610
+ every(fn, thisArg) {
611
+ return apply(this, "every", fn, thisArg);
612
+ },
613
+ filter(fn, thisArg) {
614
+ const result = apply(this, "filter", fn, thisArg);
615
+ return isProxy(this) && !isShallow(this) ? result.map(toReactive) : result;
616
+ },
617
+ find(fn, thisArg) {
618
+ const result = apply(this, "find", fn, thisArg);
619
+ return isProxy(this) && !isShallow(this) ? toReactive(result) : result;
620
+ },
621
+ findIndex(fn, thisArg) {
622
+ return apply(this, "findIndex", fn, thisArg);
623
+ },
624
+ findLast(fn, thisArg) {
625
+ const result = apply(this, "findLast", fn, thisArg);
626
+ return isProxy(this) && !isShallow(this) ? toReactive(result) : result;
627
+ },
628
+ findLastIndex(fn, thisArg) {
629
+ return apply(this, "findLastIndex", fn, thisArg);
630
+ },
631
+ // flat, flatMap could benefit from ARRAY_ITERATE but are not straight-forward to implement
632
+ forEach(fn, thisArg) {
633
+ return apply(this, "forEach", fn, thisArg);
634
+ },
635
+ includes(...args) {
636
+ return searchProxy(this, "includes", args);
637
+ },
638
+ indexOf(...args) {
639
+ return searchProxy(this, "indexOf", args);
640
+ },
641
+ join(separator) {
642
+ return reactiveReadArray(this).join(separator);
643
+ },
644
+ // keys() iterator only reads `length`, no optimisation required
645
+ lastIndexOf(...args) {
646
+ return searchProxy(this, "lastIndexOf", args);
647
+ },
648
+ map(fn, thisArg) {
649
+ return apply(this, "map", fn, thisArg);
650
+ },
651
+ pop() {
652
+ return noTracking(this, "pop");
653
+ },
654
+ push(...args) {
655
+ return noTracking(this, "push", args);
656
+ },
657
+ reduce(fn, ...args) {
658
+ return reduce(this, "reduce", fn, args);
659
+ },
660
+ reduceRight(fn, ...args) {
661
+ return reduce(this, "reduceRight", fn, args);
662
+ },
663
+ shift() {
664
+ return noTracking(this, "shift");
665
+ },
666
+ // slice could use ARRAY_ITERATE but also seems to beg for range tracking
667
+ some(fn, thisArg) {
668
+ return apply(this, "some", fn, thisArg);
669
+ },
670
+ splice(...args) {
671
+ return noTracking(this, "splice", args);
672
+ },
673
+ toReversed() {
674
+ return reactiveReadArray(this).toReversed();
675
+ },
676
+ toSorted(comparer) {
677
+ return reactiveReadArray(this).toSorted(comparer);
678
+ },
679
+ toSpliced(...args) {
680
+ return reactiveReadArray(this).toSpliced(...args);
681
+ },
682
+ unshift(...args) {
683
+ return noTracking(this, "unshift", args);
684
+ },
685
+ values() {
686
+ return iterator(this, "values", toReactive);
687
+ }
688
+ };
689
+ function iterator(self, method, wrapValue) {
690
+ const arr = shallowReadArray(self);
691
+ const iter = arr[method]();
692
+ if (arr !== self && !isShallow(self)) {
693
+ iter._next = iter.next;
694
+ iter.next = () => {
695
+ const result = iter._next();
696
+ if (result.value) {
697
+ result.value = wrapValue(result.value);
698
+ }
699
+ return result;
700
+ };
701
+ }
702
+ return iter;
703
+ }
704
+ function apply(self, method, fn, thisArg) {
705
+ const arr = shallowReadArray(self);
706
+ let wrappedFn = fn;
707
+ if (arr !== self) {
708
+ if (!isShallow(self)) {
709
+ wrappedFn = function(item, index) {
710
+ return fn.call(this, toReactive(item), index, self);
711
+ };
712
+ } else if (fn.length > 2) {
713
+ wrappedFn = function(item, index) {
714
+ return fn.call(this, item, index, self);
715
+ };
716
+ }
717
+ }
718
+ return arr[method](wrappedFn, thisArg);
719
+ }
720
+ function reduce(self, method, fn, args) {
721
+ const arr = shallowReadArray(self);
722
+ let wrappedFn = fn;
723
+ if (arr !== self) {
724
+ if (!isShallow(self)) {
725
+ wrappedFn = function(acc, item, index) {
726
+ return fn.call(this, acc, toReactive(item), index, self);
727
+ };
728
+ } else if (fn.length > 3) {
729
+ wrappedFn = function(acc, item, index) {
730
+ return fn.call(this, acc, item, index, self);
731
+ };
732
+ }
733
+ }
734
+ return arr[method](wrappedFn, ...args);
735
+ }
736
+ function searchProxy(self, method, args) {
737
+ const arr = toRaw(self);
738
+ track(arr, "iterate", ARRAY_ITERATE_KEY);
739
+ const res = arr[method](...args);
740
+ if ((res === -1 || res === false) && isProxy(args[0])) {
741
+ args[0] = toRaw(args[0]);
742
+ return arr[method](...args);
743
+ }
744
+ return res;
745
+ }
746
+ function noTracking(self, method, args = []) {
747
+ pauseTracking();
748
+ startBatch();
749
+ const res = toRaw(self)[method].apply(self, args);
750
+ endBatch();
751
+ resetTracking();
752
+ return res;
379
753
  }
380
754
 
381
755
  const isNonTrackableKeys = /* @__PURE__ */ shared.makeMap(`__proto__,__v_isRef,__isVue`);
382
756
  const builtInSymbols = new Set(
383
757
  /* @__PURE__ */ Object.getOwnPropertyNames(Symbol).filter((key) => key !== "arguments" && key !== "caller").map((key) => Symbol[key]).filter(shared.isSymbol)
384
758
  );
385
- const arrayInstrumentations = /* @__PURE__ */ createArrayInstrumentations();
386
- function createArrayInstrumentations() {
387
- const instrumentations = {};
388
- ["includes", "indexOf", "lastIndexOf"].forEach((key) => {
389
- instrumentations[key] = function(...args) {
390
- const arr = toRaw(this);
391
- for (let i = 0, l = this.length; i < l; i++) {
392
- track(arr, "get", i + "");
393
- }
394
- const res = arr[key](...args);
395
- if (res === -1 || res === false) {
396
- return arr[key](...args.map(toRaw));
397
- } else {
398
- return res;
399
- }
400
- };
401
- });
402
- ["push", "pop", "shift", "unshift", "splice"].forEach((key) => {
403
- instrumentations[key] = function(...args) {
404
- pauseTracking();
405
- pauseScheduling();
406
- const res = toRaw(this)[key].apply(this, args);
407
- resetScheduling();
408
- resetTracking();
409
- return res;
410
- };
411
- });
412
- return instrumentations;
413
- }
414
759
  function hasOwnProperty(key) {
415
760
  if (!shared.isSymbol(key))
416
761
  key = String(key);
@@ -441,14 +786,22 @@ class BaseReactiveHandler {
441
786
  }
442
787
  const targetIsArray = shared.isArray(target);
443
788
  if (!isReadonly2) {
444
- if (targetIsArray && shared.hasOwn(arrayInstrumentations, key)) {
445
- return Reflect.get(arrayInstrumentations, key, receiver);
789
+ let fn;
790
+ if (targetIsArray && (fn = arrayInstrumentations[key])) {
791
+ return fn;
446
792
  }
447
793
  if (key === "hasOwnProperty") {
448
794
  return hasOwnProperty;
449
795
  }
450
796
  }
451
- const res = Reflect.get(target, key, receiver);
797
+ const res = Reflect.get(
798
+ target,
799
+ key,
800
+ // if this is a proxy wrapping a ref, return methods using the raw ref
801
+ // as receiver so that we don't have to call `toRaw` on the ref in all
802
+ // its class methods
803
+ isRef(target) ? target : receiver
804
+ );
452
805
  if (shared.isSymbol(key) ? builtInSymbols.has(key) : isNonTrackableKeys(key)) {
453
806
  return res;
454
807
  }
@@ -911,85 +1264,8 @@ function markRaw(value) {
911
1264
  const toReactive = (value) => shared.isObject(value) ? reactive(value) : value;
912
1265
  const toReadonly = (value) => shared.isObject(value) ? readonly(value) : value;
913
1266
 
914
- class ComputedRefImpl {
915
- constructor(getter, _setter, isReadonly, isSSR) {
916
- this.getter = getter;
917
- this._setter = _setter;
918
- this.dep = void 0;
919
- this.__v_isRef = true;
920
- this["__v_isReadonly"] = false;
921
- this.effect = new ReactiveEffect(
922
- () => getter(this._value),
923
- () => triggerRefValue(
924
- this,
925
- this.effect._dirtyLevel === 2 ? 2 : 3
926
- )
927
- );
928
- this.effect.computed = this;
929
- this.effect.active = this._cacheable = !isSSR;
930
- this["__v_isReadonly"] = isReadonly;
931
- }
932
- get value() {
933
- const self = toRaw(this);
934
- if ((!self._cacheable || self.effect.dirty) && shared.hasChanged(self._value, self._value = self.effect.run())) {
935
- triggerRefValue(self, 4);
936
- }
937
- trackRefValue(self);
938
- if (self.effect._dirtyLevel >= 2) {
939
- triggerRefValue(self, 2);
940
- }
941
- return self._value;
942
- }
943
- set value(newValue) {
944
- this._setter(newValue);
945
- }
946
- // #region polyfill _dirty for backward compatibility third party code for Vue <= 3.3.x
947
- get _dirty() {
948
- return this.effect.dirty;
949
- }
950
- set _dirty(v) {
951
- this.effect.dirty = v;
952
- }
953
- // #endregion
954
- }
955
- function computed(getterOrOptions, debugOptions, isSSR = false) {
956
- let getter;
957
- let setter;
958
- const onlyGetter = shared.isFunction(getterOrOptions);
959
- if (onlyGetter) {
960
- getter = getterOrOptions;
961
- setter = shared.NOOP;
962
- } else {
963
- getter = getterOrOptions.get;
964
- setter = getterOrOptions.set;
965
- }
966
- const cRef = new ComputedRefImpl(getter, setter, onlyGetter || !setter, isSSR);
967
- return cRef;
968
- }
969
-
970
- function trackRefValue(ref2) {
971
- var _a;
972
- if (shouldTrack && activeEffect) {
973
- ref2 = toRaw(ref2);
974
- trackEffect(
975
- activeEffect,
976
- (_a = ref2.dep) != null ? _a : ref2.dep = createDep(
977
- () => ref2.dep = void 0,
978
- ref2 instanceof ComputedRefImpl ? ref2 : void 0
979
- ));
980
- }
981
- }
982
- function triggerRefValue(ref2, dirtyLevel = 4, newVal) {
983
- ref2 = toRaw(ref2);
984
- const dep = ref2.dep;
985
- if (dep) {
986
- triggerEffects(
987
- dep,
988
- dirtyLevel);
989
- }
990
- }
991
1267
  function isRef(r) {
992
- return !!(r && r.__v_isRef === true);
1268
+ return r ? r.__v_isRef === true : false;
993
1269
  }
994
1270
  function ref(value) {
995
1271
  return createRef(value, false);
@@ -1006,27 +1282,34 @@ function createRef(rawValue, shallow) {
1006
1282
  class RefImpl {
1007
1283
  constructor(value, __v_isShallow) {
1008
1284
  this.__v_isShallow = __v_isShallow;
1009
- this.dep = void 0;
1285
+ this.dep = new Dep();
1010
1286
  this.__v_isRef = true;
1011
1287
  this._rawValue = __v_isShallow ? value : toRaw(value);
1012
1288
  this._value = __v_isShallow ? value : toReactive(value);
1013
1289
  }
1014
1290
  get value() {
1015
- trackRefValue(this);
1291
+ {
1292
+ this.dep.track();
1293
+ }
1016
1294
  return this._value;
1017
1295
  }
1018
- set value(newVal) {
1019
- const useDirectValue = this.__v_isShallow || isShallow(newVal) || isReadonly(newVal);
1020
- newVal = useDirectValue ? newVal : toRaw(newVal);
1021
- if (shared.hasChanged(newVal, this._rawValue)) {
1022
- this._rawValue = newVal;
1023
- this._value = useDirectValue ? newVal : toReactive(newVal);
1024
- triggerRefValue(this, 4);
1296
+ set value(newValue) {
1297
+ const oldValue = this._rawValue;
1298
+ const useDirectValue = this.__v_isShallow || isShallow(newValue) || isReadonly(newValue);
1299
+ newValue = useDirectValue ? newValue : toRaw(newValue);
1300
+ if (shared.hasChanged(newValue, oldValue)) {
1301
+ this._rawValue = newValue;
1302
+ this._value = useDirectValue ? newValue : toReactive(newValue);
1303
+ {
1304
+ this.dep.trigger();
1305
+ }
1025
1306
  }
1026
1307
  }
1027
1308
  }
1028
1309
  function triggerRef(ref2) {
1029
- triggerRefValue(ref2, 4);
1310
+ {
1311
+ ref2.dep.trigger();
1312
+ }
1030
1313
  }
1031
1314
  function unref(ref2) {
1032
1315
  return isRef(ref2) ? ref2.value : ref2;
@@ -1051,12 +1334,9 @@ function proxyRefs(objectWithRefs) {
1051
1334
  }
1052
1335
  class CustomRefImpl {
1053
1336
  constructor(factory) {
1054
- this.dep = void 0;
1055
1337
  this.__v_isRef = true;
1056
- const { get, set } = factory(
1057
- () => trackRefValue(this),
1058
- () => triggerRefValue(this)
1059
- );
1338
+ const dep = this.dep = new Dep();
1339
+ const { get, set } = factory(dep.track.bind(dep), dep.trigger.bind(dep));
1060
1340
  this._get = get;
1061
1341
  this._set = set;
1062
1342
  }
@@ -1121,7 +1401,79 @@ function propertyToRef(source, key, defaultValue) {
1121
1401
  return isRef(val) ? val : new ObjectRefImpl(source, key, defaultValue);
1122
1402
  }
1123
1403
 
1124
- const deferredComputed = computed;
1404
+ class ComputedRefImpl {
1405
+ constructor(fn, setter, isSSR) {
1406
+ this.fn = fn;
1407
+ this.setter = setter;
1408
+ /**
1409
+ * @internal
1410
+ */
1411
+ this._value = void 0;
1412
+ /**
1413
+ * @internal
1414
+ */
1415
+ this.dep = new Dep(this);
1416
+ /**
1417
+ * @internal
1418
+ */
1419
+ this.__v_isRef = true;
1420
+ // A computed is also a subscriber that tracks other deps
1421
+ /**
1422
+ * @internal
1423
+ */
1424
+ this.deps = void 0;
1425
+ /**
1426
+ * @internal
1427
+ */
1428
+ this.depsTail = void 0;
1429
+ /**
1430
+ * @internal
1431
+ */
1432
+ this.flags = 16;
1433
+ /**
1434
+ * @internal
1435
+ */
1436
+ this.globalVersion = globalVersion - 1;
1437
+ // for backwards compat
1438
+ this.effect = this;
1439
+ this.__v_isReadonly = !setter;
1440
+ this.isSSR = isSSR;
1441
+ }
1442
+ /**
1443
+ * @internal
1444
+ */
1445
+ notify() {
1446
+ if (activeSub !== this) {
1447
+ this.flags |= 16;
1448
+ this.dep.notify();
1449
+ }
1450
+ }
1451
+ get value() {
1452
+ const link = this.dep.track();
1453
+ refreshComputed(this);
1454
+ if (link) {
1455
+ link.version = this.dep.version;
1456
+ }
1457
+ return this._value;
1458
+ }
1459
+ set value(newValue) {
1460
+ if (this.setter) {
1461
+ this.setter(newValue);
1462
+ }
1463
+ }
1464
+ }
1465
+ function computed(getterOrOptions, debugOptions, isSSR = false) {
1466
+ let getter;
1467
+ let setter;
1468
+ if (shared.isFunction(getterOrOptions)) {
1469
+ getter = getterOrOptions;
1470
+ } else {
1471
+ getter = getterOrOptions.get;
1472
+ setter = getterOrOptions.set;
1473
+ }
1474
+ const cRef = new ComputedRefImpl(getter, setter, isSSR);
1475
+ return cRef;
1476
+ }
1125
1477
 
1126
1478
  const TrackOpTypes = {
1127
1479
  "GET": "get",
@@ -1142,15 +1494,17 @@ const ReactiveFlags = {
1142
1494
  "RAW": "__v_raw"
1143
1495
  };
1144
1496
 
1497
+ exports.ARRAY_ITERATE_KEY = ARRAY_ITERATE_KEY;
1498
+ exports.EffectFlags = EffectFlags;
1145
1499
  exports.EffectScope = EffectScope;
1146
1500
  exports.ITERATE_KEY = ITERATE_KEY;
1501
+ exports.MAP_KEY_ITERATE_KEY = MAP_KEY_ITERATE_KEY;
1147
1502
  exports.ReactiveEffect = ReactiveEffect;
1148
1503
  exports.ReactiveFlags = ReactiveFlags;
1149
1504
  exports.TrackOpTypes = TrackOpTypes;
1150
1505
  exports.TriggerOpTypes = TriggerOpTypes;
1151
1506
  exports.computed = computed;
1152
1507
  exports.customRef = customRef;
1153
- exports.deferredComputed = deferredComputed;
1154
1508
  exports.effect = effect;
1155
1509
  exports.effectScope = effectScope;
1156
1510
  exports.enableTracking = enableTracking;
@@ -1161,20 +1515,23 @@ exports.isReadonly = isReadonly;
1161
1515
  exports.isRef = isRef;
1162
1516
  exports.isShallow = isShallow;
1163
1517
  exports.markRaw = markRaw;
1518
+ exports.onEffectCleanup = onEffectCleanup;
1164
1519
  exports.onScopeDispose = onScopeDispose;
1165
- exports.pauseScheduling = pauseScheduling;
1166
1520
  exports.pauseTracking = pauseTracking;
1167
1521
  exports.proxyRefs = proxyRefs;
1168
1522
  exports.reactive = reactive;
1523
+ exports.reactiveReadArray = reactiveReadArray;
1169
1524
  exports.readonly = readonly;
1170
1525
  exports.ref = ref;
1171
- exports.resetScheduling = resetScheduling;
1172
1526
  exports.resetTracking = resetTracking;
1173
1527
  exports.shallowReactive = shallowReactive;
1528
+ exports.shallowReadArray = shallowReadArray;
1174
1529
  exports.shallowReadonly = shallowReadonly;
1175
1530
  exports.shallowRef = shallowRef;
1176
1531
  exports.stop = stop;
1177
1532
  exports.toRaw = toRaw;
1533
+ exports.toReactive = toReactive;
1534
+ exports.toReadonly = toReadonly;
1178
1535
  exports.toRef = toRef;
1179
1536
  exports.toRefs = toRefs;
1180
1537
  exports.toValue = toValue;