layered-loader
Version:
Data loader with support for caching and fallback data sources
30 lines (29 loc) • 2.5 kB
TypeScript
import type { CommonCacheConfig } from './AbstractCache';
import { AbstractFlatCache } from './AbstractFlatCache';
import type { InMemoryCacheConfiguration } from './memory/InMemoryCache';
import type { InMemoryGroupCacheConfiguration } from './memory/InMemoryGroupCache';
import type { GroupNotificationPublisher } from './notifications/GroupNotificationPublisher';
import type { NotificationPublisher } from './notifications/NotificationPublisher';
import type { Cache, DataSource, GroupCache } from './types/DataSources';
import type { GetManyResult, SynchronousCache, SynchronousGroupCache } from './types/SyncDataSources';
export type LoaderConfig<LoadedValue, LoadParams = string, CacheType extends Cache<LoadedValue> | GroupCache<LoadedValue> = Cache<LoadedValue>, DataSourceType = DataSource<LoadedValue, LoadParams>, InMemoryCacheConfigType extends InMemoryCacheConfiguration | InMemoryGroupCacheConfiguration = InMemoryCacheConfiguration, InMemoryCacheType extends SynchronousCache<LoadedValue> | SynchronousGroupCache<LoadedValue> = SynchronousCache<LoadedValue>, NotificationPublisherType extends NotificationPublisher<LoadedValue> | GroupNotificationPublisher<LoadedValue> = NotificationPublisher<LoadedValue>> = {
dataSources?: readonly DataSourceType[];
dataSourceGetOneFn?: (loadParams: LoadParams) => Promise<LoadedValue | undefined | null>;
dataSourceGetManyFn?: (keys: string[], loadParams?: LoadParams) => Promise<LoadedValue[]>;
dataSourceName?: string;
throwIfLoadError?: boolean;
throwIfUnresolved?: boolean;
} & CommonCacheConfig<LoadedValue, CacheType, InMemoryCacheConfigType, InMemoryCacheType, NotificationPublisherType, LoadParams>;
export declare class Loader<LoadedValue, LoadParams = string> extends AbstractFlatCache<LoadedValue, LoadParams> {
private readonly dataSources;
private readonly isKeyRefreshing;
protected readonly throwIfLoadError: boolean;
protected readonly throwIfUnresolved: boolean;
constructor(config: LoaderConfig<LoadedValue, LoadParams, Cache<LoadedValue>>);
forceSetValue(key: string, newValue: LoadedValue | null): Promise<void>;
forceRefresh(loadParams: LoadParams): Promise<LoadedValue | undefined | null>;
protected resolveValue(key: string, loadParams: LoadParams): Promise<LoadedValue | undefined | null>;
private loadFromLoaders;
protected resolveManyValues(keys: string[], loadParams: LoadParams): Promise<GetManyResult<LoadedValue>>;
private loadManyFromLoaders;
}