|
1 |
| -import memoize from "memoizee"; |
2 | 1 | import {IDict, IExtendedPoolDataFromApi, INetworkName, IPoolType} from "./interfaces.js";
|
3 | 2 | import {createCrvApyDict, createUsdPricesDict, uncached_getAllPoolsFromApi} from './external-api.js'
|
4 | 3 |
|
| 4 | +/** |
| 5 | + * Memoizes a function that returns a promise. |
| 6 | + * Custom function instead of `memoizee` because we want to be able to set the cache manually based on server data. |
| 7 | + * @param fn The function that returns a promise and will be memoized |
| 8 | + * @param maxAge The maximum age of the cache in milliseconds |
| 9 | + * @param createKey A function that creates a key for the cache based on the arguments passed to the function |
| 10 | + * @returns A memoized `fn` function that includes a `set` method to set the cache manually |
| 11 | + */ |
| 12 | +const memoize = <TResult, TParams extends any[], TFunc extends (...args: TParams) => Promise<TResult>>(fn: TFunc, { |
| 13 | + maxAge, |
| 14 | + createKey = (list) => list.toString(), |
| 15 | +}: { |
| 16 | + maxAge: number, |
| 17 | + createKey?: (args: TParams) => string |
| 18 | +}) => { |
| 19 | + const cache: Record<string, Promise<TResult>> = {}; |
| 20 | + const timeouts: Record<string, ReturnType<typeof setTimeout>> = {}; |
| 21 | + |
| 22 | + const setCache = (key: string, promise?: Promise<TResult>) => { |
| 23 | + if (promise) { |
| 24 | + cache[key] = promise; |
| 25 | + } else if (key in cache) { |
| 26 | + delete cache[key]; |
| 27 | + } |
| 28 | + if (key in timeouts) { |
| 29 | + clearTimeout(timeouts[key]); |
| 30 | + delete timeouts[key] |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + const scheduleCleanup = (key: string) => timeouts[key] = setTimeout(() => { |
| 35 | + delete timeouts[key]; |
| 36 | + delete cache[key]; |
| 37 | + }, maxAge); |
| 38 | + |
| 39 | + const cachedFn = async (...args: TParams): Promise<TResult> => { |
| 40 | + const key = createKey(args); |
| 41 | + if (key in cache) { |
| 42 | + return cache[key]; |
| 43 | + } |
| 44 | + const promise = fn(...args); |
| 45 | + setCache(key, promise); |
| 46 | + try { |
| 47 | + const result = await promise; |
| 48 | + scheduleCleanup(key) |
| 49 | + return result; |
| 50 | + } catch (e) { |
| 51 | + delete cache[key]; |
| 52 | + throw e; |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + cachedFn.set = (result: TResult, ...args: TParams) => { |
| 57 | + const key = createKey(args); |
| 58 | + setCache(key, Promise.resolve(result)); |
| 59 | + scheduleCleanup(key); |
| 60 | + } |
| 61 | + |
| 62 | + return cachedFn as TFunc & { set: (result: TResult, ...args: TParams) => void }; |
| 63 | +} |
| 64 | + |
| 65 | +const createCache = (poolsDict: Record<IPoolType, IExtendedPoolDataFromApi>) => { |
| 66 | + const poolLists = Object.values(poolsDict) |
| 67 | + const usdPrices = createUsdPricesDict(poolLists); |
| 68 | + const crvApy = createCrvApyDict(poolLists) |
| 69 | + return {poolsDict, poolLists, usdPrices, crvApy}; |
| 70 | +}; |
| 71 | + |
5 | 72 | /**
|
6 | 73 | * This function is used to cache the data fetched from the API and the data derived from it.
|
7 | 74 | * Note: do not expose this function to the outside world, instead encapsulate it in a function that returns the data you need.
|
8 | 75 | */
|
9 |
| -const _getCachedData = memoize( |
10 |
| - async (network: INetworkName, isLiteChain: boolean) => { |
11 |
| - const poolsDict = await uncached_getAllPoolsFromApi(network, isLiteChain); |
12 |
| - const poolLists = Object.values(poolsDict) |
13 |
| - const usdPrices = createUsdPricesDict(poolLists); |
14 |
| - const crvApy = createCrvApyDict(poolLists) |
15 |
| - return { poolsDict, poolLists, usdPrices, crvApy }; |
16 |
| - }, |
17 |
| - { |
18 |
| - promise: true, |
19 |
| - maxAge: 5 * 60 * 1000, // 5m |
20 |
| - primitive: true, |
21 |
| - } |
22 |
| -) |
| 76 | +const _getCachedData = memoize(async (network: INetworkName, isLiteChain: boolean) => |
| 77 | + createCache(await uncached_getAllPoolsFromApi(network, isLiteChain)), {maxAge: 1000 * 60 * 5 /* 5 minutes */}) |
23 | 78 |
|
24 | 79 | export const _getPoolsFromApi =
|
25 | 80 | async (network: INetworkName, poolType: IPoolType, isLiteChain = false): Promise<IExtendedPoolDataFromApi> => {
|
26 | 81 | const {poolsDict} = await _getCachedData(network, isLiteChain);
|
27 | 82 | return poolsDict[poolType]
|
28 | 83 | }
|
29 | 84 |
|
| 85 | +export const _setPoolsFromApi = |
| 86 | + (network: INetworkName, isLiteChain: boolean, data: Record<IPoolType, IExtendedPoolDataFromApi>): void => |
| 87 | + _getCachedData.set(createCache(data), network, isLiteChain) |
| 88 | + |
30 | 89 | export const _getAllPoolsFromApi = async (network: INetworkName, isLiteChain: boolean): Promise<IExtendedPoolDataFromApi[]> => {
|
31 | 90 | const {poolLists} = await _getCachedData(network, isLiteChain);
|
32 | 91 | return poolLists
|
|
0 commit comments