Options
All
  • Public
  • Public/Protected
  • All
Menu

A meta converter that takes an array of other converters as input. It chains these converters by finding a path of converters that can go from the given content-type to the given type preferences. In case there are multiple paths, the shortest one with the highest weight gets found. Will error in case no path can be found.

Generated paths get stored in an internal cache for later re-use on similar requests. Note that due to this caching RepresentationConverters that change supported input/output types at runtime are not supported, unless cache size is set to 0.

This is not a TypedRepresentationConverter since the supported output types might depend on what is the input content-type.

Some suggestions on how this class can be even more optimized should this ever be needed in the future. Most of these decrease computation time at the cost of more memory.

  • Subpaths that are generated could also be cached.
  • When looking for the next step, cached paths could also be considered.
  • The algorithm could start on both ends of a possible path and work towards the middle.
  • When creating a path, store the list of unused converters instead of checking every step.

Hierarchy

Index

Constructors

constructor

Properties

Private Readonly cache

cache: LruPathCache

Private Readonly converters

Protected Readonly logger

logger: Logger = ...

Methods

canHandle

Private findBest

  • findBest(type: string, preferences: Record<string, number>, paths: ConversionPath[]): undefined | MatchedPath
  • Finds the path from the given list that can convert the given type to the given preferences. If there are multiple matches the one with the highest result weight gets chosen. Will return undefined if there are no matches.

    Parameters

    • type: string
    • preferences: Record<string, number>
    • paths: ConversionPath[]

    Returns undefined | MatchedPath

Private findPath

Private generatePath

  • generatePath(inType: string, outPreferences: Record<string, number>): Promise<MatchedPath>
  • Tries to generate the optimal and shortest ConversionPath that supports the given parameters, which will then be used to instantiate a specific MatchedPath for those parameters.

    Errors if such a path does not exist.

    Parameters

    • inType: string
    • outPreferences: Record<string, number>

    Returns Promise<MatchedPath>

handle

handleSafe

Private isMatchedPath

  • isMatchedPath(path: unknown): path is MatchedPath

Private modifyTypeWeights

  • modifyTypeWeights(weight: number, types: Record<string, number>): Record<string, number>
  • Creates a new ValuePreferences object, which is equal to the input object with all values multiplied by the given weight.

    Parameters

    • weight: number
    • types: Record<string, number>

    Returns Record<string, number>

Private supportedConverters

Private takeStep

  • takeStep(path: ConversionPath): Promise<ConversionPath[]>
  • Finds all converters that could take the output of the given path as input. For each of these converters a new path gets created which is the input path appended by the converter.

    Parameters

    • path: ConversionPath

    Returns Promise<ConversionPath[]>