优化获取可用节点时的逻辑 3x

This commit is contained in:
o.o.c 2025-01-19 10:04:36 +08:00
parent c771065674
commit c139707e62

View File

@ -296,6 +296,24 @@ class _scroll_view extends ScrollView {
} }
} }
class _yx_node_pool extends NodePool {
getAtIdx(indexPath: YXIndexPath, ...args: any[]): Node | null {
const nodes: Node[] = this['_pool']
for (let index = 0; index < nodes.length; index++) {
const obj = nodes[index];
let comp = obj.getComponent(_yx_node_element_comp)
if (comp && comp.attributes.indexPath.equals(indexPath)) {
nodes.splice(index, 1)
// @ts-ignore
const handler = this.poolHandlerComp ? obj.getComponent(this.poolHandlerComp) : null;
if (handler && handler.reuse) { handler.reuse(arguments); }
return obj
}
}
return null
}
}
/** /**
* *
*/ */
@ -641,7 +659,7 @@ export class YXCollectionView extends Component {
registerCell(identifier: string, maker: () => Node, poolComp: (new (...args: any[]) => YXCollectionViewCell) | string | null = null) { registerCell(identifier: string, maker: () => Node, poolComp: (new (...args: any[]) => YXCollectionViewCell) | string | null = null) {
let elementCategory: typeof YXLayoutAttributes.prototype.elementCategory = 'Cell' let elementCategory: typeof YXLayoutAttributes.prototype.elementCategory = 'Cell'
identifier = elementCategory + identifier identifier = elementCategory + identifier
let pool = new NodePool(poolComp) let pool = new _yx_node_pool(poolComp)
this.pools.set(identifier, pool) this.pools.set(identifier, pool)
this.makers.set(identifier, maker) this.makers.set(identifier, maker)
} }
@ -652,7 +670,7 @@ export class YXCollectionView extends Component {
registerSupplementary(identifier: string, maker: () => Node, poolComp: (new (...args: any[]) => YXCollectionViewCell) | string | null = null) { registerSupplementary(identifier: string, maker: () => Node, poolComp: (new (...args: any[]) => YXCollectionViewCell) | string | null = null) {
let elementCategory: typeof YXLayoutAttributes.prototype.elementCategory = 'Supplementary' let elementCategory: typeof YXLayoutAttributes.prototype.elementCategory = 'Supplementary'
identifier = elementCategory + identifier identifier = elementCategory + identifier
let pool = new NodePool(poolComp) let pool = new _yx_node_pool(poolComp)
this.pools.set(identifier, pool) this.pools.set(identifier, pool)
this.makers.set(identifier, maker) this.makers.set(identifier, maker)
} }
@ -670,19 +688,21 @@ export class YXCollectionView extends Component {
/** /**
* cell * cell
* @param identifier * @param identifier
* @param indexPath indexPath ()
*/ */
dequeueReusableCell(identifier: string): Node { dequeueReusableCell(identifier: string, indexPath: YXIndexPath = null): Node {
return this._dequeueReusableElement(identifier, 'Cell') return this._dequeueReusableElement(identifier, 'Cell', indexPath)
} }
/** /**
* supplementary * supplementary
* @param identifier * @param identifier
* @param indexPath indexPath ()
*/ */
dequeueReusableSupplementary(identifier: string): Node { dequeueReusableSupplementary(identifier: string, indexPath: YXIndexPath = null): Node {
return this._dequeueReusableElement(identifier, 'Supplementary') return this._dequeueReusableElement(identifier, 'Supplementary', indexPath)
} }
private _dequeueReusableElement(identifier: string, elementCategory: typeof YXLayoutAttributes.prototype.elementCategory) { private _dequeueReusableElement(identifier: string, elementCategory: typeof YXLayoutAttributes.prototype.elementCategory, indexPath: YXIndexPath = null) {
identifier = elementCategory + identifier identifier = elementCategory + identifier
let pool = this.pools.get(identifier) let pool = this.pools.get(identifier)
if (pool == null) { if (pool == null) {
@ -690,6 +710,11 @@ export class YXCollectionView extends Component {
} }
let result: Node = null let result: Node = null
// 尝试从重用池获取 (牺牲一点性能,尝试通过 indexPath 获取对应的节点,防止刷新闪烁的问题)
if (result == null && indexPath && pool instanceof _yx_node_pool) {
result = pool.getAtIdx(indexPath)
}
// 尝试从重用池获取 // 尝试从重用池获取
if (result == null) { if (result == null) {
result = pool.get() result = pool.get()