From f9ef762bc76ec803e25243d71ccae13cb08d7a40 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Fri, 13 Oct 2023 13:06:50 +0200 Subject: [PATCH] refactor(core): remove the at method from the LiveCollection type (#52227) The at operation is private and doesn't have to be part of the public interface. PR Close #52227 --- .../src/render3/instructions/control_flow.ts | 13 +++++---- .../core/src/render3/list_reconciliation.ts | 1 - .../test/render3/list_reconciliation_spec.ts | 28 ++++++++++--------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/packages/core/src/render3/instructions/control_flow.ts b/packages/core/src/render3/instructions/control_flow.ts index 896880354b8..571bac2de6b 100644 --- a/packages/core/src/render3/instructions/control_flow.ts +++ b/packages/core/src/render3/instructions/control_flow.ts @@ -169,11 +169,8 @@ class LiveCollectionLContainerImpl extends override get length(): number { return this.lContainer.length - CONTAINER_HEADER_OFFSET; } - override at(index: number): LView> { - return getExistingLViewFromLContainer(this.lContainer, index); - } override key(index: number): unknown { - return this.trackByFn(index, this.at(index)[CONTEXT].$implicit); + return this.trackByFn(index, this.getLView(index)[CONTEXT].$implicit); } override attach(index: number, lView: LView>): void { const dehydratedView = lView[HYDRATION] as DehydratedContainerView; @@ -198,16 +195,20 @@ class LiveCollectionLContainerImpl extends destroyLView(lView[TVIEW], lView); } override updateValue(index: number, value: unknown): void { - this.at(index)[CONTEXT].$implicit = value; + this.getLView(index)[CONTEXT].$implicit = value; } updateIndexes() { if (this.needsIndexUpdate) { for (let i = 0; i < this.length; i++) { - this.at(i)[CONTEXT].$index = i; + this.getLView(i)[CONTEXT].$index = i; } } } + + private getLView(index: number): LView> { + return getExistingLViewFromLContainer(this.lContainer, index); + } } /** diff --git a/packages/core/src/render3/list_reconciliation.ts b/packages/core/src/render3/list_reconciliation.ts index 4d62b5696ec..3c1579ede8c 100644 --- a/packages/core/src/render3/list_reconciliation.ts +++ b/packages/core/src/render3/list_reconciliation.ts @@ -15,7 +15,6 @@ import {TrackByFunction} from '../change_detection'; */ export abstract class LiveCollection { abstract get length(): number; - abstract at(index: number): T; abstract key(index: number): unknown; abstract attach(index: number, item: T): void; abstract detach(index: number): T; diff --git a/packages/core/test/render3/list_reconciliation_spec.ts b/packages/core/test/render3/list_reconciliation_spec.ts index 775eb591e79..10e4ae07506 100644 --- a/packages/core/test/render3/list_reconciliation_spec.ts +++ b/packages/core/test/render3/list_reconciliation_spec.ts @@ -44,27 +44,22 @@ class LoggingLiveCollection extends LiveCollection { get length(): number { return this.arr.length; } - at(index: number): T { - this.operations.at++; - const item = this.arr.at(index); - assertDefined(item, `Invalid index ${index} - item was undefined`); - return item; - } - key(index: number): unknown { + + override key(index: number): unknown { this.operations.key++; - return this.trackByFn(index, this.itemFactory.unwrap(this.at(index))); + return this.trackByFn(index, this.itemFactory.unwrap(this.getItem(index))); } - attach(index: number, item: T): void { + override attach(index: number, item: T): void { this.logs.push(['attach', index, item]); this.arr.splice(index, 0, item); } - detach(index: number): T { - const item = this.at(index); + override detach(index: number): T { + const item = this.getItem(index); this.logs.push(['detach', index, item]); this.arr.splice(index, 1); return item; } - create(index: number, value: V): T { + override create(index: number, value: V): T { this.logs.push(['create', index, value]); return this.itemFactory.create(index, value); } @@ -72,7 +67,7 @@ class LoggingLiveCollection extends LiveCollection { this.logs.push(['destroy', item]); } override updateValue(index: number, value: V): void { - this.itemFactory.update(this.at(index), index, value); + this.itemFactory.update(this.getItem(index), index, value); } getCollection() { @@ -86,6 +81,13 @@ class LoggingLiveCollection extends LiveCollection { clearLogs() { this.logs = []; } + + private getItem(index: number): T { + this.operations.at++; + const item = this.arr.at(index); + assertDefined(item, `Invalid index ${index} - item was undefined`); + return item; + } } function trackByIdentity(index: number, item: T) {