UNPKG

@data-client/core

Version:

Async State Management without the Management. REST, GraphQL, SSE, Websockets, Fetch

77 lines (76 loc) 3.12 kB
import Controller from '../controller/Controller.js'; import { FetchAction, Manager, ActionTypes, Middleware, SetResponseAction } from '../types.js'; export declare class ResetError extends Error { name: string; constructor(); } export interface FetchingMeta { promise: Promise<any>; resolve: (value?: any) => void; reject: (value?: any) => void; fetchedAt: number; } /** Handles all async network dispatches * * Dedupes concurrent requests by keeping track of all fetches in flight * and returning existing promises for requests already in flight. * * Interfaces with store via a redux-compatible middleware. * * @see https://dataclient.io/docs/api/NetworkManager */ export default class NetworkManager implements Manager { protected fetching: Map<string, FetchingMeta>; readonly dataExpiryLength: number; readonly errorExpiryLength: number; protected controller: Controller; cleanupDate?: number; constructor({ dataExpiryLength, errorExpiryLength }?: { dataExpiryLength?: number | undefined; errorExpiryLength?: number | undefined; }); middleware: Middleware; /** On mount */ init(): void; /** Ensures all promises are completed by rejecting remaining. */ cleanup(): void; /** Used by DevtoolsManager to determine whether to log an action */ skipLogging(action: ActionTypes): boolean; allSettled(): Promise<PromiseSettledResult<any>[]> | undefined; /** Clear all promise state */ protected clearAll(): void; /** Clear promise state for a given key */ protected clear(key: string): void; protected getLastReset(): number; /** Called when middleware intercepts 'rdc/fetch' action. * * Will then start a promise for a key and potentially start the network * fetch. * * Uses throttle endpoints without sideEffects. This is valuable * for ensures mutation requests always go through. */ protected handleFetch(action: FetchAction): Promise<any>; /** Called when middleware intercepts a set action. * * Will resolve the promise associated with set key. */ protected handleSet(action: SetResponseAction): void; /** Ensures only one request for a given key is in flight at any time * * Uses key to either retrieve in-flight promise, or if not * create a new promise and call fetch. * * Note: The new promise is not actually tied to fetch at all, * but is resolved when the expected 'receive' action is processed. * This ensures promises are resolved only once their data is processed * by the reducer. */ protected throttle(key: string, fetch: () => Promise<any>, fetchedAt: number): Promise<any>; /** Calls the callback when client is not 'busy' with high priority interaction tasks * * Override for platform-specific implementations */ protected idleCallback(callback: (...args: any[]) => void, options?: IdleRequestOptions): void; } //# sourceMappingURL=NetworkManager.d.ts.map