import { STATUS_ABORTED, STATUS_NOT_FOUND, STATUS_PENDING, STATUS_REJECTED, STATUS_RESOLVED, } from "./constants"; export type StatusNotFound = typeof STATUS_NOT_FOUND; export type StatusPending = typeof STATUS_PENDING; export type StatusAborted = typeof STATUS_ABORTED; export type StatusRejected = typeof STATUS_REJECTED; export type StatusResolved = typeof STATUS_RESOLVED; export type Status = | StatusNotFound | StatusPending | StatusAborted | StatusRejected | StatusResolved; export type PendingRecordData = { readonly abortController: AbortController; readonly deferred: Deferred; readonly status: StatusPending; }; export type ResolvedRecordData = { readonly metadata: unknown; readonly status: StatusResolved; readonly value: Type; }; export type RejectedRecordData = { readonly error: any; readonly status: StatusRejected; }; export type PendingRecord = { data: PendingRecordData; }; export type ResolvedRecord = { data: ResolvedRecordData; }; export type RejectedRecord = { data: RejectedRecordData; }; export type Record = | PendingRecord | ResolvedRecord | RejectedRecord; export type RecordData = | PendingRecordData | ResolvedRecordData | RejectedRecordData; export interface SubscriptionDataNotFound { readonly status: StatusNotFound; } export interface SubscriptionDataPending { readonly status: StatusPending; } export interface SubscriptionDataAborted { readonly status: StatusAborted; } export interface SubscriptionDataRejected { readonly error: any; readonly status: StatusRejected; } export interface SubscriptionDataResolved { readonly status: StatusResolved; readonly value: Type; } export type SubscriptionData = | SubscriptionDataNotFound | SubscriptionDataPending | SubscriptionDataAborted | SubscriptionDataRejected | SubscriptionDataResolved; export type StreamingSubscribeCallback = () => void; export type SubscriptionCallback = ( data: SubscriptionData ) => void; export type UnsubscribeCallback = () => void; // Convenience type used by Suspense caches. // Adds the ability to resolve or reject a pending PromiseLike. export interface Deferred { debugLabel: string | undefined; promise: Promise; reject(error: any): void; resolve(value?: Type): void; status: StatusPending | StatusRejected | StatusResolved; } // Cache types export interface Cache { abort(...params: Params): boolean; cache(value: Value, ...params: Params): void; disableDebugLogging(): void; enableDebugLogging(): void; evict(...params: Params): boolean; evictAll(): void; getStatus(...params: Params): Status; getValue(...params: Params): Value; getValueIfCached(...params: Params): Value | undefined; prefetch(...params: Params): void; read(...params: Params): Value; readAsync(...params: Params): PromiseLike | Value; subscribe( callback: SubscriptionCallback, ...params: Params ): UnsubscribeCallback; } export type CacheLoadOptions = { signal: AbortSignal; }; // Interval cache types export type IntervalCache = { abort(...params: Params): boolean; disableDebugLogging(): void; enableDebugLogging(): void; evict(...params: Params): boolean; evictAll(): boolean; getStatus(start: Point, end: Point, ...params: Params): Status; getValue(start: Point, end: Point, ...params: Params): Value[]; getValueIfCached( start: Point, end: Point, ...params: Params ): Value[] | undefined; readAsync( start: Point, end: Point, ...params: Params ): PromiseLike | Value[]; isPartialResult: (value: Value[]) => boolean; read(start: Point, end: Point, ...params: Params): Value[]; subscribe( callback: SubscriptionCallback, start: Point, end: Point, ...params: Params ): UnsubscribeCallback; }; export type IntervalCacheLoadOptions = { returnAsPartial: (values: Value[]) => Value[]; signal: AbortSignal; }; export type ComparisonFunction = (a: Type, b: Type) => number; export type GetPointForValue = (value: Value) => Point; // Streaming cache types export interface StreamingValue { complete: boolean; data: AdditionalData | undefined; error: any | undefined; progress: number | undefined; resolver: PromiseLike>; status: Status; subscribe(callback: StreamingSubscribeCallback): UnsubscribeCallback; value: Value | undefined; } export interface StreamingCacheLoadOptions { update: (value: Value, progress?: number, data?: AdditionalData) => void; resolve: () => void; reject: (error: any) => void; signal: AbortSignal; } export interface StreamingCache< Params extends any[], Value, AdditionalData = undefined > { abort(...params: Params): boolean; disableDebugLogging(): void; enableDebugLogging(): void; evict(...params: Params): boolean; evictAll(): boolean; prefetch(...params: Params): void; read( ...params: Params ): Pick, "data" | "value">; readAsync( ...params: Params ): PromiseLike, "data" | "value">>; stream(...params: Params): StreamingValue; } export interface CacheMap { clear(): void; delete(key: Key): boolean; get(key: Key): Value | undefined; has(key: Key): boolean; set(key: Key, value: Value): this; } // Externally managed cache variant export type ExternallyManagedCache = Omit< Cache, "cache" > & { cacheError(error: any, ...params: Params): void; cacheValue(value: Value, ...params: Params): void; }; // Hook types export type ImperativeErrorResponse = { error: any; status: StatusRejected; value: any; }; export type ImperativePendingResponse = { error: undefined; status: StatusPending; value: Value | undefined; }; export type ImperativeResolvedResponse = { error: undefined; status: StatusResolved; value: Value; }; export type ImperativeIntervalErrorResponse = ImperativeErrorResponse; export type ImperativeIntervalPendingResponse = ImperativePendingResponse; export type ImperativeIntervalResolvedResponse = ImperativeResolvedResponse & { isPartialResult: boolean; };