batchloader
Version:
BatchLoader is a utility for data fetching layer to reduce requests via batching written in TypeScript. Inspired by Facebook's DataLoader
23 lines • 1.25 kB
TypeScript
import { CacheLoader, ICache } from './cacheloader';
import { MappedBatchLoader } from './mappedbatchloader';
import { IBatchLoader } from './types';
export declare type BatchLoadFn<Key, Value> = (keys: Key[]) => Value[] | Promise<Value[]>;
export declare type KeyToUniqueId<Key> = (key: Key) => string;
export declare class BatchLoader<Key, Value> implements IBatchLoader<Key, Value> {
protected batchFn: BatchLoadFn<Key, Value>;
protected keyToUniqueId: KeyToUniqueId<Key> | null;
protected batchDelay: number;
protected batchSize: number;
protected queuedKeys: Key[];
protected batchPromise: Promise<Value[]> | null;
constructor(batchFn: BatchLoadFn<Key, Value>, keyToUniqueId: KeyToUniqueId<Key> | null, batchDelay?: number, batchSize?: number);
load(key: Key): Promise<Value>;
loadMany(keys: Key[]): Promise<Value[]>;
mapLoader<MappedValue>(mapFn: (value: Value, key: Key) => MappedValue): MappedBatchLoader<Key, Value, MappedValue>;
cacheLoader(cache?: ICache<Key, Value>): CacheLoader<Key, Value>;
protected triggerBatch(): Promise<Value[]>;
protected runBatchNow(): Promise<Value[]>;
private maybeBatchInChunks;
private batchInChunks;
}
//# sourceMappingURL=batchloader.d.ts.map