@vue/reactivity 3.1.1 → 3.1.5

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.
@@ -201,34 +201,36 @@ const get = /*#__PURE__*/ createGetter();
201
201
  const shallowGet = /*#__PURE__*/ createGetter(false, true);
202
202
  const readonlyGet = /*#__PURE__*/ createGetter(true);
203
203
  const shallowReadonlyGet = /*#__PURE__*/ createGetter(true, true);
204
- const arrayInstrumentations = {};
205
- ['includes', 'indexOf', 'lastIndexOf'].forEach(key => {
206
- const method = Array.prototype[key];
207
- arrayInstrumentations[key] = function (...args) {
208
- const arr = toRaw(this);
209
- for (let i = 0, l = this.length; i < l; i++) {
210
- track(arr, "get" /* GET */, i + '');
211
- }
212
- // we run the method using the original args first (which may be reactive)
213
- const res = method.apply(arr, args);
214
- if (res === -1 || res === false) {
215
- // if that didn't work, run it again using raw values.
216
- return method.apply(arr, args.map(toRaw));
217
- }
218
- else {
204
+ const arrayInstrumentations = /*#__PURE__*/ createArrayInstrumentations();
205
+ function createArrayInstrumentations() {
206
+ const instrumentations = {};
207
+ ['includes', 'indexOf', 'lastIndexOf'].forEach(key => {
208
+ instrumentations[key] = function (...args) {
209
+ const arr = toRaw(this);
210
+ for (let i = 0, l = this.length; i < l; i++) {
211
+ track(arr, "get" /* GET */, i + '');
212
+ }
213
+ // we run the method using the original args first (which may be reactive)
214
+ const res = arr[key](...args);
215
+ if (res === -1 || res === false) {
216
+ // if that didn't work, run it again using raw values.
217
+ return arr[key](...args.map(toRaw));
218
+ }
219
+ else {
220
+ return res;
221
+ }
222
+ };
223
+ });
224
+ ['push', 'pop', 'shift', 'unshift', 'splice'].forEach(key => {
225
+ instrumentations[key] = function (...args) {
226
+ pauseTracking();
227
+ const res = toRaw(this)[key].apply(this, args);
228
+ resetTracking();
219
229
  return res;
220
- }
221
- };
222
- });
223
- ['push', 'pop', 'shift', 'unshift', 'splice'].forEach(key => {
224
- const method = Array.prototype[key];
225
- arrayInstrumentations[key] = function (...args) {
226
- pauseTracking();
227
- const res = method.apply(this, args);
228
- resetTracking();
229
- return res;
230
- };
231
- });
230
+ };
231
+ });
232
+ return instrumentations;
233
+ }
232
234
  function createGetter(isReadonly = false, shallow = false) {
233
235
  return function get(target, key, receiver) {
234
236
  if (key === "__v_isReactive" /* IS_REACTIVE */) {
@@ -347,14 +349,14 @@ const readonlyHandlers = {
347
349
  return true;
348
350
  }
349
351
  };
350
- const shallowReactiveHandlers = shared.extend({}, mutableHandlers, {
352
+ const shallowReactiveHandlers = /*#__PURE__*/ shared.extend({}, mutableHandlers, {
351
353
  get: shallowGet,
352
354
  set: shallowSet
353
355
  });
354
356
  // Props handlers are special in the sense that it should not unwrap top-level
355
357
  // refs (in order to allow refs to be explicitly passed down), but should
356
358
  // retain the reactivity of the normal readonly object.
357
- const shallowReadonlyHandlers = shared.extend({}, readonlyHandlers, {
359
+ const shallowReadonlyHandlers = /*#__PURE__*/ shared.extend({}, readonlyHandlers, {
358
360
  get: shallowReadonlyGet
359
361
  });
360
362
 
@@ -524,73 +526,82 @@ function createReadonlyMethod(type) {
524
526
  return type === "delete" /* DELETE */ ? false : this;
525
527
  };
526
528
  }
527
- const mutableInstrumentations = {
528
- get(key) {
529
- return get$1(this, key);
530
- },
531
- get size() {
532
- return size(this);
533
- },
534
- has: has$1,
535
- add,
536
- set: set$1,
537
- delete: deleteEntry,
538
- clear,
539
- forEach: createForEach(false, false)
540
- };
541
- const shallowInstrumentations = {
542
- get(key) {
543
- return get$1(this, key, false, true);
544
- },
545
- get size() {
546
- return size(this);
547
- },
548
- has: has$1,
549
- add,
550
- set: set$1,
551
- delete: deleteEntry,
552
- clear,
553
- forEach: createForEach(false, true)
554
- };
555
- const readonlyInstrumentations = {
556
- get(key) {
557
- return get$1(this, key, true);
558
- },
559
- get size() {
560
- return size(this, true);
561
- },
562
- has(key) {
563
- return has$1.call(this, key, true);
564
- },
565
- add: createReadonlyMethod("add" /* ADD */),
566
- set: createReadonlyMethod("set" /* SET */),
567
- delete: createReadonlyMethod("delete" /* DELETE */),
568
- clear: createReadonlyMethod("clear" /* CLEAR */),
569
- forEach: createForEach(true, false)
570
- };
571
- const shallowReadonlyInstrumentations = {
572
- get(key) {
573
- return get$1(this, key, true, true);
574
- },
575
- get size() {
576
- return size(this, true);
577
- },
578
- has(key) {
579
- return has$1.call(this, key, true);
580
- },
581
- add: createReadonlyMethod("add" /* ADD */),
582
- set: createReadonlyMethod("set" /* SET */),
583
- delete: createReadonlyMethod("delete" /* DELETE */),
584
- clear: createReadonlyMethod("clear" /* CLEAR */),
585
- forEach: createForEach(true, true)
586
- };
587
- const iteratorMethods = ['keys', 'values', 'entries', Symbol.iterator];
588
- iteratorMethods.forEach(method => {
589
- mutableInstrumentations[method] = createIterableMethod(method, false, false);
590
- readonlyInstrumentations[method] = createIterableMethod(method, true, false);
591
- shallowInstrumentations[method] = createIterableMethod(method, false, true);
592
- shallowReadonlyInstrumentations[method] = createIterableMethod(method, true, true);
593
- });
529
+ function createInstrumentations() {
530
+ const mutableInstrumentations = {
531
+ get(key) {
532
+ return get$1(this, key);
533
+ },
534
+ get size() {
535
+ return size(this);
536
+ },
537
+ has: has$1,
538
+ add,
539
+ set: set$1,
540
+ delete: deleteEntry,
541
+ clear,
542
+ forEach: createForEach(false, false)
543
+ };
544
+ const shallowInstrumentations = {
545
+ get(key) {
546
+ return get$1(this, key, false, true);
547
+ },
548
+ get size() {
549
+ return size(this);
550
+ },
551
+ has: has$1,
552
+ add,
553
+ set: set$1,
554
+ delete: deleteEntry,
555
+ clear,
556
+ forEach: createForEach(false, true)
557
+ };
558
+ const readonlyInstrumentations = {
559
+ get(key) {
560
+ return get$1(this, key, true);
561
+ },
562
+ get size() {
563
+ return size(this, true);
564
+ },
565
+ has(key) {
566
+ return has$1.call(this, key, true);
567
+ },
568
+ add: createReadonlyMethod("add" /* ADD */),
569
+ set: createReadonlyMethod("set" /* SET */),
570
+ delete: createReadonlyMethod("delete" /* DELETE */),
571
+ clear: createReadonlyMethod("clear" /* CLEAR */),
572
+ forEach: createForEach(true, false)
573
+ };
574
+ const shallowReadonlyInstrumentations = {
575
+ get(key) {
576
+ return get$1(this, key, true, true);
577
+ },
578
+ get size() {
579
+ return size(this, true);
580
+ },
581
+ has(key) {
582
+ return has$1.call(this, key, true);
583
+ },
584
+ add: createReadonlyMethod("add" /* ADD */),
585
+ set: createReadonlyMethod("set" /* SET */),
586
+ delete: createReadonlyMethod("delete" /* DELETE */),
587
+ clear: createReadonlyMethod("clear" /* CLEAR */),
588
+ forEach: createForEach(true, true)
589
+ };
590
+ const iteratorMethods = ['keys', 'values', 'entries', Symbol.iterator];
591
+ iteratorMethods.forEach(method => {
592
+ mutableInstrumentations[method] = createIterableMethod(method, false, false);
593
+ readonlyInstrumentations[method] = createIterableMethod(method, true, false);
594
+ shallowInstrumentations[method] = createIterableMethod(method, false, true);
595
+ shallowReadonlyInstrumentations[method] = createIterableMethod(method, true, true);
596
+ });
597
+ return [
598
+ mutableInstrumentations,
599
+ readonlyInstrumentations,
600
+ shallowInstrumentations,
601
+ shallowReadonlyInstrumentations
602
+ ];
603
+ }
604
+ const [mutableInstrumentations, readonlyInstrumentations, shallowInstrumentations, shallowReadonlyInstrumentations] = /* #__PURE__*/ createInstrumentations();
594
605
  function createInstrumentationGetter(isReadonly, shallow) {
595
606
  const instrumentations = shallow
596
607
  ? isReadonly
@@ -615,16 +626,16 @@ function createInstrumentationGetter(isReadonly, shallow) {
615
626
  };
616
627
  }
617
628
  const mutableCollectionHandlers = {
618
- get: createInstrumentationGetter(false, false)
629
+ get: /*#__PURE__*/ createInstrumentationGetter(false, false)
619
630
  };
620
631
  const shallowCollectionHandlers = {
621
- get: createInstrumentationGetter(false, true)
632
+ get: /*#__PURE__*/ createInstrumentationGetter(false, true)
622
633
  };
623
634
  const readonlyCollectionHandlers = {
624
- get: createInstrumentationGetter(true, false)
635
+ get: /*#__PURE__*/ createInstrumentationGetter(true, false)
625
636
  };
626
637
  const shallowReadonlyCollectionHandlers = {
627
- get: createInstrumentationGetter(true, true)
638
+ get: /*#__PURE__*/ createInstrumentationGetter(true, true)
628
639
  };
629
640
  function checkIdentityKeys(target, has, key) {
630
641
  const rawKey = toRaw(key);
@@ -750,18 +761,19 @@ function shallowRef(value) {
750
761
  return createRef(value, true);
751
762
  }
752
763
  class RefImpl {
753
- constructor(_rawValue, _shallow = false) {
754
- this._rawValue = _rawValue;
764
+ constructor(value, _shallow = false) {
755
765
  this._shallow = _shallow;
756
766
  this.__v_isRef = true;
757
- this._value = _shallow ? _rawValue : convert(_rawValue);
767
+ this._rawValue = _shallow ? value : toRaw(value);
768
+ this._value = _shallow ? value : convert(value);
758
769
  }
759
770
  get value() {
760
771
  track(toRaw(this), "get" /* GET */, 'value');
761
772
  return this._value;
762
773
  }
763
774
  set value(newVal) {
764
- if (shared.hasChanged(toRaw(newVal), this._rawValue)) {
775
+ newVal = this._shallow ? newVal : toRaw(newVal);
776
+ if (shared.hasChanged(newVal, this._rawValue)) {
765
777
  this._rawValue = newVal;
766
778
  this._value = this._shallow ? newVal : convert(newVal);
767
779
  trigger(toRaw(this), "set" /* SET */, 'value', newVal);
@@ -182,34 +182,36 @@ const get = /*#__PURE__*/ createGetter();
182
182
  const shallowGet = /*#__PURE__*/ createGetter(false, true);
183
183
  const readonlyGet = /*#__PURE__*/ createGetter(true);
184
184
  const shallowReadonlyGet = /*#__PURE__*/ createGetter(true, true);
185
- const arrayInstrumentations = {};
186
- ['includes', 'indexOf', 'lastIndexOf'].forEach(key => {
187
- const method = Array.prototype[key];
188
- arrayInstrumentations[key] = function (...args) {
189
- const arr = toRaw(this);
190
- for (let i = 0, l = this.length; i < l; i++) {
191
- track(arr, "get" /* GET */, i + '');
192
- }
193
- // we run the method using the original args first (which may be reactive)
194
- const res = method.apply(arr, args);
195
- if (res === -1 || res === false) {
196
- // if that didn't work, run it again using raw values.
197
- return method.apply(arr, args.map(toRaw));
198
- }
199
- else {
185
+ const arrayInstrumentations = /*#__PURE__*/ createArrayInstrumentations();
186
+ function createArrayInstrumentations() {
187
+ const instrumentations = {};
188
+ ['includes', 'indexOf', 'lastIndexOf'].forEach(key => {
189
+ instrumentations[key] = function (...args) {
190
+ const arr = toRaw(this);
191
+ for (let i = 0, l = this.length; i < l; i++) {
192
+ track(arr, "get" /* GET */, i + '');
193
+ }
194
+ // we run the method using the original args first (which may be reactive)
195
+ const res = arr[key](...args);
196
+ if (res === -1 || res === false) {
197
+ // if that didn't work, run it again using raw values.
198
+ return arr[key](...args.map(toRaw));
199
+ }
200
+ else {
201
+ return res;
202
+ }
203
+ };
204
+ });
205
+ ['push', 'pop', 'shift', 'unshift', 'splice'].forEach(key => {
206
+ instrumentations[key] = function (...args) {
207
+ pauseTracking();
208
+ const res = toRaw(this)[key].apply(this, args);
209
+ resetTracking();
200
210
  return res;
201
- }
202
- };
203
- });
204
- ['push', 'pop', 'shift', 'unshift', 'splice'].forEach(key => {
205
- const method = Array.prototype[key];
206
- arrayInstrumentations[key] = function (...args) {
207
- pauseTracking();
208
- const res = method.apply(this, args);
209
- resetTracking();
210
- return res;
211
- };
212
- });
211
+ };
212
+ });
213
+ return instrumentations;
214
+ }
213
215
  function createGetter(isReadonly = false, shallow = false) {
214
216
  return function get(target, key, receiver) {
215
217
  if (key === "__v_isReactive" /* IS_REACTIVE */) {
@@ -322,14 +324,14 @@ const readonlyHandlers = {
322
324
  return true;
323
325
  }
324
326
  };
325
- const shallowReactiveHandlers = shared.extend({}, mutableHandlers, {
327
+ const shallowReactiveHandlers = /*#__PURE__*/ shared.extend({}, mutableHandlers, {
326
328
  get: shallowGet,
327
329
  set: shallowSet
328
330
  });
329
331
  // Props handlers are special in the sense that it should not unwrap top-level
330
332
  // refs (in order to allow refs to be explicitly passed down), but should
331
333
  // retain the reactivity of the normal readonly object.
332
- const shallowReadonlyHandlers = shared.extend({}, readonlyHandlers, {
334
+ const shallowReadonlyHandlers = /*#__PURE__*/ shared.extend({}, readonlyHandlers, {
333
335
  get: shallowReadonlyGet
334
336
  });
335
337
 
@@ -485,73 +487,82 @@ function createReadonlyMethod(type) {
485
487
  return type === "delete" /* DELETE */ ? false : this;
486
488
  };
487
489
  }
488
- const mutableInstrumentations = {
489
- get(key) {
490
- return get$1(this, key);
491
- },
492
- get size() {
493
- return size(this);
494
- },
495
- has: has$1,
496
- add,
497
- set: set$1,
498
- delete: deleteEntry,
499
- clear,
500
- forEach: createForEach(false, false)
501
- };
502
- const shallowInstrumentations = {
503
- get(key) {
504
- return get$1(this, key, false, true);
505
- },
506
- get size() {
507
- return size(this);
508
- },
509
- has: has$1,
510
- add,
511
- set: set$1,
512
- delete: deleteEntry,
513
- clear,
514
- forEach: createForEach(false, true)
515
- };
516
- const readonlyInstrumentations = {
517
- get(key) {
518
- return get$1(this, key, true);
519
- },
520
- get size() {
521
- return size(this, true);
522
- },
523
- has(key) {
524
- return has$1.call(this, key, true);
525
- },
526
- add: createReadonlyMethod("add" /* ADD */),
527
- set: createReadonlyMethod("set" /* SET */),
528
- delete: createReadonlyMethod("delete" /* DELETE */),
529
- clear: createReadonlyMethod("clear" /* CLEAR */),
530
- forEach: createForEach(true, false)
531
- };
532
- const shallowReadonlyInstrumentations = {
533
- get(key) {
534
- return get$1(this, key, true, true);
535
- },
536
- get size() {
537
- return size(this, true);
538
- },
539
- has(key) {
540
- return has$1.call(this, key, true);
541
- },
542
- add: createReadonlyMethod("add" /* ADD */),
543
- set: createReadonlyMethod("set" /* SET */),
544
- delete: createReadonlyMethod("delete" /* DELETE */),
545
- clear: createReadonlyMethod("clear" /* CLEAR */),
546
- forEach: createForEach(true, true)
547
- };
548
- const iteratorMethods = ['keys', 'values', 'entries', Symbol.iterator];
549
- iteratorMethods.forEach(method => {
550
- mutableInstrumentations[method] = createIterableMethod(method, false, false);
551
- readonlyInstrumentations[method] = createIterableMethod(method, true, false);
552
- shallowInstrumentations[method] = createIterableMethod(method, false, true);
553
- shallowReadonlyInstrumentations[method] = createIterableMethod(method, true, true);
554
- });
490
+ function createInstrumentations() {
491
+ const mutableInstrumentations = {
492
+ get(key) {
493
+ return get$1(this, key);
494
+ },
495
+ get size() {
496
+ return size(this);
497
+ },
498
+ has: has$1,
499
+ add,
500
+ set: set$1,
501
+ delete: deleteEntry,
502
+ clear,
503
+ forEach: createForEach(false, false)
504
+ };
505
+ const shallowInstrumentations = {
506
+ get(key) {
507
+ return get$1(this, key, false, true);
508
+ },
509
+ get size() {
510
+ return size(this);
511
+ },
512
+ has: has$1,
513
+ add,
514
+ set: set$1,
515
+ delete: deleteEntry,
516
+ clear,
517
+ forEach: createForEach(false, true)
518
+ };
519
+ const readonlyInstrumentations = {
520
+ get(key) {
521
+ return get$1(this, key, true);
522
+ },
523
+ get size() {
524
+ return size(this, true);
525
+ },
526
+ has(key) {
527
+ return has$1.call(this, key, true);
528
+ },
529
+ add: createReadonlyMethod("add" /* ADD */),
530
+ set: createReadonlyMethod("set" /* SET */),
531
+ delete: createReadonlyMethod("delete" /* DELETE */),
532
+ clear: createReadonlyMethod("clear" /* CLEAR */),
533
+ forEach: createForEach(true, false)
534
+ };
535
+ const shallowReadonlyInstrumentations = {
536
+ get(key) {
537
+ return get$1(this, key, true, true);
538
+ },
539
+ get size() {
540
+ return size(this, true);
541
+ },
542
+ has(key) {
543
+ return has$1.call(this, key, true);
544
+ },
545
+ add: createReadonlyMethod("add" /* ADD */),
546
+ set: createReadonlyMethod("set" /* SET */),
547
+ delete: createReadonlyMethod("delete" /* DELETE */),
548
+ clear: createReadonlyMethod("clear" /* CLEAR */),
549
+ forEach: createForEach(true, true)
550
+ };
551
+ const iteratorMethods = ['keys', 'values', 'entries', Symbol.iterator];
552
+ iteratorMethods.forEach(method => {
553
+ mutableInstrumentations[method] = createIterableMethod(method, false, false);
554
+ readonlyInstrumentations[method] = createIterableMethod(method, true, false);
555
+ shallowInstrumentations[method] = createIterableMethod(method, false, true);
556
+ shallowReadonlyInstrumentations[method] = createIterableMethod(method, true, true);
557
+ });
558
+ return [
559
+ mutableInstrumentations,
560
+ readonlyInstrumentations,
561
+ shallowInstrumentations,
562
+ shallowReadonlyInstrumentations
563
+ ];
564
+ }
565
+ const [mutableInstrumentations, readonlyInstrumentations, shallowInstrumentations, shallowReadonlyInstrumentations] = /* #__PURE__*/ createInstrumentations();
555
566
  function createInstrumentationGetter(isReadonly, shallow) {
556
567
  const instrumentations = shallow
557
568
  ? isReadonly
@@ -576,16 +587,16 @@ function createInstrumentationGetter(isReadonly, shallow) {
576
587
  };
577
588
  }
578
589
  const mutableCollectionHandlers = {
579
- get: createInstrumentationGetter(false, false)
590
+ get: /*#__PURE__*/ createInstrumentationGetter(false, false)
580
591
  };
581
592
  const shallowCollectionHandlers = {
582
- get: createInstrumentationGetter(false, true)
593
+ get: /*#__PURE__*/ createInstrumentationGetter(false, true)
583
594
  };
584
595
  const readonlyCollectionHandlers = {
585
- get: createInstrumentationGetter(true, false)
596
+ get: /*#__PURE__*/ createInstrumentationGetter(true, false)
586
597
  };
587
598
  const shallowReadonlyCollectionHandlers = {
588
- get: createInstrumentationGetter(true, true)
599
+ get: /*#__PURE__*/ createInstrumentationGetter(true, true)
589
600
  };
590
601
 
591
602
  const reactiveMap = new WeakMap();
@@ -697,18 +708,19 @@ function shallowRef(value) {
697
708
  return createRef(value, true);
698
709
  }
699
710
  class RefImpl {
700
- constructor(_rawValue, _shallow = false) {
701
- this._rawValue = _rawValue;
711
+ constructor(value, _shallow = false) {
702
712
  this._shallow = _shallow;
703
713
  this.__v_isRef = true;
704
- this._value = _shallow ? _rawValue : convert(_rawValue);
714
+ this._rawValue = _shallow ? value : toRaw(value);
715
+ this._value = _shallow ? value : convert(value);
705
716
  }
706
717
  get value() {
707
718
  track(toRaw(this), "get" /* GET */, 'value');
708
719
  return this._value;
709
720
  }
710
721
  set value(newVal) {
711
- if (shared.hasChanged(toRaw(newVal), this._rawValue)) {
722
+ newVal = this._shallow ? newVal : toRaw(newVal);
723
+ if (shared.hasChanged(newVal, this._rawValue)) {
712
724
  this._rawValue = newVal;
713
725
  this._value = this._shallow ? newVal : convert(newVal);
714
726
  trigger(toRaw(this), "set" /* SET */, 'value', newVal);
@@ -204,7 +204,7 @@ export declare function shallowRef<T>(value: T): Ref<T>;
204
204
  export declare function shallowRef<T = any>(): Ref<T | undefined>;
205
205
 
206
206
  export declare type ShallowUnwrapRef<T> = {
207
- [K in keyof T]: T[K] extends Ref<infer V> ? V : T[K];
207
+ [K in keyof T]: T[K] extends Ref<infer V> ? V : T[K] extends Ref<infer V> | undefined ? unknown extends V ? undefined : V | undefined : T[K];
208
208
  };
209
209
 
210
210
  declare function stop_2(effect: ReactiveEffect): void;
@@ -295,7 +295,7 @@ export declare const enum TriggerOpTypes {
295
295
 
296
296
  export declare function triggerRef(ref: Ref): void;
297
297
 
298
- export declare function unref<T>(ref: T): T extends Ref<infer V> ? V : T;
298
+ export declare function unref<T>(ref: T | Ref<T>): T;
299
299
 
300
300
  export declare type UnwrapNestedRefs<T> = T extends Ref ? T : UnwrapRef<T>;
301
301