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
This commit is contained in:
Pawel Kozlowski
2023-10-13 13:06:50 +02:00
parent e87ac245e0
commit f9ef762bc7
3 changed files with 22 additions and 20 deletions
@@ -169,11 +169,8 @@ class LiveCollectionLContainerImpl extends
override get length(): number {
return this.lContainer.length - CONTAINER_HEADER_OFFSET;
}
override at(index: number): LView<RepeaterContext<unknown>> {
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<RepeaterContext<unknown>>): 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<RepeaterContext<unknown>> {
return getExistingLViewFromLContainer(this.lContainer, index);
}
}
/**
@@ -15,7 +15,6 @@ import {TrackByFunction} from '../change_detection';
*/
export abstract class LiveCollection<T, V> {
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;
@@ -44,27 +44,22 @@ class LoggingLiveCollection<T, V> extends LiveCollection<T, V> {
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<T, V> extends LiveCollection<T, V> {
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<T, V> extends LiveCollection<T, V> {
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<T>(index: number, item: T) {