@tarojs/runtime 3.6.9-alpha.12 → 3.6.9-alpha.13

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.
@@ -18,6 +18,7 @@ export declare class TaroNode extends TaroEventTarget {
18
18
  constructor();
19
19
  private hydrate;
20
20
  private updateChildNodes;
21
+ private updateSingleChild;
21
22
  get _root(): TaroRootElement | null;
22
23
  protected findIndex(refChild: TaroNode): number;
23
24
  get _path(): string;
@@ -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({