vscroll
Version:
Virtual scroll engine
95 lines (94 loc) • 3.88 kB
TypeScript
import { Item } from '../item';
import { Settings } from '../settings';
import { Logger } from '../logger';
import { SizeStrategy, Direction } from '../../inputs/index';
interface ItemToCache<Data> {
$index: number;
data: Data;
size?: number;
}
interface ItemUpdate {
$index: number;
size: number;
toRemove?: boolean;
}
export declare class ItemCache<Data = unknown> {
$index: number;
data: Data | null;
size?: number;
position: number;
constructor(item: ItemToCache<Data>, saveData: boolean);
changeIndex(value: number): void;
}
export declare class Cache<Data = unknown> {
minIndex: number;
maxIndex: number;
readonly itemSize: number;
readonly saveData: boolean;
readonly cacheOnReload: boolean;
readonly sizeStrategy: SizeStrategy;
readonly logger: Logger;
private items;
private defaultSize;
constructor({ itemSize, cacheData, cacheOnReload, sizeStrategy }: Settings, logger: Logger);
reset(force: boolean): void;
get size(): number;
get(index: number): ItemCache<Data> | undefined;
getSizeByIndex(index: number): number;
getDefaultSize(): number;
recalculateDefaultSize(): boolean;
/**
* Adds item to Set by $index, replaces existed item if $index matches.
* Maintains min/max indexes and default item size.
*
* @param {Item<Data>} item A Buffer item to be cached, an objects with { $index, data, size } props.
* @returns {ItemCache<Data>} Cached item.
*/
add(item: Item<Data>): ItemCache<Data>;
/**
* Inserts items to Set, shifts $indexes of items that remain.
* Replaces current Set with a new one with new regular $indexes.
* Maintains min/max indexes.
*
* @param {Data[]} toInsert List of non-indexed items to be inserted.
* @param {number} index The index before/after which the insertion is performed.
* @param {Direction} direction Determines the direction of insertion.
* @param {boolean} fixRight Defines indexes shifting strategy.
* If false, indexes that are greater than the inserted ones are increased.
* If true, indexes that are less than than the inserted ones are decreased.
*/
insertItems(toInsert: Data[], index: number, direction: Direction, fixRight: boolean): void;
/**
* Removes items from Set, shifts $indexes of items that remain.
* Replaces current Set with a new one with new regular $indexes.
* Maintains min/max indexes and default item size.
*
* @param {number[]} toRemove List of indexes to be removed.
* @param {boolean} fixRight Defines indexes shifting strategy.
* If false, indexes that are greater than the removed ones will be decreased.
* If true, indexes that are less than than the removed ones will be increased.
*/
removeItems(toRemove: number[], fixRight: boolean): void;
/**
* Destructively updates Set based on subset (before-after) changes.
* Replaces current Set with a new one with new regular $indexes.
* Maintains min/max indexes. Maintains default item size on remove only.
*
* @param {ItemUpdate[]} before Initial subset of items to be replaced by "after".
* Each element is an object with { $index, size, toRemove } props. Must be $index-incremental.
* Items to be removed must have toRemove flag: before[].toRemove = true.
* @param {Item<Data>[]} after Transformed subset that replaces "before". Must be $index-incremental.
* Must contain at least 1 $index from "before" or be empty.
* @param {boolean} fixRight This is to fix right indexes during subset collapsing. Acts only if "after" is empty.
*/
updateSubset(before: ItemUpdate[], after: Item<Data>[], fixRight?: boolean): void;
/**
* Shifts all indexes by some value.
* Replaces current Set with a new one with new regular $indexes.
* Maintains min/max indexes.
*
* @param {number} delta A shift value.
*/
shiftIndexes(delta: number): void;
}
export {};