Skip to content

Commit

Permalink
added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
stevensJourney committed Jul 18, 2024
1 parent 5c33da2 commit 12f42a0
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion libs/lib-services/src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,20 @@ export type ContainerImplementationDefaultGenerators = {
[type in ContainerImplementation]: () => ContainerImplementationTypes[type];
};

/**
* Helper for identifying constructors
*/
export interface Abstract<T> {
prototype: T;
}
/**
* A basic class constructor
*/
export type Newable<T> = new (...args: never[]) => T;

/**
* Identifier used to get and register implementations
*/
export type ServiceIdentifier<T = unknown> = string | symbol | Newable<T> | Abstract<T> | ContainerImplementation;

const DEFAULT_GENERATORS: ContainerImplementationDefaultGenerators = {
Expand All @@ -35,6 +45,10 @@ const DEFAULT_GENERATORS: ContainerImplementationDefaultGenerators = {
[ContainerImplementation.TERMINATION_HANDLER]: () => createTerminationHandler()
};

/**
* A container which provides means for registering and getting various
* function implementations.
*/
export class Container {
protected implementations: Map<ServiceIdentifier<any>, any>;

Expand Down Expand Up @@ -63,6 +77,12 @@ export class Container {
this.implementations = new Map();
}

/**
* Gets an implementation given an identifier.
* An exception is thrown if the implementation has not been registered.
* Core [ContainerImplementation] identifiers are mapped to their respective implementation types.
* This also allows for getting generic implementations (unknown to the core framework) which have been registered.
*/
getImplementation<T>(identifier: Newable<T> | Abstract<T>): T;
getImplementation<T extends ContainerImplementation>(identifier: T): ContainerImplementationTypes[T];
getImplementation<T>(identifier: ServiceIdentifier<T>): T;
Expand All @@ -74,6 +94,12 @@ export class Container {
return implementation;
}

/**
* Gets an implementation given an identifier.
* Null is returned if the implementation has not been registered yet.
* Core [ContainerImplementation] identifiers are mapped to their respective implementation types.
* This also allows for getting generic implementations (unknown to the core framework) which have been registered.
*/
getOptional<T>(identifier: Newable<T> | Abstract<T>): T | null;
getOptional<T extends ContainerImplementation>(identifier: T): ContainerImplementationTypes[T] | null;
getOptional<T>(identifier: ServiceIdentifier<T>): T | null;
Expand All @@ -96,7 +122,7 @@ export class Container {
}

/**
* Allows for overriding a default implementation
* Allows for registering core and generic implementations of services/helpers.
*/
register<T>(identifier: ServiceIdentifier<T>, implementation: T) {
this.implementations.set(identifier, implementation);
Expand Down

0 comments on commit 12f42a0

Please sign in to comment.