removeChild()

in packages/core/src/compositing/layer.ts [120:151]


  removeChild(child: Layer) {
    assert(child.parent === this)
    assert(child.attached === this.attached)
    assert(this.debugUltimatePrevSiblingOf(child, this.firstChild))
    assert(this.debugUltimateNextSiblingOf(child, this.lastChild))

    if (!child.prevSibling) {
      // child 是第一个子节点
      assert(this.firstChild === child)
      this.firstChild = child.nextSibling
    } else {
      child.prevSibling.nextSibling = child.nextSibling
    }

    if (!child.nextSibling) {
      // child 是最后一个子节点
      assert(this.lastChild === child)
      this.lastChild = child.prevSibling
    } else {
      child.nextSibling.prevSibling = child.prevSibling
    }

    assert((!this.firstChild) === (!this.lastChild), '如果移除了最后一个子节点,则 firstChild 和 lastChild 都为 undefined,否则 firstChild 和 lastChild 都不能为 undefined')
    assert(!this.firstChild || this.firstChild.attached === this.attached)
    assert(!this.lastChild || this.lastChild.attached === this.attached)
    assert(!this.firstChild || this.debugUltimateNextSiblingOf(this.firstChild, this.lastChild), 'firstChild 和 lastChild 不能是循环兄弟节点')
    assert(!this.lastChild || this.debugUltimatePrevSiblingOf(this.lastChild, this.firstChild), 'lastChild 和 firstChild 不能是循环兄弟节点')
    child.prevSibling = undefined
    child.nextSibling = undefined
    this.dropChild(child)
    assert(!child.attached)
  }