UNPKG

@saberhq/sail

Version:

Account caching and batched loading for React-based Solana applications.

29 lines 1.11 kB
import zip from "lodash.zip"; import invariant from "tiny-invariant"; import { SailAccountLoadError } from "../errors"; export const fetchKeysUsingLoader = async (loader, keys) => { const keysWithIndex = keys.map((k, i) => [k, i]); const result = await loader.loadMany(keysWithIndex.map((k) => k[0])); const nextData = keys.map(() => ({ data: null })); zip(keysWithIndex, result).forEach(([indexInfo, keyResult]) => { invariant(indexInfo, "index info missing"); invariant(keyResult !== undefined, "key result missing"); const [accountId, nextIndex] = indexInfo; if (keyResult instanceof Error) { return (nextData[nextIndex] = { data: null, error: new SailAccountLoadError(keyResult, accountId), }); } return (nextData[nextIndex] = { data: keyResult ? { accountId, accountInfo: keyResult, } : null, }); }); return nextData; }; //# sourceMappingURL=fetchKeysUsingLoader.js.map