@saberhq/sail
Version:
Account caching and batched loading for React-based Solana applications.
99 lines • 3.45 kB
TypeScript
/// <reference types="node" />
import type { AccountInfoFetcher, Provider } from "@saberhq/solana-contrib";
import type { AccountInfo } from "@solana/web3.js";
import { PublicKey } from "@solana/web3.js";
import DataLoader from "dataloader";
import type { AccountFetchResult, SailError } from "..";
import type { AccountDatum } from "../types";
import type { CacheBatchUpdateEvent } from "./emitter";
/**
* Gets the cache key associated with the given pubkey.
* @param pubkey
* @returns
*/
export declare const getCacheKeyOfPublicKey: (pubkey: PublicKey) => string;
export declare type AccountLoader = DataLoader<PublicKey, AccountInfo<Buffer> | null, string>;
export interface UseAccountsArgs {
/**
* Duration in ms in which to batch all accounts data requests. Defaults to 500ms.
*/
batchDurationMs?: number;
/**
* Milliseconds between each refresh. Defaults to 60_000.
*/
refreshIntervalMs?: number;
/**
* Called whenever an error occurs.
*/
onError: (err: SailError) => void;
/**
* If true, allows one to subscribe to account updates via websockets rather than via polling.
*/
useWebsocketAccountUpdates?: boolean;
/**
* If true, disables periodic account refetches for subscriptions.
*/
disableAutoRefresh?: boolean;
}
/**
* Function signature for fetching keys.
*/
export declare type FetchKeysFn = (keys: readonly PublicKey[]) => Promise<readonly AccountFetchResult[]>;
/**
* Fetches keys, passing through null/undefined values.
* @param fetchKeys
* @param keys
* @returns
*/
export declare const fetchKeysMaybe: (fetchKeys: FetchKeysFn, keys: readonly (PublicKey | null | undefined)[]) => Promise<readonly (AccountFetchResult | null | undefined)[]>;
export interface UseAccounts extends Required<UseAccountsArgs> {
/**
* The loader. Usually should not be used directly.
*/
loader: AccountLoader;
/**
* Refetches an account.
*/
refetch: (key: PublicKey) => Promise<AccountInfo<Buffer> | null>;
/**
* Refetches multiple accounts.
*/
refetchMany: (keys: readonly PublicKey[]) => Promise<(AccountInfo<Buffer> | Error | null)[]>;
/**
* Refetches all accounts that are being subscribed to.
*/
refetchAllSubscriptions: () => Promise<void>;
/**
* Registers a callback to be called whenever a batch of items is cached.
*/
onBatchCache: (cb: (args: CacheBatchUpdateEvent) => void) => void;
/**
* Fetches the data associated with the given keys, via the AccountLoader.
*/
fetchKeys: FetchKeysFn;
/**
* Causes a key to be refetched periodically.
*/
subscribe: (key: PublicKey) => () => Promise<void>;
/**
* Gets the cached data of an account.
*/
getCached: (key: PublicKey) => AccountInfo<Buffer> | null | undefined;
/**
* Gets an AccountDatum from the cache.
*
* If the AccountInfo has never been fetched, this returns undefined.
* If the AccountInfo has been fetched but wasn't found, this returns null.
*/
getDatum: (key: PublicKey | null | undefined) => AccountDatum;
/**
* Fetches accounts.
*/
batchFetcher: AccountInfoFetcher;
/**
* Provider using the batch fetcher.
*/
batchProviderMut: Provider | null;
}
export declare const useAccountsInternal: (args: UseAccountsArgs) => UseAccounts;
//# sourceMappingURL=useAccountsInternal.d.ts.map