From 030721af65d7e1c805737ef2360703229d4ff319 Mon Sep 17 00:00:00 2001 From: alin Date: Thu, 22 Feb 2024 16:18:44 +0100 Subject: [PATCH] Use new Concern Constructor interface --- .../src/support/concerns/Injector.ts | 13 +- .../src/support/concerns/MustUseConcerns.ts | 4 +- .../contracts/src/support/concerns/types.ts | 9 +- .../support/src/concerns/ConcernsInjector.ts | 268 +++++++++--------- 4 files changed, 151 insertions(+), 143 deletions(-) diff --git a/packages/contracts/src/support/concerns/Injector.ts b/packages/contracts/src/support/concerns/Injector.ts index 8b462c5d..4a65c9f2 100644 --- a/packages/contracts/src/support/concerns/Injector.ts +++ b/packages/contracts/src/support/concerns/Injector.ts @@ -1,5 +1,6 @@ import { Constructor } from "@aedart/contracts"; import Concern from "./Concern"; +import ConcernConstructor from "./ConcernConstructor"; import Configuration from "./Configuration"; import MustUseConcerns from "./MustUseConcerns"; @@ -36,13 +37,13 @@ export default interface Injector * @template T = object The target class that concern classes must be injected into * @template C = {@link Concern} * - * @param {Constructor | Configuration} concerns List of concern classes / injection configurations + * @param {ConcernConstructor | Configuration} concerns List of concern classes / injection configurations * * @returns {MustUseConcerns} The modified target class * * @throws {InjectionException} */ - inject(...concerns: (Constructor|Configuration)[]): MustUseConcerns; + inject(...concerns: (ConcernConstructor|Configuration)[]): MustUseConcerns; /** * Defines the concern classes that must be used by the target class. @@ -50,6 +51,7 @@ export default interface Injector * **Note**: _Method changes the target class, such that it implements and respects the * {@link MustUseConcerns} interface. The original target class' constructor remains the untouched!_ * + * @template C extends Concern * @template T = object * * @param {T} target The target class that must define the concern classes to be used @@ -60,7 +62,7 @@ export default interface Injector * @throws {InjectionException} If given concern classes conflict with target class' parent concern classes, * e.g. in case of duplicates. Or, if unable to modify target class. */ - defineConcerns(target: T, concerns: Constructor[]): MustUseConcerns; + defineConcerns(target: T, concerns: ConcernConstructor[]): MustUseConcerns; /** * Defines a concerns {@link Container} in target class' prototype. @@ -102,6 +104,7 @@ export default interface Injector * **Note**: _Method will do nothing, if a property or method already exists in the target class' prototype * chain, with the same name as given "alias"._ * + * @template C extends Concern * @template T = object * * @param {MustUseConcerns} target The target in which "alias" must be defined in @@ -115,10 +118,10 @@ export default interface Injector * @throws {InjectionException} If unable to define "alias" in target class, e.g. due to failure when obtaining * or defining [property descriptors]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor#description}. */ - defineAlias( + defineAlias( target: MustUseConcerns, alias: PropertyKey, key: PropertyKey, - source: Constructor + source: ConcernConstructor ): boolean; } \ No newline at end of file diff --git a/packages/contracts/src/support/concerns/MustUseConcerns.ts b/packages/contracts/src/support/concerns/MustUseConcerns.ts index 7c76c9ad..5d4c502a 100644 --- a/packages/contracts/src/support/concerns/MustUseConcerns.ts +++ b/packages/contracts/src/support/concerns/MustUseConcerns.ts @@ -1,5 +1,5 @@ import type { ConstructorOrAbstractConstructor } from "@aedart/contracts"; -import type { ConcernClasses } from "./index"; +import type { ConcernClasses, Concern } from "./index"; import { CONCERN_CLASSES } from "./index"; import Owner from "./Owner"; @@ -35,5 +35,5 @@ export default interface MustUseConcerns * * @return {ConcernClasses} */ - [CONCERN_CLASSES](): ConcernClasses; + [CONCERN_CLASSES](): ConcernClasses; } \ No newline at end of file diff --git a/packages/contracts/src/support/concerns/types.ts b/packages/contracts/src/support/concerns/types.ts index 0f78abd1..32309490 100644 --- a/packages/contracts/src/support/concerns/types.ts +++ b/packages/contracts/src/support/concerns/types.ts @@ -1,5 +1,6 @@ -import { Constructor, ConstructorOrAbstractConstructor } from "@aedart/contracts"; +import { ConstructorOrAbstractConstructor } from "@aedart/contracts"; import Concern from "./Concern"; +import ConcernConstructor from "./ConcernConstructor"; /** * An alias for a property or method in a {@link Concern} class @@ -21,8 +22,8 @@ export type Aliases = { /** * Array that holds a {@link Concern} class / Owner class pair. */ -export type ConcernOwnerClassPair = [ - Constructor, // Concern Class +export type ConcernOwnerClassPair = [ + ConcernConstructor, // Concern Class ConstructorOrAbstractConstructor // Owner class that must use the concern class ]; @@ -32,4 +33,4 @@ export type ConcernOwnerClassPair = [ * * @see ConcernOwnerClassPair */ -export type ConcernClasses = ConcernOwnerClassPair[]; \ No newline at end of file +export type ConcernClasses = ConcernOwnerClassPair[]; \ No newline at end of file diff --git a/packages/support/src/concerns/ConcernsInjector.ts b/packages/support/src/concerns/ConcernsInjector.ts index d7cc234c..40aee181 100644 --- a/packages/support/src/concerns/ConcernsInjector.ts +++ b/packages/support/src/concerns/ConcernsInjector.ts @@ -1,5 +1,6 @@ import { Concern, + ConcernConstructor, Injector, MustUseConcerns, Configuration @@ -46,137 +47,140 @@ export default class ConcernsInjector implements Injector { return this.#target; } - - /** - * Injects concern classes into the target class and return the modified target. - * - * **Note**: _Method performs injection in the following way:_ - * - * _**A**: Defines the concern classes in target class, via {@link defineConcerns}._ - * - * _**B**: Defines a concerns container in target class' prototype, via {@link defineContainer}._ - * - * _**C**: Defines "aliases" (proxy properties and methods) in target class' prototype, via {@link defineAliases}._ - * - * @template T = object The target class that concern classes must be injected into - * @template C = {@link Concern} - * - * @param {Constructor | Configuration} concerns List of concern classes / injection configurations - * - * @returns {MustUseConcerns} The modified target class - * - * @throws {InjectionException} - */ - public inject(...concerns: (Constructor|Configuration)[]): MustUseConcerns - { - // TODO: implement this method... - - // Resolve arguments, such that they are of type "concern injection configuration". - - // A) Define the concern classes in target class - - // B) Define a concerns container in target class' prototype - - // C) Define "aliases" (proxy properties and methods) in target class' prototype - - return this.target as MustUseConcerns; - } - /** - * Defines the concern classes that must be used by the target class. - * - * **Note**: _Method changes the target class, such that it implements and respects the - * {@link MustUseConcerns} interface. The original target class' constructor remains the untouched!_ - * - * @template T = object - * - * @param {T} target The target class that must define the concern classes to be used - * @param {Constructor[]} concerns List of concern classes - * - * @returns {MustUseConcerns} The modified target class - * - * @throws {InjectionException} If given concern classes conflict with target class' parent concern classes, - * e.g. in case of duplicates. Or, if unable to modify target class. - */ - public defineConcerns(target: T, concerns: Constructor[]): MustUseConcerns - { - // TODO: implement this method... - - return target as MustUseConcerns; - } - - /** - * Defines a concerns {@link Container} in target class' prototype. - * - * **Note**: _Method changes the target class, such that it implements and respects the - * [Owner]{@link import('@aedart/contracts/support/concerns').Owner} interface!_ - * - * @template T = object - * - * @param {MustUseConcerns} target The target in which a concerns container must be defined - * - * @returns {MustUseConcerns} The modified target class - * - * @throws {InjectionException} If unable to define concerns container in target class - */ - public defineContainer(target: MustUseConcerns): MustUseConcerns - { - // TODO: implement this method... - - return target; - } - - /** - * Defines "aliases" (proxy properties and methods) in target class' prototype, such that they - * point to the properties and methods available in the concern classes. - * - * **Note**: _Method defines each alias using the {@link defineAlias} method!_ - * - * @template T = object - * - * @param {MustUseConcerns} target The target in which "aliases" must be defined in - * @param {Configuration[]} configurations List of concern injection configurations - * - * @returns {MustUseConcerns} The modified target class - * - * @throws {InjectionException} If case of alias naming conflicts. Or, if unable to define aliases in target class. - */ - public defineAliases(target: MustUseConcerns, configurations: Configuration[]): MustUseConcerns - { - // TODO: implement this method... - - return target; - } - - /** - * Defines an "alias" (proxy property or method) in target class' prototype, to a property or method - * in given concern. - * - * **Note**: _Method will do nothing, if a property or method already exists in the target class' prototype - * chain, with the same name as given "alias"._ - * - * @template T = object - * - * @param {MustUseConcerns} target The target in which "alias" must be defined in - * @param {PropertyKey} alias Name of the "alias" in the target class (name of the proxy property or method) - * @param {PropertyKey} key Name of the property or method that the "alias" is for, in the concern class (`source`) - * @param {Constructor} source The concern class that holds the property or methods (`key`) - * - * @returns {boolean} `true` if "alias" was in target class. `false` if not, e.g. a property or method already - * exists in target class' prototype chain, with the same name as the alias. - * - * @throws {InjectionException} If unable to define "alias" in target class, e.g. due to failure when obtaining - * or defining [property descriptors]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor#description}. - */ - public defineAlias( - target: MustUseConcerns, - alias: PropertyKey, - key: PropertyKey, - source: Constructor - ): boolean - { - // TODO: implement this method... - - return false; - } + // TODO: INCOMPLETE... + + // + // /** + // * Injects concern classes into the target class and return the modified target. + // * + // * **Note**: _Method performs injection in the following way:_ + // * + // * _**A**: Defines the concern classes in target class, via {@link defineConcerns}._ + // * + // * _**B**: Defines a concerns container in target class' prototype, via {@link defineContainer}._ + // * + // * _**C**: Defines "aliases" (proxy properties and methods) in target class' prototype, via {@link defineAliases}._ + // * + // * @template T = object The target class that concern classes must be injected into + // * @template C = {@link Concern} + // * + // * @param {Constructor | Configuration} concerns List of concern classes / injection configurations + // * + // * @returns {MustUseConcerns} The modified target class + // * + // * @throws {InjectionException} + // */ + // public inject(...concerns: (Constructor|Configuration)[]): MustUseConcerns + // { + // // TODO: implement this method... + // + // // Resolve arguments, such that they are of type "concern injection configuration". + // + // // A) Define the concern classes in target class + // + // // B) Define a concerns container in target class' prototype + // + // // C) Define "aliases" (proxy properties and methods) in target class' prototype + // + // return this.target as MustUseConcerns; + // } + // + // /** + // * Defines the concern classes that must be used by the target class. + // * + // * **Note**: _Method changes the target class, such that it implements and respects the + // * {@link MustUseConcerns} interface. The original target class' constructor remains the untouched!_ + // * + // * @template T = object + // * + // * @param {T} target The target class that must define the concern classes to be used + // * @param {Constructor[]} concerns List of concern classes + // * + // * @returns {MustUseConcerns} The modified target class + // * + // * @throws {InjectionException} If given concern classes conflict with target class' parent concern classes, + // * e.g. in case of duplicates. Or, if unable to modify target class. + // */ + // public defineConcerns(target: T, concerns: Constructor[]): MustUseConcerns + // { + // // TODO: implement this method... + // + // return target as MustUseConcerns; + // } + // + // /** + // * Defines a concerns {@link Container} in target class' prototype. + // * + // * **Note**: _Method changes the target class, such that it implements and respects the + // * [Owner]{@link import('@aedart/contracts/support/concerns').Owner} interface!_ + // * + // * @template T = object + // * + // * @param {MustUseConcerns} target The target in which a concerns container must be defined + // * + // * @returns {MustUseConcerns} The modified target class + // * + // * @throws {InjectionException} If unable to define concerns container in target class + // */ + // public defineContainer(target: MustUseConcerns): MustUseConcerns + // { + // // TODO: implement this method... + // + // return target; + // } + // + // /** + // * Defines "aliases" (proxy properties and methods) in target class' prototype, such that they + // * point to the properties and methods available in the concern classes. + // * + // * **Note**: _Method defines each alias using the {@link defineAlias} method!_ + // * + // * @template T = object + // * + // * @param {MustUseConcerns} target The target in which "aliases" must be defined in + // * @param {Configuration[]} configurations List of concern injection configurations + // * + // * @returns {MustUseConcerns} The modified target class + // * + // * @throws {InjectionException} If case of alias naming conflicts. Or, if unable to define aliases in target class. + // */ + // public defineAliases(target: MustUseConcerns, configurations: Configuration[]): MustUseConcerns + // { + // // TODO: implement this method... + // + // return target; + // } + // + // /** + // * Defines an "alias" (proxy property or method) in target class' prototype, to a property or method + // * in given concern. + // * + // * **Note**: _Method will do nothing, if a property or method already exists in the target class' prototype + // * chain, with the same name as given "alias"._ + // * + // * @template T = object + // * + // * @param {MustUseConcerns} target The target in which "alias" must be defined in + // * @param {PropertyKey} alias Name of the "alias" in the target class (name of the proxy property or method) + // * @param {PropertyKey} key Name of the property or method that the "alias" is for, in the concern class (`source`) + // * @param {Constructor} source The concern class that holds the property or methods (`key`) + // * + // * @returns {boolean} `true` if "alias" was in target class. `false` if not, e.g. a property or method already + // * exists in target class' prototype chain, with the same name as the alias. + // * + // * @throws {InjectionException} If unable to define "alias" in target class, e.g. due to failure when obtaining + // * or defining [property descriptors]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor#description}. + // */ + // public defineAlias( + // target: MustUseConcerns, + // alias: PropertyKey, + // key: PropertyKey, + // source: Constructor + // ): boolean + // { + // // TODO: implement this method... + // + // return false; + // } } \ No newline at end of file