@wessberg/di
Advanced tools
Comparing version 1.0.26 to 1.0.27
@@ -1,2 +0,9 @@ | ||
<a name="1.0.26"></a> | ||
## <small>1.0.27 (2018-06-08)</small> | ||
* 1.0.27 ([4cb4d7a](https://github.com/wessberg/di/commit/4cb4d7a)) | ||
* Added partial support for handling circular dependencies. Added handling for constructor arguments t ([6e73c7a](https://github.com/wessberg/di/commit/6e73c7a)) | ||
* Bumped version ([c02089f](https://github.com/wessberg/di/commit/c02089f)) | ||
## <small>1.0.26 (2018-02-03)</small> | ||
@@ -10,3 +17,2 @@ | ||
<a name="1.0.25"></a> | ||
## <small>1.0.25 (2017-09-10)</small> | ||
@@ -21,3 +27,2 @@ | ||
<a name="1.0.24"></a> | ||
## <small>1.0.24 (2017-09-03)</small> | ||
@@ -30,3 +35,2 @@ | ||
<a name="1.0.23"></a> | ||
## <small>1.0.23 (2017-09-03)</small> | ||
@@ -42,3 +46,2 @@ | ||
<a name="1.0.21"></a> | ||
## <small>1.0.21 (2017-07-20)</small> | ||
@@ -52,3 +55,2 @@ | ||
<a name="1.0.20"></a> | ||
## <small>1.0.20 (2017-07-19)</small> | ||
@@ -62,3 +64,2 @@ | ||
<a name="1.0.19"></a> | ||
## <small>1.0.19 (2017-07-19)</small> | ||
@@ -72,3 +73,2 @@ | ||
<a name="1.0.18"></a> | ||
## <small>1.0.18 (2017-07-19)</small> | ||
@@ -83,3 +83,2 @@ | ||
<a name="1.0.17"></a> | ||
## <small>1.0.17 (2017-05-31)</small> | ||
@@ -92,3 +91,2 @@ | ||
<a name="1.0.16"></a> | ||
## <small>1.0.16 (2017-05-30)</small> | ||
@@ -101,3 +99,2 @@ | ||
<a name="1.0.15"></a> | ||
## <small>1.0.15 (2017-05-24)</small> | ||
@@ -110,3 +107,2 @@ | ||
<a name="1.0.14"></a> | ||
## <small>1.0.14 (2017-05-24)</small> | ||
@@ -119,3 +115,2 @@ | ||
<a name="1.0.13"></a> | ||
## <small>1.0.13 (2017-05-24)</small> | ||
@@ -128,3 +123,2 @@ | ||
<a name="1.0.12"></a> | ||
## <small>1.0.12 (2017-05-19)</small> | ||
@@ -137,3 +131,2 @@ | ||
<a name="1.0.11"></a> | ||
## <small>1.0.11 (2017-05-18)</small> | ||
@@ -146,3 +139,2 @@ | ||
<a name="1.0.10"></a> | ||
## <small>1.0.10 (2017-05-18)</small> | ||
@@ -155,3 +147,2 @@ | ||
<a name="1.0.9"></a> | ||
## <small>1.0.9 (2017-05-18)</small> | ||
@@ -164,3 +155,2 @@ | ||
<a name="1.0.8"></a> | ||
## <small>1.0.8 (2017-05-18)</small> | ||
@@ -173,3 +163,2 @@ | ||
<a name="1.0.7"></a> | ||
## <small>1.0.7 (2017-05-18)</small> | ||
@@ -182,3 +171,2 @@ | ||
<a name="1.0.6"></a> | ||
## <small>1.0.6 (2017-05-18)</small> | ||
@@ -191,3 +179,2 @@ | ||
<a name="1.0.5"></a> | ||
## <small>1.0.5 (2017-05-16)</small> | ||
@@ -200,3 +187,2 @@ | ||
<a name="1.0.4"></a> | ||
## <small>1.0.4 (2017-05-16)</small> | ||
@@ -209,3 +195,2 @@ | ||
<a name="1.0.3"></a> | ||
## <small>1.0.3 (2017-05-14)</small> | ||
@@ -218,3 +203,2 @@ | ||
<a name="1.0.2"></a> | ||
## <small>1.0.2 (2017-04-24)</small> | ||
@@ -227,3 +211,2 @@ | ||
<a name="1.0.1"></a> | ||
## <small>1.0.1 (2017-04-24)</small> | ||
@@ -230,0 +213,0 @@ |
@@ -1,5 +0,5 @@ | ||
import { IDIContainer } from "./i-di-container"; | ||
import { IRegisterOptions } from "../register-options/i-register-options"; | ||
import { IGetOptions } from "../get-options/i-get-options"; | ||
import { IHasOptions } from "../has-options/i-has-options"; | ||
import { IRegisterOptions } from "../register-options/i-register-options"; | ||
import { IDIContainer } from "./i-di-container"; | ||
/** | ||
@@ -72,3 +72,3 @@ * A Dependency-Injection container that holds services and can produce instances of them as required. | ||
*/ | ||
private register<T, U>(kind, newExpression?, options?); | ||
private register; | ||
/** | ||
@@ -79,3 +79,3 @@ * Returns true if an instance exists that matches the given identifier. | ||
*/ | ||
private hasInstance(identifier); | ||
private hasInstance; | ||
/** | ||
@@ -86,3 +86,3 @@ * Gets the cached instance, if any, associated with the given identifier. | ||
*/ | ||
private getInstance<T>(identifier); | ||
private getInstance; | ||
/** | ||
@@ -94,3 +94,3 @@ * Gets an IRegistrationRecord associated with the given identifier. | ||
*/ | ||
private getRegistrationRecord<T, U>({identifier, parent}); | ||
private getRegistrationRecord; | ||
/** | ||
@@ -102,11 +102,18 @@ * Caches the given instance so that it can be retrieved in the future. | ||
*/ | ||
private setInstance<T>(identifier, instance); | ||
private setInstance; | ||
/** | ||
* Gets a proxied instance | ||
* @param {T} instance | ||
* @returns {T} | ||
*/ | ||
private getLazyInstance; | ||
/** | ||
* Constructs a new instance of the given identifier and returns it. | ||
* It checks the constructor arguments and injects any services it might depend on recursively. | ||
* @param {string} identifier | ||
* @param {string?} parent | ||
* @returns {T} | ||
*/ | ||
private constructInstance<T>({identifier, parent}); | ||
private constructInstance; | ||
} | ||
export declare const DIContainer: DIServiceContainer; |
@@ -0,2 +1,5 @@ | ||
import { CONSTRUCTOR_ARGUMENTS_IDENTIFIER } from "../constructor-arguments/constructor-arguments-identifier"; | ||
import { RegistrationKind } from "../registration-kind/registration-kind"; | ||
// tslint:disable:variable-name | ||
// tslint:disable:no-any | ||
/** | ||
@@ -23,6 +26,4 @@ * A Dependency-Injection container that holds services and can produce instances of them as required. | ||
*/ | ||
/*tslint:disable:no-any*/ | ||
this.instances = new Map(); | ||
} | ||
/*tslint:enable:no-any*/ | ||
/** | ||
@@ -90,6 +91,9 @@ * Registers a service that will be instantiated once in the application lifecycle. All requests | ||
throw new ReferenceError(`${this.constructor.name} could not register service: No options was given!`); | ||
// Add the constructor arguments if some were given | ||
if (options.constructorArguments != null) { | ||
this.constructorArguments.set(options.identifier, [...options.constructorArguments]); | ||
} | ||
// Add the constructor arguments if there is an implementation and it has a static property representing its constructor arguments | ||
const args = options.constructorArguments != null | ||
? options.constructorArguments | ||
: options.implementation != null | ||
? options.implementation[CONSTRUCTOR_ARGUMENTS_IDENTIFIER] | ||
: null; | ||
this.constructorArguments.set(options.identifier, [...(args == null ? [] : args)]); | ||
this.serviceRegistry.set(options.identifier, Object.assign({}, options, { kind }, (newExpression == null ? {} : { newExpression }))); | ||
@@ -137,5 +141,14 @@ } | ||
/** | ||
* Gets a proxied instance | ||
* @param {T} instance | ||
* @returns {T} | ||
*/ | ||
getLazyInstance(instance) { | ||
return new Proxy({}, { get: (_, key) => instance[key] }); | ||
} | ||
/** | ||
* Constructs a new instance of the given identifier and returns it. | ||
* It checks the constructor arguments and injects any services it might depend on recursively. | ||
* @param {string} identifier | ||
* @param {string?} parent | ||
* @returns {T} | ||
@@ -151,2 +164,3 @@ */ | ||
let instance; | ||
let circular = false; | ||
// If a user-provided new-expression has been provided, invoke that to get an instance. | ||
@@ -163,2 +177,4 @@ if (registrationRecord.newExpression != null) { | ||
const instanceArgs = mappedArgs.map((dep) => dep === undefined ? undefined : this.constructInstance({ identifier: dep, parent: identifier })); | ||
// It is circular if any of the arguments are identical to that of the identifier | ||
circular = mappedArgs.some(arg => arg === identifier); | ||
try { | ||
@@ -177,2 +193,6 @@ // Try to construct an instance with 'new' and if it fails, call the implementation directly. | ||
} | ||
// Make the instance lazy if 'circular' is given in the RegistrationOptions | ||
if (circular) { | ||
instance = this.getLazyInstance(instance); | ||
} | ||
return registrationRecord.kind === RegistrationKind.SINGLETON ? this.setInstance(identifier, instance) : instance; | ||
@@ -182,5 +202,3 @@ } | ||
// Provide access to a concrete instance of the DIServiceContainer to the outside. | ||
/*tslint:disable*/ | ||
export const DIContainer = new DIServiceContainer(); | ||
/*tslint:enable*/ | ||
//# sourceMappingURL=di-container.js.map |
@@ -1,2 +0,2 @@ | ||
export { ConstructorArgument } from "./constructor-argument/constructor-argument"; | ||
export { ConstructorArgument } from "./constructor-arguments/constructor-argument"; | ||
export { IGetOptions } from "./get-options/i-get-options"; | ||
@@ -7,1 +7,2 @@ export { IRegisterOptions } from "./register-options/i-register-options"; | ||
export { IDIContainer } from "./di-container/i-di-container"; | ||
export { CONSTRUCTOR_ARGUMENTS_IDENTIFIER } from "./constructor-arguments/constructor-arguments-identifier"; |
export { DIContainer } from "./di-container/di-container"; | ||
export { CONSTRUCTOR_ARGUMENTS_IDENTIFIER } from "./constructor-arguments/constructor-arguments-identifier"; | ||
//# sourceMappingURL=index.js.map |
import { IContainerIdentifierable } from "../container-identifierable/i-container-identifierable"; | ||
import { NewableService } from "../newable-service/newable-service"; | ||
import { CustomConstructableService } from "../custom-constructable-service/custom-constructable-service"; | ||
import { ConstructorArgument } from "../constructor-argument/constructor-argument"; | ||
import { ConstructorArgument } from "../constructor-arguments/constructor-argument"; | ||
export interface IRegisterOptions<T> extends IContainerIdentifierable { | ||
implementation: NewableService<T> | CustomConstructableService<T> | null; | ||
constructorArguments: Iterable<ConstructorArgument> | null; | ||
constructorArguments?: Iterable<ConstructorArgument> | null; | ||
} |
export declare enum RegistrationKind { | ||
SINGLETON = 0, | ||
TRANSIENT = 1, | ||
TRANSIENT = 1 | ||
} |
@@ -1,5 +0,5 @@ | ||
import { IDIContainer } from "./i-di-container"; | ||
import { IRegisterOptions } from "../register-options/i-register-options"; | ||
import { IGetOptions } from "../get-options/i-get-options"; | ||
import { IHasOptions } from "../has-options/i-has-options"; | ||
import { IRegisterOptions } from "../register-options/i-register-options"; | ||
import { IDIContainer } from "./i-di-container"; | ||
/** | ||
@@ -72,3 +72,3 @@ * A Dependency-Injection container that holds services and can produce instances of them as required. | ||
*/ | ||
private register<T, U>(kind, newExpression?, options?); | ||
private register; | ||
/** | ||
@@ -79,3 +79,3 @@ * Returns true if an instance exists that matches the given identifier. | ||
*/ | ||
private hasInstance(identifier); | ||
private hasInstance; | ||
/** | ||
@@ -86,3 +86,3 @@ * Gets the cached instance, if any, associated with the given identifier. | ||
*/ | ||
private getInstance<T>(identifier); | ||
private getInstance; | ||
/** | ||
@@ -94,3 +94,3 @@ * Gets an IRegistrationRecord associated with the given identifier. | ||
*/ | ||
private getRegistrationRecord<T, U>({identifier, parent}); | ||
private getRegistrationRecord; | ||
/** | ||
@@ -102,11 +102,18 @@ * Caches the given instance so that it can be retrieved in the future. | ||
*/ | ||
private setInstance<T>(identifier, instance); | ||
private setInstance; | ||
/** | ||
* Gets a proxied instance | ||
* @param {T} instance | ||
* @returns {T} | ||
*/ | ||
private getLazyInstance; | ||
/** | ||
* Constructs a new instance of the given identifier and returns it. | ||
* It checks the constructor arguments and injects any services it might depend on recursively. | ||
* @param {string} identifier | ||
* @param {string?} parent | ||
* @returns {T} | ||
*/ | ||
private constructInstance<T>({identifier, parent}); | ||
private constructInstance; | ||
} | ||
export declare const DIContainer: DIServiceContainer; |
@@ -7,3 +7,3 @@ (function (factory) { | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports", "../registration-kind/registration-kind"], factory); | ||
define(["require", "exports", "../constructor-arguments/constructor-arguments-identifier", "../registration-kind/registration-kind"], factory); | ||
} | ||
@@ -13,3 +13,6 @@ })(function (require, exports) { | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const constructor_arguments_identifier_1 = require("../constructor-arguments/constructor-arguments-identifier"); | ||
const registration_kind_1 = require("../registration-kind/registration-kind"); | ||
// tslint:disable:variable-name | ||
// tslint:disable:no-any | ||
/** | ||
@@ -36,6 +39,4 @@ * A Dependency-Injection container that holds services and can produce instances of them as required. | ||
*/ | ||
/*tslint:disable:no-any*/ | ||
this.instances = new Map(); | ||
} | ||
/*tslint:enable:no-any*/ | ||
/** | ||
@@ -103,6 +104,9 @@ * Registers a service that will be instantiated once in the application lifecycle. All requests | ||
throw new ReferenceError(`${this.constructor.name} could not register service: No options was given!`); | ||
// Add the constructor arguments if some were given | ||
if (options.constructorArguments != null) { | ||
this.constructorArguments.set(options.identifier, [...options.constructorArguments]); | ||
} | ||
// Add the constructor arguments if there is an implementation and it has a static property representing its constructor arguments | ||
const args = options.constructorArguments != null | ||
? options.constructorArguments | ||
: options.implementation != null | ||
? options.implementation[constructor_arguments_identifier_1.CONSTRUCTOR_ARGUMENTS_IDENTIFIER] | ||
: null; | ||
this.constructorArguments.set(options.identifier, [...(args == null ? [] : args)]); | ||
this.serviceRegistry.set(options.identifier, Object.assign({}, options, { kind }, (newExpression == null ? {} : { newExpression }))); | ||
@@ -150,5 +154,14 @@ } | ||
/** | ||
* Gets a proxied instance | ||
* @param {T} instance | ||
* @returns {T} | ||
*/ | ||
getLazyInstance(instance) { | ||
return new Proxy({}, { get: (_, key) => instance[key] }); | ||
} | ||
/** | ||
* Constructs a new instance of the given identifier and returns it. | ||
* It checks the constructor arguments and injects any services it might depend on recursively. | ||
* @param {string} identifier | ||
* @param {string?} parent | ||
* @returns {T} | ||
@@ -164,2 +177,3 @@ */ | ||
let instance; | ||
let circular = false; | ||
// If a user-provided new-expression has been provided, invoke that to get an instance. | ||
@@ -176,2 +190,4 @@ if (registrationRecord.newExpression != null) { | ||
const instanceArgs = mappedArgs.map((dep) => dep === undefined ? undefined : this.constructInstance({ identifier: dep, parent: identifier })); | ||
// It is circular if any of the arguments are identical to that of the identifier | ||
circular = mappedArgs.some(arg => arg === identifier); | ||
try { | ||
@@ -190,2 +206,6 @@ // Try to construct an instance with 'new' and if it fails, call the implementation directly. | ||
} | ||
// Make the instance lazy if 'circular' is given in the RegistrationOptions | ||
if (circular) { | ||
instance = this.getLazyInstance(instance); | ||
} | ||
return registrationRecord.kind === registration_kind_1.RegistrationKind.SINGLETON ? this.setInstance(identifier, instance) : instance; | ||
@@ -196,6 +216,4 @@ } | ||
// Provide access to a concrete instance of the DIServiceContainer to the outside. | ||
/*tslint:disable*/ | ||
exports.DIContainer = new DIServiceContainer(); | ||
}); | ||
/*tslint:enable*/ | ||
//# sourceMappingURL=di-container.js.map |
@@ -1,2 +0,2 @@ | ||
export { ConstructorArgument } from "./constructor-argument/constructor-argument"; | ||
export { ConstructorArgument } from "./constructor-arguments/constructor-argument"; | ||
export { IGetOptions } from "./get-options/i-get-options"; | ||
@@ -7,1 +7,2 @@ export { IRegisterOptions } from "./register-options/i-register-options"; | ||
export { IDIContainer } from "./di-container/i-di-container"; | ||
export { CONSTRUCTOR_ARGUMENTS_IDENTIFIER } from "./constructor-arguments/constructor-arguments-identifier"; |
@@ -7,3 +7,3 @@ (function (factory) { | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports", "./di-container/di-container"], factory); | ||
define(["require", "exports", "./di-container/di-container", "./constructor-arguments/constructor-arguments-identifier"], factory); | ||
} | ||
@@ -15,3 +15,5 @@ })(function (require, exports) { | ||
exports.DIContainer = di_container_1.DIContainer; | ||
var constructor_arguments_identifier_1 = require("./constructor-arguments/constructor-arguments-identifier"); | ||
exports.CONSTRUCTOR_ARGUMENTS_IDENTIFIER = constructor_arguments_identifier_1.CONSTRUCTOR_ARGUMENTS_IDENTIFIER; | ||
}); | ||
//# sourceMappingURL=index.js.map |
import { IContainerIdentifierable } from "../container-identifierable/i-container-identifierable"; | ||
import { NewableService } from "../newable-service/newable-service"; | ||
import { CustomConstructableService } from "../custom-constructable-service/custom-constructable-service"; | ||
import { ConstructorArgument } from "../constructor-argument/constructor-argument"; | ||
import { ConstructorArgument } from "../constructor-arguments/constructor-argument"; | ||
export interface IRegisterOptions<T> extends IContainerIdentifierable { | ||
implementation: NewableService<T> | CustomConstructableService<T> | null; | ||
constructorArguments: Iterable<ConstructorArgument> | null; | ||
constructorArguments?: Iterable<ConstructorArgument> | null; | ||
} |
export declare enum RegistrationKind { | ||
SINGLETON = 0, | ||
TRANSIENT = 1, | ||
TRANSIENT = 1 | ||
} |
{ | ||
"name": "@wessberg/di", | ||
"version": "1.0.26", | ||
"version": "1.0.27", | ||
"description": "A Dependency-Injection container that holds services and can produce instances of them as required. It mimics reflection by parsing the app at compile-time and supporting the generic-reflection syntax.", | ||
@@ -45,10 +45,10 @@ "scripts": { | ||
"devDependencies": { | ||
"@wessberg/ts-config": "0.0.25", | ||
"conventional-changelog-cli": "^1.3.8", | ||
"@wessberg/ts-config": "0.0.27", | ||
"conventional-changelog-cli": "^2.0.1", | ||
"husky": "latest", | ||
"tslint": "^5.9.1", | ||
"typescript": "^2.7.1" | ||
"tslint": "^5.10.0", | ||
"typescript": "^2.9.1" | ||
}, | ||
"dependencies": { | ||
"tslib": "^1.9.0" | ||
"tslib": "^1.9.2" | ||
}, | ||
@@ -55,0 +55,0 @@ "main": "./dist/umd/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
69402
87
915
Updatedtslib@^1.9.2