@tarojs/runtime 3.7.0-alpha.4 → 3.7.0-alpha.6

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.
@@ -221,6 +221,7 @@ declare class TaroNode extends TaroEventTarget {
221
221
  constructor();
222
222
  private hydrate;
223
223
  private updateChildNodes;
224
+ private updateSingleChild;
224
225
  get _root(): TaroRootElement | null;
225
226
  protected findIndex(refChild: TaroNode): number;
226
227
  get _path(): string;
@@ -599,6 +599,20 @@ var TaroNode = /*@__PURE__*/(function (TaroEventTarget) {
599
599
  value: isClean ? cleanChildNodes : rerenderChildNodes
600
600
  });
601
601
  };
602
+ TaroNode.prototype.updateSingleChild = function updateSingleChild (index) {
603
+ var this$1$1 = this;
604
+
605
+ this.childNodes.forEach(function (child, childIndex) {
606
+ if (isComment(child))
607
+ { return; }
608
+ if (index && childIndex < index)
609
+ { return; }
610
+ this$1$1.enqueueUpdate({
611
+ path: child._path,
612
+ value: this$1$1.hydrate(child)
613
+ });
614
+ });
615
+ };
602
616
  prototypeAccessors._root.get = function () {
603
617
  var _a;
604
618
  return ((_a = this.parentNode) === null || _a === void 0 ? void 0 : _a._root) || null;
@@ -692,22 +706,24 @@ var TaroNode = /*@__PURE__*/(function (TaroEventTarget) {
692
706
  // - cleanRef: false (No need to clean eventSource, because newChild is about to be inserted)
693
707
  // - update: true (Need to update parent.childNodes, because parent.childNodes is reordered)
694
708
  newChild.remove({ cleanRef: false });
709
+ var index = 0;
695
710
  // Data structure
696
711
  newChild.parentNode = this;
697
712
  if (refChild) {
698
713
  // insertBefore & replaceChild
699
- var index = this.findIndex(refChild);
714
+ index = this.findIndex(refChild);
700
715
  this.childNodes.splice(index, 0, newChild);
701
716
  }
702
717
  else {
703
718
  // appendChild
704
719
  this.childNodes.push(newChild);
705
720
  }
721
+ var childNodesLength = this.childNodes.length;
706
722
  // Serialization
707
723
  if (this._root) {
708
724
  if (!refChild) {
709
725
  // appendChild
710
- var isOnlyChild = this.childNodes.length === 1;
726
+ var isOnlyChild = childNodesLength === 1;
711
727
  if (isOnlyChild) {
712
728
  this.updateChildNodes();
713
729
  }
@@ -726,8 +742,26 @@ var TaroNode = /*@__PURE__*/(function (TaroEventTarget) {
726
742
  });
727
743
  }
728
744
  else {
729
- // insertBefore
730
- this.updateChildNodes();
745
+ // insertBefore 有两种更新模式
746
+ // 比方说有 A B C 三个节点,现在要在 C 前插入 D
747
+ // 1. 插入 D,然后更新整个父节点的 childNodes 数组
748
+ // setData({ cn: [A, B, D, C] })
749
+ // 2. 插入 D,然后更新 D 以及 D 之后每个节点的数据
750
+ // setData ({
751
+ // cn.[2]: D,
752
+ // cn.[3]: C,
753
+ // })
754
+ // 由于微信解析 ’cn.[2]‘ 这些路径的时候也需要消耗时间,
755
+ // 所以根据 insertBefore 插入的位置来做不同的处理
756
+ var mark = childNodesLength * 2 / 3;
757
+ if (mark > index) {
758
+ // 如果 insertBefore 的位置在 childNodes 的 2/3 前,则为了避免解析路径消耗过多的时间,采用第一种方式
759
+ this.updateChildNodes();
760
+ }
761
+ else {
762
+ // 如果 insertBefore 的位置在 childNodes 的 2/3 之后,则采用第二种方式,避免 childNodes 的全量更新
763
+ this.updateSingleChild(index);
764
+ }
731
765
  }
732
766
  }
733
767
  MutationObserver.record({
@@ -221,6 +221,7 @@ declare class TaroNode extends TaroEventTarget {
221
221
  constructor();
222
222
  private hydrate;
223
223
  private updateChildNodes;
224
+ private updateSingleChild;
224
225
  get _root(): TaroRootElement | null;
225
226
  protected findIndex(refChild: TaroNode): number;
226
227
  get _path(): string;
@@ -573,6 +573,18 @@ class TaroNode extends TaroEventTarget {
573
573
  value: isClean ? cleanChildNodes : rerenderChildNodes
574
574
  });
575
575
  }
576
+ updateSingleChild(index) {
577
+ this.childNodes.forEach((child, childIndex) => {
578
+ if (isComment(child))
579
+ return;
580
+ if (index && childIndex < index)
581
+ return;
582
+ this.enqueueUpdate({
583
+ path: child._path,
584
+ value: this.hydrate(child)
585
+ });
586
+ });
587
+ }
576
588
  get _root() {
577
589
  var _a;
578
590
  return ((_a = this.parentNode) === null || _a === void 0 ? void 0 : _a._root) || null;
@@ -664,22 +676,24 @@ class TaroNode extends TaroEventTarget {
664
676
  // - cleanRef: false (No need to clean eventSource, because newChild is about to be inserted)
665
677
  // - update: true (Need to update parent.childNodes, because parent.childNodes is reordered)
666
678
  newChild.remove({ cleanRef: false });
679
+ let index = 0;
667
680
  // Data structure
668
681
  newChild.parentNode = this;
669
682
  if (refChild) {
670
683
  // insertBefore & replaceChild
671
- const index = this.findIndex(refChild);
684
+ index = this.findIndex(refChild);
672
685
  this.childNodes.splice(index, 0, newChild);
673
686
  }
674
687
  else {
675
688
  // appendChild
676
689
  this.childNodes.push(newChild);
677
690
  }
691
+ const childNodesLength = this.childNodes.length;
678
692
  // Serialization
679
693
  if (this._root) {
680
694
  if (!refChild) {
681
695
  // appendChild
682
- const isOnlyChild = this.childNodes.length === 1;
696
+ const isOnlyChild = childNodesLength === 1;
683
697
  if (isOnlyChild) {
684
698
  this.updateChildNodes();
685
699
  }
@@ -698,8 +712,26 @@ class TaroNode extends TaroEventTarget {
698
712
  });
699
713
  }
700
714
  else {
701
- // insertBefore
702
- this.updateChildNodes();
715
+ // insertBefore 有两种更新模式
716
+ // 比方说有 A B C 三个节点,现在要在 C 前插入 D
717
+ // 1. 插入 D,然后更新整个父节点的 childNodes 数组
718
+ // setData({ cn: [A, B, D, C] })
719
+ // 2. 插入 D,然后更新 D 以及 D 之后每个节点的数据
720
+ // setData ({
721
+ // cn.[2]: D,
722
+ // cn.[3]: C,
723
+ // })
724
+ // 由于微信解析 ’cn.[2]‘ 这些路径的时候也需要消耗时间,
725
+ // 所以根据 insertBefore 插入的位置来做不同的处理
726
+ const mark = childNodesLength * 2 / 3;
727
+ if (mark > index) {
728
+ // 如果 insertBefore 的位置在 childNodes 的 2/3 前,则为了避免解析路径消耗过多的时间,采用第一种方式
729
+ this.updateChildNodes();
730
+ }
731
+ else {
732
+ // 如果 insertBefore 的位置在 childNodes 的 2/3 之后,则采用第二种方式,避免 childNodes 的全量更新
733
+ this.updateSingleChild(index);
734
+ }
703
735
  }
704
736
  }
705
737
  MutationObserver.record({