Uneventful
    Preparing search index...

    Function ext

    • Experimental

      Create an extension accessor function that returns a memoized value for a given target object or function.

      (Memoization is done via WeakMap, so the cached extensions will be freed automatically once the target is garbage-collected.)

      Type Parameters

      • Target extends object

        The type of target object that will be extended. Both the passed-in factory function and the returned accessor function will take a parameter of this type, which must be an object or function type. (So it's suitable for weak referencing.)

      • ExtType extends Object

        The type of extension that will be cached. Both the passed-in factory function and the returned accessor will return a value of this type.

      Parameters

      • factory: (tgt: Target, map: WeakMap<Target, ExtType>) => ExtType

        Function called with a target object or function and a weakmap to create a new extension for that target. The result is cached in the weakmap so the factory is called at most once per target (unless you alter the weakmap contents directly).

      • map: WeakMap<Target, ExtType> = ...

        Optional: the weakmap to use to store the extensions. This allows you to manipulate the map contents (e.g. remove items or clear it) from outside the factory function. If no map is provided, one is created automatically, but then it's only accessible via the factory function's second parameter.

      Returns (tgt: Target) => ExtType

      A function that always returns the same extension for a given target (assuming you don't alter the WeakMap), calling the factory function if an extension doesn't exist yet for that target.