Class CachedHandler<TIn, TOut>

Caches output data from the source handler based on the input object. The fields parameter can be used to instead use one or more specific entries from the input object as cache key, so has as actual required typing (keyof TIn)[].

A WeakMap is used internally so strict object equality determines cache hits, and data will be removed once the key stops existing. This also means that the cache key needs to be an object. Errors will be thrown in case a primitive is used.

Type Parameters

  • TIn extends Record<string, unknown>
  • TOut = void

Hierarchy (view full)



  • Checks whether the input can be handled by this class. If it cannot handle the input, rejects with an error explaining why.


    • input: TIn

      Input that could potentially be handled.

    Returns Promise<void>

    A promise resolving if the input can be handled, rejecting with an Error if not.

  • Returns the WeakMap that contains actual objects that were cached, so the last WeakMap in the chain of maps.

    Returns undefined if no such map exists because earlier keys were not cached.

    Will always return a map if ensure is set to true, in such a case the intermediate maps will be created and added to the previous map.


    • input: TIn
    • keys: object[]
    • cache: WeakMap<object, NestedMap<TOut>>
    • ensure: boolean = false

    Returns undefined | WeakMap<object, TOut>

  • Extracts the values that will be used as keys from the input object. In case the fields value was undefined, this will return an array containing the input object itself.


    Returns [object, ...object[]]