UNPKG

layered-loader

Version:

Data loader with support for caching and fallback data sources

48 lines (47 loc) 3.82 kB
import type { InMemoryCacheConfiguration } from './memory/InMemoryCache'; import type { InMemoryGroupCacheConfiguration } from './memory/InMemoryGroupCache'; import type { AbstractNotificationConsumer } from './notifications/AbstractNotificationConsumer'; import type { GroupNotificationPublisher } from './notifications/GroupNotificationPublisher'; import type { NotificationPublisher } from './notifications/NotificationPublisher'; import type { Cache, GroupCache } from './types/DataSources'; import type { SynchronousCache, SynchronousGroupCache } from './types/SyncDataSources'; import type { Logger } from './util/Logger'; export type LoaderErrorHandler = (err: Error, key: string | undefined, loader: Record<string, any>, logger: Logger) => void; export declare const DEFAULT_LOAD_ERROR_HANDLER: LoaderErrorHandler; export declare const DEFAULT_CACHE_ERROR_HANDLER: LoaderErrorHandler; export type CacheKeyResolver<SourceData> = (sourceData: SourceData) => string; export type IdHolder = { id: string; }; export declare const DEFAULT_FROM_STRING_RESOLVER: CacheKeyResolver<string>; export declare const DEFAULT_FROM_ID_RESOLVER: CacheKeyResolver<IdHolder>; export declare const DEFAULT_UNDEFINED_FROM_VALUE_RESOLVER: CacheKeyResolver<unknown>; export type CommonCacheConfig<LoadedValue, CacheType extends Cache<LoadedValue> | GroupCache<LoadedValue> = Cache<LoadedValue>, InMemoryCacheConfigType extends InMemoryCacheConfiguration | InMemoryGroupCacheConfiguration = InMemoryCacheConfiguration, InMemoryCacheType extends SynchronousCache<LoadedValue> | SynchronousGroupCache<LoadedValue> = SynchronousCache<LoadedValue>, NotificationPublisherType extends NotificationPublisher<LoadedValue> | GroupNotificationPublisher<LoadedValue> = NotificationPublisher<LoadedValue>, LoadParams = string> = { logger?: Logger; cacheUpdateErrorHandler?: LoaderErrorHandler; loadErrorHandler?: LoaderErrorHandler; inMemoryCache?: InMemoryCacheConfigType | false; asyncCache?: CacheType; notificationConsumer?: AbstractNotificationConsumer<LoadedValue, InMemoryCacheType>; notificationPublisher?: NotificationPublisherType; cacheKeyFromLoadParamsResolver?: CacheKeyResolver<LoadParams>; cacheKeyFromValueResolver?: CacheKeyResolver<LoadedValue>; }; export declare abstract class AbstractCache<LoadedValue, LoadChildType = Promise<LoadedValue | undefined | null> | undefined, CacheType extends Cache<LoadedValue> | GroupCache<LoadedValue> = Cache<LoadedValue>, InMemoryCacheType extends SynchronousCache<LoadedValue> | SynchronousGroupCache<LoadedValue> = SynchronousCache<LoadedValue>, InMemoryCacheConfigType extends InMemoryCacheConfiguration | InMemoryGroupCacheConfiguration = InMemoryCacheConfiguration, NotificationPublisherType extends NotificationPublisher<LoadedValue> | GroupNotificationPublisher<LoadedValue> = NotificationPublisher<LoadedValue>, LoadParams = string> { protected readonly inMemoryCache: InMemoryCacheType; protected readonly asyncCache?: CacheType; readonly cacheKeyFromLoadParamsResolver: CacheKeyResolver<LoadParams>; readonly cacheKeyFromValueResolver: CacheKeyResolver<LoadedValue>; protected readonly logger: Logger; protected readonly cacheUpdateErrorHandler: LoaderErrorHandler; protected readonly loadErrorHandler: LoaderErrorHandler; protected readonly runningLoads: Map<string, LoadChildType>; private readonly notificationConsumer?; protected readonly notificationPublisher?: NotificationPublisherType; abstract isGroupCache(): boolean; private initPromises; constructor(config: CommonCacheConfig<LoadedValue, CacheType, InMemoryCacheConfigType, InMemoryCacheType, NotificationPublisherType, LoadParams>); init(): Promise<void>; invalidateCache(): Promise<void>; close(): Promise<void>; }