core: simplify item key derivation in virtualized grouping

This commit is contained in:
Abdullah Atta
2023-12-11 11:01:04 +05:00
committed by Abdullah Atta
parent 05ccfcd9fb
commit 321bd2aa93

View File

@@ -49,18 +49,7 @@ export class VirtualizedGrouping<T> {
}
getKey(index: number) {
const batchIndex = Math.floor(index / this.batchSize);
const batch = this.cache.get(batchIndex);
if (!batch) return `${index}`;
const { items, groups } = batch;
const itemIndexInBatch = index - batchIndex * this.batchSize;
const group = groups?.find(
(f) => f.index === itemIndexInBatch && !f.hidden
);
return group
? group.group.id
: (items[itemIndexInBatch] as any)?.id || `${index}`;
return `${index}`;
}
item(index: number): Promise<{ item: T; group?: GroupHeader }>;
@@ -147,18 +136,10 @@ export class VirtualizedGrouping<T> {
private loadBatch(batch: number, operate?: BatchOperator<T>) {
if (this.pending.has(batch)) return this.pending.get(batch)!;
if (!this.isLastBatch(batch)) clearTimeout(this.loadBatchTimeout);
return new Promise<Batch<T>>((resolve, reject) => {
this.loadBatchTimeout = setTimeout(() => {
const promise = this.load(batch, operate);
this.pending.set(batch, promise);
return promise
.then(resolve)
.catch(reject)
.finally(() => {
this.pending.delete(batch);
});
}, 16) as unknown as number;
const promise = this.load(batch, operate);
this.pending.set(batch, promise);
return promise.finally(() => {
this.pending.delete(batch);
});
}