react-suspense-fetch
Version:
A low-level library for React Suspense for Data Fetching
36 lines (35 loc) • 1.42 kB
TypeScript
declare type FetchFunc<Result, Input> = (input: Input, options: {
signal: AbortSignal;
}) => Promise<Result>;
declare type GetOptions = {
forcePrefetch?: boolean;
};
/**
* fetch store
*
* `prefetch` will start fetching.
* `get` will return a result or throw a promise when a result is not ready.
* `preset` will set a result without fetching.
* `evict` will remove a result.
* `abort` will cancel fetching.
*
* There are three cache types:
* - WeakMap: `input` has to be an object in this case
* - Map: you need to call evict to remove from cache
* - Map with areEqual: you can specify a custom comparator
*/
export declare type FetchStore<Result, Input> = {
prefetch: (input: Input) => void;
get: (input: Input, option?: GetOptions) => Result;
preset: (input: Input, result: Result) => void;
evict: (input: Input) => void;
abort: (input: Input) => void;
};
export declare function createFetchStore<Result, Input extends object>(fetchFunc: FetchFunc<Result, Input>, cacheType: {
type: 'WeakMap';
}, presets?: Iterable<readonly [Input, Result]>): FetchStore<Result, Input>;
export declare function createFetchStore<Result, Input>(fetchFunc: FetchFunc<Result, Input>, cacheType?: {
type: 'Map';
areEqual?: ((a: Input, b: Input) => boolean);
}, presets?: Iterable<readonly [Input, Result]>): FetchStore<Result, Input>;
export {};