All files / src/utils use-promise-store.ts

92.3% Statements 12/13
75% Branches 3/4
100% Functions 4/4
92.3% Lines 12/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52          13x       13x                                   15x   15x   13x 13x   13x 11x 11x     11x     13x         15x        
import { GetOptions, GetResult } from '@fingerprint/agent'
import { useCallback } from 'react'
import { useConst } from './use-const'
 
function getCacheKey(options?: GetOptions) {
  Iif (!options) {
    return ''
  }
 
  return `${options.tag}-${options.linkedId}-${options.timeout}`
}
 
export type UsePromiseStoreReturn = {
  /**
   *  Accepts a callback that returns a promise (`requestCallback`)
   *   and optional parameters (`options`). Ensures that the same request identified by a cache key is
   *   only executed once at a time, and returns the stored promise for the request. The promise is
   *   removed from the store once it is resolved or rejected.
   * */
  doRequest: (requestCallback: () => Promise<GetResult>, options?: GetOptions) => Promise<GetResult>
}
 
/**
 * Manages a store of promises to handle unique asynchronous requests, ensuring that
 * requests with the same key are not duplicated while they are still pending.
 */
export function usePromiseStore(): UsePromiseStoreReturn {
  const store = useConst(new Map<string, Promise<GetResult>>())
 
  const doRequest = useCallback(
    (requestCallback: () => Promise<GetResult>, options?: GetOptions) => {
      const cacheKey = getCacheKey(options)
      let cachedPromise = store.get(cacheKey)
 
      if (!cachedPromise) {
        cachedPromise = requestCallback().finally(() => {
          store.delete(cacheKey)
        })
 
        store.set(cacheKey, cachedPromise)
      }
 
      return cachedPromise
    },
    [store]
  )
 
  return {
    doRequest,
  }
}