import { ErrorTypes, SnapshotInterface, Schema, Denormalize, Queryable, SchemaArgs } from '@data-client/normalizr'; import { ExpiryStatus, EndpointInterface, FetchFunction, ResolveType, DenormalizeNullable, MemoCache } from '@data-client/normalizr'; import AbortOptimistic from './AbortOptimistic.js'; import { EndpointUpdateFunction } from './types.js'; import { ReduxMiddlewareAPI } from '../manager/applyManager.js'; import { GCInterface } from '../state/GCPolicy.js'; import { ActionTypes, State } from '../types.js'; export type GenericDispatch = (value: any) => Promise; export type DataClientDispatch = (value: ActionTypes) => Promise; export interface ControllerConstructorProps { dispatch?: D; getState?: () => State; memo?: Pick; gcPolicy?: GCInterface; } /** * Imperative control of Reactive Data Client store * @see https://dataclient.io/docs/api/Controller */ export default class Controller { /** * Dispatches an action to Reactive Data Client reducer. * * @see https://dataclient.io/docs/api/Controller#dispatch */ protected _dispatch: D; /** * Gets the latest state snapshot that is fully committed. * * This can be useful for imperative use-cases like event handlers. * This should *not* be used to render; instead useSuspense() or useCache() * @see https://dataclient.io/docs/api/Controller#getState */ getState: () => State; /** * Singleton to maintain referential equality between calls */ readonly memo: Pick; /** * Handles garbage collection */ readonly gcPolicy: GCInterface; constructor({ dispatch, getState, memo, gcPolicy, }?: ControllerConstructorProps); dispatch: D; bindMiddleware({ dispatch, getState, }: { dispatch: D; getState: ReduxMiddlewareAPI['getState']; }): void; /*************** Action Dispatchers ***************/ /** * Fetches the endpoint with given args, updating the Reactive Data Client cache with the response or error upon completion. * @see https://dataclient.io/docs/api/Controller#fetch */ fetch: ; }>(endpoint: E, ...args: readonly [ ...Parameters ]) => E["schema"] extends undefined | null ? ReturnType : Promise>; /** * Fetches only if endpoint is considered 'stale'; otherwise returns undefined * @see https://dataclient.io/docs/api/Controller#fetchIfStale */ fetchIfStale: ; }>(endpoint: E, ...args: readonly [ ...Parameters ]) => E["schema"] extends undefined | null ? ReturnType | ResolveType : Promise> | Denormalize; /** * Forces refetching and suspense on useSuspense with the same Endpoint and parameters. * @see https://dataclient.io/docs/api/Controller#invalidate */ invalidate: (endpoint: E, ...args: readonly [ ...Parameters ] | readonly [ null ]) => Promise; /** * Forces refetching and suspense on useSuspense on all matching endpoint result keys. * @see https://dataclient.io/docs/api/Controller#invalidateAll * @returns Promise that resolves when invalidation is commited. */ invalidateAll: (options: { testKey: (key: string) => boolean; }) => Promise; /** * Sets all matching endpoint result keys to be STALE. * @see https://dataclient.io/docs/api/Controller#expireAll * @returns Promise that resolves when expiry is commited. *NOT* fetch promise */ expireAll: (options: { testKey: (key: string) => boolean; }) => Promise; /** * Resets the entire Reactive Data Client cache. All inflight requests will not resolve. * @see https://dataclient.io/docs/api/Controller#resetEntireStore */ resetEntireStore: () => Promise; /** * Sets value for the Queryable and args. * @see https://dataclient.io/docs/api/Controller#set */ set(schema: S, ...rest: readonly [ ...SchemaArgs, (previousValue: Denormalize) => {} ]): Promise; set(schema: S, ...rest: readonly [ ...SchemaArgs, {} ]): Promise; /** * Sets response for the Endpoint and args. * @see https://dataclient.io/docs/api/Controller#setResponse */ setResponse: ; }>(endpoint: E, ...rest: readonly [ ...Parameters, any ]) => Promise; /** * Sets an error response for the Endpoint and args. * @see https://dataclient.io/docs/api/Controller#setError */ setError: ; }>(endpoint: E, ...rest: readonly [ ...Parameters, Error ]) => Promise; /** * Resolves an inflight fetch. * @see https://dataclient.io/docs/api/Controller#resolve */ resolve: ; }>(endpoint: E, meta: { args: readonly [ ...Parameters ]; response: Error; fetchedAt: number; error: true; } | { args: readonly [ ...Parameters ]; response: any; fetchedAt: number; error?: false | undefined; }) => Promise; /** * Marks a new subscription to a given Endpoint. * @see https://dataclient.io/docs/api/Controller#subscribe */ subscribe: >(endpoint: E, ...args: readonly [ ...Parameters ] | readonly [ null ]) => Promise; /** * Marks completion of subscription to a given Endpoint. * @see https://dataclient.io/docs/api/Controller#unsubscribe */ unsubscribe: >(endpoint: E, ...args: readonly [ ...Parameters ] | readonly [ null ]) => Promise; /*************** More ***************/ /** * Gets a snapshot (https://dataclient.io/docs/api/Snapshot) * @see https://dataclient.io/docs/api/Controller#snapshot */ snapshot: (state: State, fetchedAt?: number) => Snapshot; /** * Gets the error, if any, for a given endpoint. Returns undefined for no errors. * @see https://dataclient.io/docs/api/Controller#getError */ getError(endpoint: E, ...rest: readonly [ null, State ] | readonly [ ...Parameters, State ]): ErrorTypes | undefined; getError>(endpoint: E, ...rest: readonly [ null, State ] | readonly [ ...Parameters, State ]): ErrorTypes | undefined; /** * Gets the (globally referentially stable) response for a given endpoint/args pair from state given. * @see https://dataclient.io/docs/api/Controller#getResponse */ getResponse(endpoint: E, ...rest: readonly [ null, State ] | readonly [ ...Parameters, State ]): { data: DenormalizeNullable; expiryStatus: ExpiryStatus; expiresAt: number; countRef: () => () => void; }; getResponse>(endpoint: E, ...rest: readonly [ ...(readonly [ ...Parameters ] | readonly [ null ]), State ]): { data: DenormalizeNullable; expiryStatus: ExpiryStatus; expiresAt: number; countRef: () => () => void; }; /** * Gets the (globally referentially stable) response for a given endpoint/args pair from state given. * @see https://dataclient.io/docs/api/Controller#getResponseMeta */ getResponseMeta(endpoint: E, ...rest: readonly [ null, State ] | readonly [ ...Parameters, State ]): { data: DenormalizeNullable; expiryStatus: ExpiryStatus; expiresAt: number; countRef: () => () => void; }; getResponseMeta>(endpoint: E, ...rest: readonly [ ...(readonly [ ...Parameters ] | readonly [ null ]), State ]): { data: DenormalizeNullable; expiryStatus: ExpiryStatus; expiresAt: number; countRef: () => () => void; }; /** * Queries the store for a Querable schema * @see https://dataclient.io/docs/api/Controller#get */ get(schema: S, ...rest: readonly [ ...SchemaArgs, Pick, 'entities' | 'indexes'> ]): DenormalizeNullable | undefined; /** * Queries the store for a Querable schema; providing related metadata * @see https://dataclient.io/docs/api/Controller#getQueryMeta */ getQueryMeta(schema: S, ...rest: readonly [ ...SchemaArgs, Pick, 'entities' | 'indexes'> ]): { data: DenormalizeNullable | undefined; countRef: () => () => void; }; private getExpiryStatus; } export { ErrorTypes }; declare class Snapshot implements SnapshotInterface { static readonly abort: AbortOptimistic; private state; private controller; readonly fetchedAt: number; readonly abort: AbortOptimistic; constructor(controller: Controller, state: State, fetchedAt?: number); /*************** Data Access ***************/ /** @see https://dataclient.io/docs/api/Snapshot#getResponse */ getResponse(endpoint: E, ...args: readonly [ null ]): { data: DenormalizeNullable; expiryStatus: ExpiryStatus; expiresAt: number; }; getResponse(endpoint: E, ...args: readonly [ ...Parameters ]): { data: DenormalizeNullable; expiryStatus: ExpiryStatus; expiresAt: number; }; getResponse>(endpoint: E, ...args: readonly [ ...Parameters ] | readonly [ null ]): { data: DenormalizeNullable; expiryStatus: ExpiryStatus; expiresAt: number; }; /** @see https://dataclient.io/docs/api/Snapshot#getResponseMeta */ getResponseMeta(endpoint: E, ...args: readonly [ null ]): { data: DenormalizeNullable; expiryStatus: ExpiryStatus; expiresAt: number; }; getResponseMeta(endpoint: E, ...args: readonly [ ...Parameters ]): { data: DenormalizeNullable; expiryStatus: ExpiryStatus; expiresAt: number; }; getResponseMeta>(endpoint: E, ...args: readonly [ ...Parameters ] | readonly [ null ]): { data: DenormalizeNullable; expiryStatus: ExpiryStatus; expiresAt: number; }; /** @see https://dataclient.io/docs/api/Snapshot#getError */ getError(endpoint: E, ...args: readonly [ ...Parameters ] | readonly [ null ]): ErrorTypes | undefined; getError>(endpoint: E, ...args: readonly [ ...Parameters ] | readonly [ null ]): ErrorTypes | undefined; /** * Retrieved memoized value for any Querable schema * @see https://dataclient.io/docs/api/Snapshot#get */ get(schema: S, ...args: SchemaArgs): DenormalizeNullable | undefined; /** * Queries the store for a Querable schema; providing related metadata * @see https://dataclient.io/docs/api/Snapshot#getQueryMeta */ getQueryMeta(schema: S, ...args: SchemaArgs): { data: DenormalizeNullable | undefined; countRef: () => () => void; }; } //# sourceMappingURL=Controller.d.ts.map