@vue/reactivity 3.4.25 → 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.25
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
  **/
@@ -13,8 +13,6 @@ var VueReactivity = (function (exports) {
13
13
  return (val) => set.has(val);
14
14
  }
15
15
 
16
- const NOOP = () => {
17
- };
18
16
  const extend = Object.assign;
19
17
  const hasOwnProperty$1 = Object.prototype.hasOwnProperty;
20
18
  const hasOwn = (val, key) => hasOwnProperty$1.call(val, key);
@@ -135,157 +133,296 @@ var VueReactivity = (function (exports) {
135
133
  function effectScope(detached) {
136
134
  return new EffectScope(detached);
137
135
  }
138
- function recordEffectScope(effect, scope = activeEffectScope) {
139
- if (scope && scope.active) {
140
- scope.effects.push(effect);
141
- }
142
- }
143
136
  function getCurrentScope() {
144
137
  return activeEffectScope;
145
138
  }
146
- function onScopeDispose(fn) {
139
+ function onScopeDispose(fn, failSilently = false) {
147
140
  if (activeEffectScope) {
148
141
  activeEffectScope.cleanups.push(fn);
149
- } else {
142
+ } else if (!failSilently) {
150
143
  warn(
151
144
  `onScopeDispose() is called when there is no active effect scope to be associated with.`
152
145
  );
153
146
  }
154
147
  }
155
148
 
156
- let activeEffect;
149
+ let activeSub;
150
+ const EffectFlags = {
151
+ "ACTIVE": 1,
152
+ "1": "ACTIVE",
153
+ "RUNNING": 2,
154
+ "2": "RUNNING",
155
+ "TRACKING": 4,
156
+ "4": "TRACKING",
157
+ "NOTIFIED": 8,
158
+ "8": "NOTIFIED",
159
+ "DIRTY": 16,
160
+ "16": "DIRTY",
161
+ "ALLOW_RECURSE": 32,
162
+ "32": "ALLOW_RECURSE",
163
+ "NO_BATCH": 64,
164
+ "64": "NO_BATCH"
165
+ };
157
166
  class ReactiveEffect {
158
- constructor(fn, trigger, scheduler, scope) {
167
+ constructor(fn) {
159
168
  this.fn = fn;
160
- this.trigger = trigger;
161
- this.scheduler = scheduler;
162
- this.active = true;
163
- this.deps = [];
164
169
  /**
165
170
  * @internal
166
171
  */
167
- this._dirtyLevel = 4;
172
+ this.deps = void 0;
168
173
  /**
169
174
  * @internal
170
175
  */
171
- this._trackId = 0;
176
+ this.depsTail = void 0;
172
177
  /**
173
178
  * @internal
174
179
  */
175
- this._runnings = 0;
180
+ this.flags = 1 | 4;
176
181
  /**
177
182
  * @internal
178
183
  */
179
- this._shouldSchedule = false;
184
+ this.nextEffect = void 0;
180
185
  /**
181
186
  * @internal
182
187
  */
183
- this._depsLength = 0;
184
- recordEffectScope(this, scope);
185
- }
186
- get dirty() {
187
- if (this._dirtyLevel === 2 || this._dirtyLevel === 3) {
188
- this._dirtyLevel = 1;
189
- pauseTracking();
190
- for (let i = 0; i < this._depsLength; i++) {
191
- const dep = this.deps[i];
192
- if (dep.computed) {
193
- triggerComputed(dep.computed);
194
- if (this._dirtyLevel >= 4) {
195
- break;
196
- }
197
- }
198
- }
199
- if (this._dirtyLevel === 1) {
200
- this._dirtyLevel = 0;
201
- }
202
- resetTracking();
188
+ this.cleanup = void 0;
189
+ this.scheduler = void 0;
190
+ if (activeEffectScope && activeEffectScope.active) {
191
+ activeEffectScope.effects.push(this);
203
192
  }
204
- return this._dirtyLevel >= 4;
205
193
  }
206
- set dirty(v) {
207
- this._dirtyLevel = v ? 4 : 0;
194
+ /**
195
+ * @internal
196
+ */
197
+ notify() {
198
+ if (this.flags & 2 && !(this.flags & 32)) {
199
+ return;
200
+ }
201
+ if (this.flags & 64) {
202
+ return this.trigger();
203
+ }
204
+ if (!(this.flags & 8)) {
205
+ this.flags |= 8;
206
+ this.nextEffect = batchedEffect;
207
+ batchedEffect = this;
208
+ }
208
209
  }
209
210
  run() {
210
- this._dirtyLevel = 0;
211
- if (!this.active) {
211
+ if (!(this.flags & 1)) {
212
212
  return this.fn();
213
213
  }
214
- let lastShouldTrack = shouldTrack;
215
- let lastEffect = activeEffect;
214
+ this.flags |= 2;
215
+ cleanupEffect(this);
216
+ prepareDeps(this);
217
+ const prevEffect = activeSub;
218
+ const prevShouldTrack = shouldTrack;
219
+ activeSub = this;
220
+ shouldTrack = true;
216
221
  try {
217
- shouldTrack = true;
218
- activeEffect = this;
219
- this._runnings++;
220
- preCleanupEffect(this);
221
222
  return this.fn();
222
223
  } finally {
223
- postCleanupEffect(this);
224
- this._runnings--;
225
- activeEffect = lastEffect;
226
- shouldTrack = lastShouldTrack;
224
+ if (activeSub !== this) {
225
+ warn(
226
+ "Active effect was not restored correctly - this is likely a Vue internal bug."
227
+ );
228
+ }
229
+ cleanupDeps(this);
230
+ activeSub = prevEffect;
231
+ shouldTrack = prevShouldTrack;
232
+ this.flags &= ~2;
227
233
  }
228
234
  }
229
235
  stop() {
230
- var _a;
231
- if (this.active) {
232
- preCleanupEffect(this);
233
- postCleanupEffect(this);
234
- (_a = this.onStop) == null ? void 0 : _a.call(this);
235
- this.active = false;
236
+ if (this.flags & 1) {
237
+ for (let link = this.deps; link; link = link.nextDep) {
238
+ removeSub(link);
239
+ }
240
+ this.deps = this.depsTail = void 0;
241
+ cleanupEffect(this);
242
+ this.onStop && this.onStop();
243
+ this.flags &= ~1;
244
+ }
245
+ }
246
+ trigger() {
247
+ if (this.scheduler) {
248
+ this.scheduler();
249
+ } else {
250
+ this.runIfDirty();
251
+ }
252
+ }
253
+ /**
254
+ * @internal
255
+ */
256
+ runIfDirty() {
257
+ if (isDirty(this)) {
258
+ this.run();
236
259
  }
237
260
  }
261
+ get dirty() {
262
+ return isDirty(this);
263
+ }
238
264
  }
239
- function triggerComputed(computed) {
240
- return computed.value;
265
+ let batchDepth = 0;
266
+ let batchedEffect;
267
+ function startBatch() {
268
+ batchDepth++;
241
269
  }
242
- function preCleanupEffect(effect2) {
243
- effect2._trackId++;
244
- effect2._depsLength = 0;
270
+ function endBatch() {
271
+ if (batchDepth > 1) {
272
+ batchDepth--;
273
+ return;
274
+ }
275
+ let error;
276
+ while (batchedEffect) {
277
+ let e = batchedEffect;
278
+ batchedEffect = void 0;
279
+ while (e) {
280
+ const next = e.nextEffect;
281
+ e.nextEffect = void 0;
282
+ e.flags &= ~8;
283
+ if (e.flags & 1) {
284
+ try {
285
+ e.trigger();
286
+ } catch (err) {
287
+ if (!error)
288
+ error = err;
289
+ }
290
+ }
291
+ e = next;
292
+ }
293
+ }
294
+ batchDepth--;
295
+ if (error)
296
+ throw error;
297
+ }
298
+ function prepareDeps(sub) {
299
+ for (let link = sub.deps; link; link = link.nextDep) {
300
+ link.version = -1;
301
+ link.prevActiveLink = link.dep.activeLink;
302
+ link.dep.activeLink = link;
303
+ }
304
+ }
305
+ function cleanupDeps(sub) {
306
+ let head;
307
+ let tail = sub.depsTail;
308
+ for (let link = tail; link; link = link.prevDep) {
309
+ if (link.version === -1) {
310
+ if (link === tail)
311
+ tail = link.prevDep;
312
+ removeSub(link);
313
+ removeDep(link);
314
+ } else {
315
+ head = link;
316
+ }
317
+ link.dep.activeLink = link.prevActiveLink;
318
+ link.prevActiveLink = void 0;
319
+ }
320
+ sub.deps = head;
321
+ sub.depsTail = tail;
245
322
  }
246
- function postCleanupEffect(effect2) {
247
- if (effect2.deps.length > effect2._depsLength) {
248
- for (let i = effect2._depsLength; i < effect2.deps.length; i++) {
249
- cleanupDepEffect(effect2.deps[i], effect2);
323
+ function isDirty(sub) {
324
+ for (let link = sub.deps; link; link = link.nextDep) {
325
+ if (link.dep.version !== link.version || link.dep.computed && refreshComputed(link.dep.computed) === false || link.dep.version !== link.version) {
326
+ return true;
250
327
  }
251
- effect2.deps.length = effect2._depsLength;
252
328
  }
329
+ if (sub._dirty) {
330
+ return true;
331
+ }
332
+ return false;
253
333
  }
254
- function cleanupDepEffect(dep, effect2) {
255
- const trackId = dep.get(effect2);
256
- if (trackId !== void 0 && effect2._trackId !== trackId) {
257
- dep.delete(effect2);
258
- if (dep.size === 0) {
259
- dep.cleanup();
334
+ function refreshComputed(computed) {
335
+ if (computed.flags & 2) {
336
+ return false;
337
+ }
338
+ if (computed.flags & 4 && !(computed.flags & 16)) {
339
+ return;
340
+ }
341
+ computed.flags &= ~16;
342
+ if (computed.globalVersion === globalVersion) {
343
+ return;
344
+ }
345
+ computed.globalVersion = globalVersion;
346
+ const dep = computed.dep;
347
+ computed.flags |= 2;
348
+ if (dep.version > 0 && !computed.isSSR && !isDirty(computed)) {
349
+ computed.flags &= ~2;
350
+ return;
351
+ }
352
+ const prevSub = activeSub;
353
+ const prevShouldTrack = shouldTrack;
354
+ activeSub = computed;
355
+ shouldTrack = true;
356
+ try {
357
+ prepareDeps(computed);
358
+ const value = computed.fn();
359
+ if (dep.version === 0 || hasChanged(value, computed._value)) {
360
+ computed._value = value;
361
+ dep.version++;
260
362
  }
363
+ } catch (err) {
364
+ dep.version++;
365
+ throw err;
366
+ } finally {
367
+ activeSub = prevSub;
368
+ shouldTrack = prevShouldTrack;
369
+ cleanupDeps(computed);
370
+ computed.flags &= ~2;
371
+ }
372
+ }
373
+ function removeSub(link) {
374
+ const { dep, prevSub, nextSub } = link;
375
+ if (prevSub) {
376
+ prevSub.nextSub = nextSub;
377
+ link.prevSub = void 0;
378
+ }
379
+ if (nextSub) {
380
+ nextSub.prevSub = prevSub;
381
+ link.nextSub = void 0;
382
+ }
383
+ if (dep.subs === link) {
384
+ dep.subs = prevSub;
385
+ }
386
+ if (!dep.subs && dep.computed) {
387
+ dep.computed.flags &= ~4;
388
+ for (let l = dep.computed.deps; l; l = l.nextDep) {
389
+ removeSub(l);
390
+ }
391
+ }
392
+ }
393
+ function removeDep(link) {
394
+ const { prevDep, nextDep } = link;
395
+ if (prevDep) {
396
+ prevDep.nextDep = nextDep;
397
+ link.prevDep = void 0;
398
+ }
399
+ if (nextDep) {
400
+ nextDep.prevDep = prevDep;
401
+ link.nextDep = void 0;
261
402
  }
262
403
  }
263
404
  function effect(fn, options) {
264
405
  if (fn.effect instanceof ReactiveEffect) {
265
406
  fn = fn.effect.fn;
266
407
  }
267
- const _effect = new ReactiveEffect(fn, NOOP, () => {
268
- if (_effect.dirty) {
269
- _effect.run();
270
- }
271
- });
408
+ const e = new ReactiveEffect(fn);
272
409
  if (options) {
273
- extend(_effect, options);
274
- if (options.scope)
275
- recordEffectScope(_effect, options.scope);
410
+ extend(e, options);
276
411
  }
277
- if (!options || !options.lazy) {
278
- _effect.run();
412
+ try {
413
+ e.run();
414
+ } catch (err) {
415
+ e.stop();
416
+ throw err;
279
417
  }
280
- const runner = _effect.run.bind(_effect);
281
- runner.effect = _effect;
418
+ const runner = e.run.bind(e);
419
+ runner.effect = e;
282
420
  return runner;
283
421
  }
284
422
  function stop(runner) {
285
423
  runner.effect.stop();
286
424
  }
287
425
  let shouldTrack = true;
288
- let pauseScheduleStack = 0;
289
426
  const trackStack = [];
290
427
  function pauseTracking() {
291
428
  trackStack.push(shouldTrack);
@@ -299,192 +436,423 @@ var VueReactivity = (function (exports) {
299
436
  const last = trackStack.pop();
300
437
  shouldTrack = last === void 0 ? true : last;
301
438
  }
302
- function pauseScheduling() {
303
- pauseScheduleStack++;
304
- }
305
- function resetScheduling() {
306
- pauseScheduleStack--;
307
- while (!pauseScheduleStack && queueEffectSchedulers.length) {
308
- queueEffectSchedulers.shift()();
439
+ function onEffectCleanup(fn, failSilently = false) {
440
+ if (activeSub instanceof ReactiveEffect) {
441
+ activeSub.cleanup = fn;
442
+ } else if (!failSilently) {
443
+ warn(
444
+ `onEffectCleanup() was called when there was no active effect to associate with.`
445
+ );
309
446
  }
310
447
  }
311
- function trackEffect(effect2, dep, debuggerEventExtraInfo) {
312
- var _a;
313
- if (dep.get(effect2) !== effect2._trackId) {
314
- dep.set(effect2, effect2._trackId);
315
- const oldDep = effect2.deps[effect2._depsLength];
316
- if (oldDep !== dep) {
317
- if (oldDep) {
318
- cleanupDepEffect(oldDep, effect2);
319
- }
320
- effect2.deps[effect2._depsLength++] = dep;
321
- } else {
322
- effect2._depsLength++;
448
+ function cleanupEffect(e) {
449
+ const { cleanup } = e;
450
+ e.cleanup = void 0;
451
+ if (cleanup) {
452
+ const prevSub = activeSub;
453
+ activeSub = void 0;
454
+ try {
455
+ cleanup();
456
+ } finally {
457
+ activeSub = prevSub;
323
458
  }
459
+ }
460
+ }
461
+
462
+ let globalVersion = 0;
463
+ class Dep {
464
+ constructor(computed) {
465
+ this.computed = computed;
466
+ this.version = 0;
467
+ /**
468
+ * Link between this dep and the current active effect
469
+ */
470
+ this.activeLink = void 0;
471
+ /**
472
+ * Doubly linked list representing the subscribing effects (tail)
473
+ */
474
+ this.subs = void 0;
324
475
  {
325
- (_a = effect2.onTrack) == null ? void 0 : _a.call(effect2, extend({ effect: effect2 }, debuggerEventExtraInfo));
476
+ this.subsHead = void 0;
326
477
  }
327
478
  }
328
- }
329
- const queueEffectSchedulers = [];
330
- function triggerEffects(dep, dirtyLevel, debuggerEventExtraInfo) {
331
- var _a;
332
- pauseScheduling();
333
- for (const effect2 of dep.keys()) {
334
- let tracking;
335
- if (effect2._dirtyLevel < dirtyLevel && (tracking != null ? tracking : tracking = dep.get(effect2) === effect2._trackId)) {
336
- effect2._shouldSchedule || (effect2._shouldSchedule = effect2._dirtyLevel === 0);
337
- effect2._dirtyLevel = dirtyLevel;
479
+ track(debugInfo) {
480
+ if (!activeSub || !shouldTrack) {
481
+ return;
338
482
  }
339
- if (effect2._shouldSchedule && (tracking != null ? tracking : tracking = dep.get(effect2) === effect2._trackId)) {
340
- {
341
- (_a = effect2.onTrigger) == null ? void 0 : _a.call(effect2, extend({ effect: effect2 }, debuggerEventExtraInfo));
483
+ let link = this.activeLink;
484
+ if (link === void 0 || link.sub !== activeSub) {
485
+ link = this.activeLink = {
486
+ dep: this,
487
+ sub: activeSub,
488
+ version: this.version,
489
+ nextDep: void 0,
490
+ prevDep: void 0,
491
+ nextSub: void 0,
492
+ prevSub: void 0,
493
+ prevActiveLink: void 0
494
+ };
495
+ if (!activeSub.deps) {
496
+ activeSub.deps = activeSub.depsTail = link;
497
+ } else {
498
+ link.prevDep = activeSub.depsTail;
499
+ activeSub.depsTail.nextDep = link;
500
+ activeSub.depsTail = link;
501
+ }
502
+ if (activeSub.flags & 4) {
503
+ addSub(link);
504
+ }
505
+ } else if (link.version === -1) {
506
+ link.version = this.version;
507
+ if (link.nextDep) {
508
+ const next = link.nextDep;
509
+ next.prevDep = link.prevDep;
510
+ if (link.prevDep) {
511
+ link.prevDep.nextDep = next;
512
+ }
513
+ link.prevDep = activeSub.depsTail;
514
+ link.nextDep = void 0;
515
+ activeSub.depsTail.nextDep = link;
516
+ activeSub.depsTail = link;
517
+ if (activeSub.deps === link) {
518
+ activeSub.deps = next;
519
+ }
342
520
  }
343
- effect2.trigger();
344
- if ((!effect2._runnings || effect2.allowRecurse) && effect2._dirtyLevel !== 2) {
345
- effect2._shouldSchedule = false;
346
- if (effect2.scheduler) {
347
- queueEffectSchedulers.push(effect2.scheduler);
521
+ }
522
+ if (activeSub.onTrack) {
523
+ activeSub.onTrack(
524
+ extend(
525
+ {
526
+ effect: activeSub
527
+ },
528
+ debugInfo
529
+ )
530
+ );
531
+ }
532
+ return link;
533
+ }
534
+ trigger(debugInfo) {
535
+ this.version++;
536
+ globalVersion++;
537
+ this.notify(debugInfo);
538
+ }
539
+ notify(debugInfo) {
540
+ startBatch();
541
+ try {
542
+ if (true) {
543
+ for (let head = this.subsHead; head; head = head.nextSub) {
544
+ if (head.sub.onTrigger && !(head.sub.flags & 8)) {
545
+ head.sub.onTrigger(
546
+ extend(
547
+ {
548
+ effect: head.sub
549
+ },
550
+ debugInfo
551
+ )
552
+ );
553
+ }
348
554
  }
349
555
  }
556
+ for (let link = this.subs; link; link = link.prevSub) {
557
+ link.sub.notify();
558
+ }
559
+ } finally {
560
+ endBatch();
350
561
  }
351
562
  }
352
- resetScheduling();
353
563
  }
354
-
355
- const createDep = (cleanup, computed) => {
356
- const dep = /* @__PURE__ */ new Map();
357
- dep.cleanup = cleanup;
358
- dep.computed = computed;
359
- return dep;
360
- };
361
-
564
+ function addSub(link) {
565
+ const computed = link.dep.computed;
566
+ if (computed && !link.dep.subs) {
567
+ computed.flags |= 4 | 16;
568
+ for (let l = computed.deps; l; l = l.nextDep) {
569
+ addSub(l);
570
+ }
571
+ }
572
+ const currentTail = link.dep.subs;
573
+ if (currentTail !== link) {
574
+ link.prevSub = currentTail;
575
+ if (currentTail)
576
+ currentTail.nextSub = link;
577
+ }
578
+ if (link.dep.subsHead === void 0) {
579
+ link.dep.subsHead = link;
580
+ }
581
+ link.dep.subs = link;
582
+ }
362
583
  const targetMap = /* @__PURE__ */ new WeakMap();
363
- const ITERATE_KEY = Symbol("iterate" );
364
- const MAP_KEY_ITERATE_KEY = Symbol("Map key iterate" );
584
+ const ITERATE_KEY = Symbol("Object iterate" );
585
+ const MAP_KEY_ITERATE_KEY = Symbol("Map keys iterate" );
586
+ const ARRAY_ITERATE_KEY = Symbol("Array iterate" );
365
587
  function track(target, type, key) {
366
- if (shouldTrack && activeEffect) {
588
+ if (shouldTrack && activeSub) {
367
589
  let depsMap = targetMap.get(target);
368
590
  if (!depsMap) {
369
591
  targetMap.set(target, depsMap = /* @__PURE__ */ new Map());
370
592
  }
371
593
  let dep = depsMap.get(key);
372
594
  if (!dep) {
373
- depsMap.set(key, dep = createDep(() => depsMap.delete(key)));
595
+ depsMap.set(key, dep = new Dep());
374
596
  }
375
- trackEffect(
376
- activeEffect,
377
- dep,
378
- {
597
+ {
598
+ dep.track({
379
599
  target,
380
600
  type,
381
601
  key
382
- }
383
- );
602
+ });
603
+ }
384
604
  }
385
605
  }
386
606
  function trigger(target, type, key, newValue, oldValue, oldTarget) {
387
607
  const depsMap = targetMap.get(target);
388
608
  if (!depsMap) {
609
+ globalVersion++;
389
610
  return;
390
611
  }
391
612
  let deps = [];
392
613
  if (type === "clear") {
393
614
  deps = [...depsMap.values()];
394
- } else if (key === "length" && isArray(target)) {
395
- const newLength = Number(newValue);
396
- depsMap.forEach((dep, key2) => {
397
- if (key2 === "length" || !isSymbol(key2) && key2 >= newLength) {
398
- deps.push(dep);
399
- }
400
- });
401
615
  } else {
402
- if (key !== void 0) {
403
- deps.push(depsMap.get(key));
404
- }
405
- switch (type) {
406
- case "add":
407
- if (!isArray(target)) {
408
- deps.push(depsMap.get(ITERATE_KEY));
409
- if (isMap(target)) {
410
- deps.push(depsMap.get(MAP_KEY_ITERATE_KEY));
411
- }
412
- } else if (isIntegerKey(key)) {
413
- deps.push(depsMap.get("length"));
616
+ const targetIsArray = isArray(target);
617
+ const isArrayIndex = targetIsArray && isIntegerKey(key);
618
+ if (targetIsArray && key === "length") {
619
+ const newLength = Number(newValue);
620
+ depsMap.forEach((dep, key2) => {
621
+ if (key2 === "length" || key2 === ARRAY_ITERATE_KEY || !isSymbol(key2) && key2 >= newLength) {
622
+ deps.push(dep);
414
623
  }
415
- break;
416
- case "delete":
417
- if (!isArray(target)) {
418
- deps.push(depsMap.get(ITERATE_KEY));
624
+ });
625
+ } else {
626
+ const push = (dep) => dep && deps.push(dep);
627
+ if (key !== void 0) {
628
+ push(depsMap.get(key));
629
+ }
630
+ if (isArrayIndex) {
631
+ push(depsMap.get(ARRAY_ITERATE_KEY));
632
+ }
633
+ switch (type) {
634
+ case "add":
635
+ if (!targetIsArray) {
636
+ push(depsMap.get(ITERATE_KEY));
637
+ if (isMap(target)) {
638
+ push(depsMap.get(MAP_KEY_ITERATE_KEY));
639
+ }
640
+ } else if (isArrayIndex) {
641
+ push(depsMap.get("length"));
642
+ }
643
+ break;
644
+ case "delete":
645
+ if (!targetIsArray) {
646
+ push(depsMap.get(ITERATE_KEY));
647
+ if (isMap(target)) {
648
+ push(depsMap.get(MAP_KEY_ITERATE_KEY));
649
+ }
650
+ }
651
+ break;
652
+ case "set":
419
653
  if (isMap(target)) {
420
- deps.push(depsMap.get(MAP_KEY_ITERATE_KEY));
654
+ push(depsMap.get(ITERATE_KEY));
421
655
  }
422
- }
423
- break;
424
- case "set":
425
- if (isMap(target)) {
426
- deps.push(depsMap.get(ITERATE_KEY));
427
- }
428
- break;
656
+ break;
657
+ }
429
658
  }
430
659
  }
431
- pauseScheduling();
660
+ startBatch();
432
661
  for (const dep of deps) {
433
- if (dep) {
434
- triggerEffects(
435
- dep,
436
- 4,
437
- {
438
- target,
439
- type,
440
- key,
441
- newValue,
442
- oldValue,
443
- oldTarget
444
- }
445
- );
662
+ {
663
+ dep.trigger({
664
+ target,
665
+ type,
666
+ key,
667
+ newValue,
668
+ oldValue,
669
+ oldTarget
670
+ });
446
671
  }
447
672
  }
448
- resetScheduling();
673
+ endBatch();
449
674
  }
450
675
  function getDepFromReactive(object, key) {
451
676
  var _a;
452
677
  return (_a = targetMap.get(object)) == null ? void 0 : _a.get(key);
453
678
  }
454
679
 
680
+ function reactiveReadArray(array) {
681
+ const raw = toRaw(array);
682
+ if (raw === array)
683
+ return raw;
684
+ track(raw, "iterate", ARRAY_ITERATE_KEY);
685
+ return isShallow(array) ? raw : raw.map(toReactive);
686
+ }
687
+ function shallowReadArray(arr) {
688
+ track(arr = toRaw(arr), "iterate", ARRAY_ITERATE_KEY);
689
+ return arr;
690
+ }
691
+ const arrayInstrumentations = {
692
+ __proto__: null,
693
+ [Symbol.iterator]() {
694
+ return iterator(this, Symbol.iterator, toReactive);
695
+ },
696
+ concat(...args) {
697
+ return reactiveReadArray(this).concat(
698
+ ...args.map((x) => reactiveReadArray(x))
699
+ );
700
+ },
701
+ entries() {
702
+ return iterator(this, "entries", (value) => {
703
+ value[1] = toReactive(value[1]);
704
+ return value;
705
+ });
706
+ },
707
+ every(fn, thisArg) {
708
+ return apply(this, "every", fn, thisArg);
709
+ },
710
+ filter(fn, thisArg) {
711
+ const result = apply(this, "filter", fn, thisArg);
712
+ return isProxy(this) && !isShallow(this) ? result.map(toReactive) : result;
713
+ },
714
+ find(fn, thisArg) {
715
+ const result = apply(this, "find", fn, thisArg);
716
+ return isProxy(this) && !isShallow(this) ? toReactive(result) : result;
717
+ },
718
+ findIndex(fn, thisArg) {
719
+ return apply(this, "findIndex", fn, thisArg);
720
+ },
721
+ findLast(fn, thisArg) {
722
+ const result = apply(this, "findLast", fn, thisArg);
723
+ return isProxy(this) && !isShallow(this) ? toReactive(result) : result;
724
+ },
725
+ findLastIndex(fn, thisArg) {
726
+ return apply(this, "findLastIndex", fn, thisArg);
727
+ },
728
+ // flat, flatMap could benefit from ARRAY_ITERATE but are not straight-forward to implement
729
+ forEach(fn, thisArg) {
730
+ return apply(this, "forEach", fn, thisArg);
731
+ },
732
+ includes(...args) {
733
+ return searchProxy(this, "includes", args);
734
+ },
735
+ indexOf(...args) {
736
+ return searchProxy(this, "indexOf", args);
737
+ },
738
+ join(separator) {
739
+ return reactiveReadArray(this).join(separator);
740
+ },
741
+ // keys() iterator only reads `length`, no optimisation required
742
+ lastIndexOf(...args) {
743
+ return searchProxy(this, "lastIndexOf", args);
744
+ },
745
+ map(fn, thisArg) {
746
+ return apply(this, "map", fn, thisArg);
747
+ },
748
+ pop() {
749
+ return noTracking(this, "pop");
750
+ },
751
+ push(...args) {
752
+ return noTracking(this, "push", args);
753
+ },
754
+ reduce(fn, ...args) {
755
+ return reduce(this, "reduce", fn, args);
756
+ },
757
+ reduceRight(fn, ...args) {
758
+ return reduce(this, "reduceRight", fn, args);
759
+ },
760
+ shift() {
761
+ return noTracking(this, "shift");
762
+ },
763
+ // slice could use ARRAY_ITERATE but also seems to beg for range tracking
764
+ some(fn, thisArg) {
765
+ return apply(this, "some", fn, thisArg);
766
+ },
767
+ splice(...args) {
768
+ return noTracking(this, "splice", args);
769
+ },
770
+ toReversed() {
771
+ return reactiveReadArray(this).toReversed();
772
+ },
773
+ toSorted(comparer) {
774
+ return reactiveReadArray(this).toSorted(comparer);
775
+ },
776
+ toSpliced(...args) {
777
+ return reactiveReadArray(this).toSpliced(...args);
778
+ },
779
+ unshift(...args) {
780
+ return noTracking(this, "unshift", args);
781
+ },
782
+ values() {
783
+ return iterator(this, "values", toReactive);
784
+ }
785
+ };
786
+ function iterator(self, method, wrapValue) {
787
+ const arr = shallowReadArray(self);
788
+ const iter = arr[method]();
789
+ if (arr !== self && !isShallow(self)) {
790
+ iter._next = iter.next;
791
+ iter.next = () => {
792
+ const result = iter._next();
793
+ if (result.value) {
794
+ result.value = wrapValue(result.value);
795
+ }
796
+ return result;
797
+ };
798
+ }
799
+ return iter;
800
+ }
801
+ function apply(self, method, fn, thisArg) {
802
+ const arr = shallowReadArray(self);
803
+ let wrappedFn = fn;
804
+ if (arr !== self) {
805
+ if (!isShallow(self)) {
806
+ wrappedFn = function(item, index) {
807
+ return fn.call(this, toReactive(item), index, self);
808
+ };
809
+ } else if (fn.length > 2) {
810
+ wrappedFn = function(item, index) {
811
+ return fn.call(this, item, index, self);
812
+ };
813
+ }
814
+ }
815
+ return arr[method](wrappedFn, thisArg);
816
+ }
817
+ function reduce(self, method, fn, args) {
818
+ const arr = shallowReadArray(self);
819
+ let wrappedFn = fn;
820
+ if (arr !== self) {
821
+ if (!isShallow(self)) {
822
+ wrappedFn = function(acc, item, index) {
823
+ return fn.call(this, acc, toReactive(item), index, self);
824
+ };
825
+ } else if (fn.length > 3) {
826
+ wrappedFn = function(acc, item, index) {
827
+ return fn.call(this, acc, item, index, self);
828
+ };
829
+ }
830
+ }
831
+ return arr[method](wrappedFn, ...args);
832
+ }
833
+ function searchProxy(self, method, args) {
834
+ const arr = toRaw(self);
835
+ track(arr, "iterate", ARRAY_ITERATE_KEY);
836
+ const res = arr[method](...args);
837
+ if ((res === -1 || res === false) && isProxy(args[0])) {
838
+ args[0] = toRaw(args[0]);
839
+ return arr[method](...args);
840
+ }
841
+ return res;
842
+ }
843
+ function noTracking(self, method, args = []) {
844
+ pauseTracking();
845
+ startBatch();
846
+ const res = toRaw(self)[method].apply(self, args);
847
+ endBatch();
848
+ resetTracking();
849
+ return res;
850
+ }
851
+
455
852
  const isNonTrackableKeys = /* @__PURE__ */ makeMap(`__proto__,__v_isRef,__isVue`);
456
853
  const builtInSymbols = new Set(
457
854
  /* @__PURE__ */ Object.getOwnPropertyNames(Symbol).filter((key) => key !== "arguments" && key !== "caller").map((key) => Symbol[key]).filter(isSymbol)
458
855
  );
459
- const arrayInstrumentations = /* @__PURE__ */ createArrayInstrumentations();
460
- function createArrayInstrumentations() {
461
- const instrumentations = {};
462
- ["includes", "indexOf", "lastIndexOf"].forEach((key) => {
463
- instrumentations[key] = function(...args) {
464
- const arr = toRaw(this);
465
- for (let i = 0, l = this.length; i < l; i++) {
466
- track(arr, "get", i + "");
467
- }
468
- const res = arr[key](...args);
469
- if (res === -1 || res === false) {
470
- return arr[key](...args.map(toRaw));
471
- } else {
472
- return res;
473
- }
474
- };
475
- });
476
- ["push", "pop", "shift", "unshift", "splice"].forEach((key) => {
477
- instrumentations[key] = function(...args) {
478
- pauseTracking();
479
- pauseScheduling();
480
- const res = toRaw(this)[key].apply(this, args);
481
- resetScheduling();
482
- resetTracking();
483
- return res;
484
- };
485
- });
486
- return instrumentations;
487
- }
488
856
  function hasOwnProperty(key) {
489
857
  if (!isSymbol(key))
490
858
  key = String(key);
@@ -515,14 +883,22 @@ var VueReactivity = (function (exports) {
515
883
  }
516
884
  const targetIsArray = isArray(target);
517
885
  if (!isReadonly2) {
518
- if (targetIsArray && hasOwn(arrayInstrumentations, key)) {
519
- return Reflect.get(arrayInstrumentations, key, receiver);
886
+ let fn;
887
+ if (targetIsArray && (fn = arrayInstrumentations[key])) {
888
+ return fn;
520
889
  }
521
890
  if (key === "hasOwnProperty") {
522
891
  return hasOwnProperty;
523
892
  }
524
893
  }
525
- const res = Reflect.get(target, key, receiver);
894
+ const res = Reflect.get(
895
+ target,
896
+ key,
897
+ // if this is a proxy wrapping a ref, return methods using the raw ref
898
+ // as receiver so that we don't have to call `toRaw` on the ref in all
899
+ // its class methods
900
+ isRef(target) ? target : receiver
901
+ );
526
902
  if (isSymbol(key) ? builtInSymbols.has(key) : isNonTrackableKeys(key)) {
527
903
  return res;
528
904
  }
@@ -1021,110 +1397,8 @@ var VueReactivity = (function (exports) {
1021
1397
  const toReactive = (value) => isObject(value) ? reactive(value) : value;
1022
1398
  const toReadonly = (value) => isObject(value) ? readonly(value) : value;
1023
1399
 
1024
- const COMPUTED_SIDE_EFFECT_WARN = `Computed is still dirty after getter evaluation, likely because a computed is mutating its own dependency in its getter. State mutations in computed getters should be avoided. Check the docs for more details: https://vuejs.org/guide/essentials/computed.html#getters-should-be-side-effect-free`;
1025
- class ComputedRefImpl {
1026
- constructor(getter, _setter, isReadonly, isSSR) {
1027
- this.getter = getter;
1028
- this._setter = _setter;
1029
- this.dep = void 0;
1030
- this.__v_isRef = true;
1031
- this["__v_isReadonly"] = false;
1032
- this.effect = new ReactiveEffect(
1033
- () => getter(this._value),
1034
- () => triggerRefValue(
1035
- this,
1036
- this.effect._dirtyLevel === 2 ? 2 : 3
1037
- )
1038
- );
1039
- this.effect.computed = this;
1040
- this.effect.active = this._cacheable = !isSSR;
1041
- this["__v_isReadonly"] = isReadonly;
1042
- }
1043
- get value() {
1044
- const self = toRaw(this);
1045
- if ((!self._cacheable || self.effect.dirty) && hasChanged(self._value, self._value = self.effect.run())) {
1046
- triggerRefValue(self, 4);
1047
- }
1048
- trackRefValue(self);
1049
- if (self.effect._dirtyLevel >= 2) {
1050
- if (this._warnRecursive) {
1051
- warn(COMPUTED_SIDE_EFFECT_WARN, `
1052
-
1053
- getter: `, this.getter);
1054
- }
1055
- triggerRefValue(self, 2);
1056
- }
1057
- return self._value;
1058
- }
1059
- set value(newValue) {
1060
- this._setter(newValue);
1061
- }
1062
- // #region polyfill _dirty for backward compatibility third party code for Vue <= 3.3.x
1063
- get _dirty() {
1064
- return this.effect.dirty;
1065
- }
1066
- set _dirty(v) {
1067
- this.effect.dirty = v;
1068
- }
1069
- // #endregion
1070
- }
1071
- function computed(getterOrOptions, debugOptions, isSSR = false) {
1072
- let getter;
1073
- let setter;
1074
- const onlyGetter = isFunction(getterOrOptions);
1075
- if (onlyGetter) {
1076
- getter = getterOrOptions;
1077
- setter = () => {
1078
- warn("Write operation failed: computed value is readonly");
1079
- } ;
1080
- } else {
1081
- getter = getterOrOptions.get;
1082
- setter = getterOrOptions.set;
1083
- }
1084
- const cRef = new ComputedRefImpl(getter, setter, onlyGetter || !setter, isSSR);
1085
- if (debugOptions && !isSSR) {
1086
- cRef.effect.onTrack = debugOptions.onTrack;
1087
- cRef.effect.onTrigger = debugOptions.onTrigger;
1088
- }
1089
- return cRef;
1090
- }
1091
-
1092
- function trackRefValue(ref2) {
1093
- var _a;
1094
- if (shouldTrack && activeEffect) {
1095
- ref2 = toRaw(ref2);
1096
- trackEffect(
1097
- activeEffect,
1098
- (_a = ref2.dep) != null ? _a : ref2.dep = createDep(
1099
- () => ref2.dep = void 0,
1100
- ref2 instanceof ComputedRefImpl ? ref2 : void 0
1101
- ),
1102
- {
1103
- target: ref2,
1104
- type: "get",
1105
- key: "value"
1106
- }
1107
- );
1108
- }
1109
- }
1110
- function triggerRefValue(ref2, dirtyLevel = 4, newVal) {
1111
- ref2 = toRaw(ref2);
1112
- const dep = ref2.dep;
1113
- if (dep) {
1114
- triggerEffects(
1115
- dep,
1116
- dirtyLevel,
1117
- {
1118
- target: ref2,
1119
- type: "set",
1120
- key: "value",
1121
- newValue: newVal
1122
- }
1123
- );
1124
- }
1125
- }
1126
1400
  function isRef(r) {
1127
- return !!(r && r.__v_isRef === true);
1401
+ return r ? r.__v_isRef === true : false;
1128
1402
  }
1129
1403
  function ref(value) {
1130
1404
  return createRef(value, false);
@@ -1141,27 +1415,49 @@ getter: `, this.getter);
1141
1415
  class RefImpl {
1142
1416
  constructor(value, __v_isShallow) {
1143
1417
  this.__v_isShallow = __v_isShallow;
1144
- this.dep = void 0;
1418
+ this.dep = new Dep();
1145
1419
  this.__v_isRef = true;
1146
1420
  this._rawValue = __v_isShallow ? value : toRaw(value);
1147
1421
  this._value = __v_isShallow ? value : toReactive(value);
1148
1422
  }
1149
1423
  get value() {
1150
- trackRefValue(this);
1424
+ {
1425
+ this.dep.track({
1426
+ target: this,
1427
+ type: "get",
1428
+ key: "value"
1429
+ });
1430
+ }
1151
1431
  return this._value;
1152
1432
  }
1153
- set value(newVal) {
1154
- const useDirectValue = this.__v_isShallow || isShallow(newVal) || isReadonly(newVal);
1155
- newVal = useDirectValue ? newVal : toRaw(newVal);
1156
- if (hasChanged(newVal, this._rawValue)) {
1157
- this._rawValue = newVal;
1158
- this._value = useDirectValue ? newVal : toReactive(newVal);
1159
- triggerRefValue(this, 4, newVal);
1433
+ set value(newValue) {
1434
+ const oldValue = this._rawValue;
1435
+ const useDirectValue = this.__v_isShallow || isShallow(newValue) || isReadonly(newValue);
1436
+ newValue = useDirectValue ? newValue : toRaw(newValue);
1437
+ if (hasChanged(newValue, oldValue)) {
1438
+ this._rawValue = newValue;
1439
+ this._value = useDirectValue ? newValue : toReactive(newValue);
1440
+ {
1441
+ this.dep.trigger({
1442
+ target: this,
1443
+ type: "set",
1444
+ key: "value",
1445
+ newValue,
1446
+ oldValue
1447
+ });
1448
+ }
1160
1449
  }
1161
1450
  }
1162
1451
  }
1163
1452
  function triggerRef(ref2) {
1164
- triggerRefValue(ref2, 4, ref2.value );
1453
+ {
1454
+ ref2.dep.trigger({
1455
+ target: ref2,
1456
+ type: "set",
1457
+ key: "value",
1458
+ newValue: ref2._value
1459
+ });
1460
+ }
1165
1461
  }
1166
1462
  function unref(ref2) {
1167
1463
  return isRef(ref2) ? ref2.value : ref2;
@@ -1186,12 +1482,9 @@ getter: `, this.getter);
1186
1482
  }
1187
1483
  class CustomRefImpl {
1188
1484
  constructor(factory) {
1189
- this.dep = void 0;
1190
1485
  this.__v_isRef = true;
1191
- const { get, set } = factory(
1192
- () => trackRefValue(this),
1193
- () => triggerRefValue(this)
1194
- );
1486
+ const dep = this.dep = new Dep();
1487
+ const { get, set } = factory(dep.track.bind(dep), dep.trigger.bind(dep));
1195
1488
  this._get = get;
1196
1489
  this._set = set;
1197
1490
  }
@@ -1259,7 +1552,89 @@ getter: `, this.getter);
1259
1552
  return isRef(val) ? val : new ObjectRefImpl(source, key, defaultValue);
1260
1553
  }
1261
1554
 
1262
- const deferredComputed = computed;
1555
+ class ComputedRefImpl {
1556
+ constructor(fn, setter, isSSR) {
1557
+ this.fn = fn;
1558
+ this.setter = setter;
1559
+ /**
1560
+ * @internal
1561
+ */
1562
+ this._value = void 0;
1563
+ /**
1564
+ * @internal
1565
+ */
1566
+ this.dep = new Dep(this);
1567
+ /**
1568
+ * @internal
1569
+ */
1570
+ this.__v_isRef = true;
1571
+ // A computed is also a subscriber that tracks other deps
1572
+ /**
1573
+ * @internal
1574
+ */
1575
+ this.deps = void 0;
1576
+ /**
1577
+ * @internal
1578
+ */
1579
+ this.depsTail = void 0;
1580
+ /**
1581
+ * @internal
1582
+ */
1583
+ this.flags = 16;
1584
+ /**
1585
+ * @internal
1586
+ */
1587
+ this.globalVersion = globalVersion - 1;
1588
+ // for backwards compat
1589
+ this.effect = this;
1590
+ this.__v_isReadonly = !setter;
1591
+ this.isSSR = isSSR;
1592
+ }
1593
+ /**
1594
+ * @internal
1595
+ */
1596
+ notify() {
1597
+ if (activeSub !== this) {
1598
+ this.flags |= 16;
1599
+ this.dep.notify();
1600
+ }
1601
+ }
1602
+ get value() {
1603
+ const link = this.dep.track({
1604
+ target: this,
1605
+ type: "get",
1606
+ key: "value"
1607
+ }) ;
1608
+ refreshComputed(this);
1609
+ if (link) {
1610
+ link.version = this.dep.version;
1611
+ }
1612
+ return this._value;
1613
+ }
1614
+ set value(newValue) {
1615
+ if (this.setter) {
1616
+ this.setter(newValue);
1617
+ } else {
1618
+ warn("Write operation failed: computed value is readonly");
1619
+ }
1620
+ }
1621
+ }
1622
+ function computed(getterOrOptions, debugOptions, isSSR = false) {
1623
+ let getter;
1624
+ let setter;
1625
+ if (isFunction(getterOrOptions)) {
1626
+ getter = getterOrOptions;
1627
+ } else {
1628
+ getter = getterOrOptions.get;
1629
+ setter = getterOrOptions.set;
1630
+ }
1631
+ const cRef = new ComputedRefImpl(getter, setter, isSSR);
1632
+ if (debugOptions && !isSSR) {
1633
+ cRef.onTrack = debugOptions.onTrack;
1634
+ cRef.onTrigger = debugOptions.onTrigger;
1635
+ }
1636
+ return cRef;
1637
+ }
1263
1638
 
1264
1639
  const TrackOpTypes = {
1265
1640
  "GET": "get",
@@ -1280,15 +1655,17 @@ getter: `, this.getter);
1280
1655
  "RAW": "__v_raw"
1281
1656
  };
1282
1657
 
1658
+ exports.ARRAY_ITERATE_KEY = ARRAY_ITERATE_KEY;
1659
+ exports.EffectFlags = EffectFlags;
1283
1660
  exports.EffectScope = EffectScope;
1284
1661
  exports.ITERATE_KEY = ITERATE_KEY;
1662
+ exports.MAP_KEY_ITERATE_KEY = MAP_KEY_ITERATE_KEY;
1285
1663
  exports.ReactiveEffect = ReactiveEffect;
1286
1664
  exports.ReactiveFlags = ReactiveFlags;
1287
1665
  exports.TrackOpTypes = TrackOpTypes;
1288
1666
  exports.TriggerOpTypes = TriggerOpTypes;
1289
1667
  exports.computed = computed;
1290
1668
  exports.customRef = customRef;
1291
- exports.deferredComputed = deferredComputed;
1292
1669
  exports.effect = effect;
1293
1670
  exports.effectScope = effectScope;
1294
1671
  exports.enableTracking = enableTracking;
@@ -1299,20 +1676,23 @@ getter: `, this.getter);
1299
1676
  exports.isRef = isRef;
1300
1677
  exports.isShallow = isShallow;
1301
1678
  exports.markRaw = markRaw;
1679
+ exports.onEffectCleanup = onEffectCleanup;
1302
1680
  exports.onScopeDispose = onScopeDispose;
1303
- exports.pauseScheduling = pauseScheduling;
1304
1681
  exports.pauseTracking = pauseTracking;
1305
1682
  exports.proxyRefs = proxyRefs;
1306
1683
  exports.reactive = reactive;
1684
+ exports.reactiveReadArray = reactiveReadArray;
1307
1685
  exports.readonly = readonly;
1308
1686
  exports.ref = ref;
1309
- exports.resetScheduling = resetScheduling;
1310
1687
  exports.resetTracking = resetTracking;
1311
1688
  exports.shallowReactive = shallowReactive;
1689
+ exports.shallowReadArray = shallowReadArray;
1312
1690
  exports.shallowReadonly = shallowReadonly;
1313
1691
  exports.shallowRef = shallowRef;
1314
1692
  exports.stop = stop;
1315
1693
  exports.toRaw = toRaw;
1694
+ exports.toReactive = toReactive;
1695
+ exports.toReadonly = toReadonly;
1316
1696
  exports.toRef = toRef;
1317
1697
  exports.toRefs = toRefs;
1318
1698
  exports.toValue = toValue;