Class Registry<K, V>

The Singleton Registry is a mapping used by the Provider to lookup Provided constructors by Registrations upon class extension. Magic strings should represent sane module paths in dot-notation. Whenever a currently not registered constructor is requested, an intermediary class is created, cached internally and returned. When the actual constructor is registered later, the previously created intermediary class is removed from the internal caching and further steps are taken to guarantee the transparent addressing of the actual constructor through the dropped intermediary class.

Decorator

Singleton

See

Type Parameters

Hierarchy

Constructors

Properties

Methods

Constructors

  • Public constructor. The constructor of this class accepts the same parameters as its overridden super Map constructor and acts the same. I.e., through instantiating this Singleton class and passing a list of tuples of Registrations and their corresponding class constructors, these tuples may be preemptively registered.

    Type Parameters

    Parameters

    • Optional tuples: Iterable<[K, V]>

      An Iterable of tuples provide.

    Returns Registry<K, V>

    Example

    Preemptively provide a class constructor by magic string:

    import { type Registration, Registry } from '@sgrud/core';
    import { Service } from './service';

    const registration = 'sgrud.example.Service';
    new Registry<Registration, typeof Service>([
    [registration, Service]
    ]);

Properties

cached: Map<K, V>

Internal Mapping of all cached, i.e., forward-referenced, class constructors. Whenever a constructor, which is not currently registered, is requested as a Provider, an intermediary class is created and stored within this map until the actual constructor is registered. As soon as this happens, the intermediary class is removed from this map and further steps are taken to guarantee the transparent addressing of the actual constructor through the dropped intermediary class.

caches: WeakSet<V>

Internally used WeakSet containing all intermediary classes created upon requesting a currently not registered constructor as provider. This set is used internally to check if a intermediary class has already been replaced by the actual constructor.

Methods

  • Overridden get method. Looks up the Provided constructor by magic string. If no provided constructor is found, an intermediary class is created, cached internally and returned. While this intermediary class and the functionality supporting it take care of inheritance, i.e., allow forward-referenced base classes to be extended, it cannot substitute for the actual extended constructor. Therefore, the static extension of forward-referenced classes is possible, but as long as the actual extended constructor is not registered (and therefore the intermediary class still caches the inheritance chain), the extending classes cannot be instantiated, called etc. Doing so will result in a ReferenceError being thrown.

    Parameters

    • registration: K

      The magic string to get the class constructor by.

    Returns V

    The Provided constructor or a cached intermediary.

    Throws

    A ReferenceError when a cached class is invoked.

    Example

    Retrieve a provided constructor by magic string:

    import { type Registration, Registry } from '@sgrud/core';
    import { type Service } from 'example-module';

    const registration = 'sgrud.example.Service';
    new Registry<Registration, typeof Service>().get(registration);
  • Overridden set method. Whenever a class constructor is provided by magic string through calling this method, a test is run, whether this constructor was previously requested and therefore cached as intermediary class. If so, the intermediary class is removed from the internal mapping and further steps are taken to guarantee the transparent addressing of the newly provided constructor through the previously cached and now dropped intermediary class.

    Parameters

    • registration: K

      The magic string to set the class constructor by.

    • constructor: V

      The constructor to register for the registration.

    Returns this

    This Registry instance.

    Example

    Preemptively provide a constructor by magic string:

    import { type Registration, Registry } from '@sgrud/core';
    import { Service } from './service';

    const registration = 'sgrud.example.Service';
    new Registry<Registration, typeof Service>().set(registration, Service);

Generated using TypeDoc