Comparing version 2.1.1 to 2.2.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.container = exports.ContainerImpl = void 0; | ||
const symbols_1 = require("./symbols"); | ||
class ContainerImpl { | ||
@@ -15,3 +16,3 @@ constructor(parentContainer) { | ||
const withInjection = (...injectionParams) => { | ||
implementationType.__injectionParams = injectionParams; | ||
implementationType[symbols_1.injectionSymbol] = injectionParams; | ||
}; | ||
@@ -61,19 +62,52 @@ this.dependencies.set(implementationType, implementationType); | ||
} | ||
if (implementation.__singleton && this.singletons.has(implementation)) { | ||
if (implementation[symbols_1.singletonSymbol] && this.singletons.has(implementation)) { | ||
return this.singletons.get(implementation); | ||
} | ||
const dependencyArguments = []; | ||
if (implementation.__dependencies) { | ||
implementation.__dependencies.forEach((dependencyType) => { | ||
const instance = this.resolve(dependencyType); | ||
dependencyArguments.push(instance); | ||
}); | ||
// const dependencyArguments: any[] = []; | ||
// if (implementation[dependencies]) { | ||
// implementation[dependencies]!.forEach((dependencyType: Constructor | AbstractConstructor) => { | ||
// const instance = this.resolve(dependencyType); | ||
// dependencyArguments.push(instance); | ||
// }); | ||
// } | ||
// | ||
// const injectionParams = implementation[injection] || []; | ||
// | ||
// const target = new implementation(...[ | ||
// ...dependencyArguments, | ||
// ...injectionParams, | ||
// ...args, | ||
// ]); | ||
let injectableArguments = []; | ||
const injectionParams = [ | ||
...(Array.isArray(implementation[symbols_1.injectionSymbol]) ? implementation[symbols_1.injectionSymbol] : []), | ||
...args, | ||
]; | ||
let index = 0; | ||
let injectionParamsIndex = 0; | ||
if (implementation[symbols_1.dependenciesSymbol]) { | ||
let has = true; | ||
const injectableDependencies = implementation[symbols_1.dependenciesSymbol].filter(d => d); | ||
const length = injectableDependencies.length + injectionParams.length; | ||
while (has) { | ||
const dependencyType = implementation[symbols_1.dependenciesSymbol][index]; | ||
if (dependencyType) { | ||
const instance = this.resolve(dependencyType); | ||
injectableArguments[index] = instance; | ||
} | ||
else if (injectionParams[injectionParamsIndex]) { | ||
injectableArguments[index] = injectionParams[injectionParamsIndex]; | ||
injectionParamsIndex++; | ||
} | ||
index++; | ||
if (index >= length) { | ||
has = false; | ||
} | ||
} | ||
} | ||
const injectionParams = implementation.__injectionParams || []; | ||
const target = new implementation(...[ | ||
...dependencyArguments, | ||
...injectionParams, | ||
...args, | ||
]); | ||
if (implementation.__singleton) { | ||
else { | ||
injectableArguments = injectionParams; | ||
} | ||
const target = new implementation(...injectableArguments); | ||
if (implementation[symbols_1.singletonSymbol]) { | ||
this.singletons.set(implementation, target); | ||
@@ -80,0 +114,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.dependencies = void 0; | ||
const symbols_1 = require("./symbols"); | ||
function dependencies(...dependencies) { | ||
return function (constructor) { | ||
var _a; | ||
const decoratedConstructor = (_a = class extends constructor { | ||
var _a, _b; | ||
const decoratedConstructor = (_b = class extends constructor { | ||
}, | ||
_a.__dependencies = dependencies || [], | ||
_a); | ||
_a = symbols_1.dependenciesSymbol, | ||
_b[_a] = dependencies || [], | ||
_b); | ||
decoratedConstructor.className = constructor.name; | ||
@@ -12,0 +14,0 @@ return decoratedConstructor; |
@@ -5,1 +5,2 @@ export * from './ContainerImpl'; | ||
export * from './types'; | ||
export * from './symbols'; |
@@ -8,2 +8,3 @@ "use strict"; | ||
tslib_1.__exportStar(require("./types"), exports); | ||
tslib_1.__exportStar(require("./symbols"), exports); | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.singleton = void 0; | ||
const symbols_1 = require("./symbols"); | ||
function singleton() { | ||
return function (constructor) { | ||
var _a; | ||
const decoratedConstructor = (_a = class extends constructor { | ||
var _a, _b; | ||
const decoratedConstructor = (_b = class extends constructor { | ||
}, | ||
_a.__singleton = true, | ||
_a); | ||
_a = symbols_1.singletonSymbol, | ||
_b[_a] = true, | ||
_b); | ||
decoratedConstructor.className = constructor.name; | ||
@@ -12,0 +14,0 @@ return decoratedConstructor; |
@@ -0,1 +1,2 @@ | ||
import { singletonSymbol, dependenciesSymbol, injectionSymbol } from './symbols'; | ||
declare type AbstractConstructor<T = any> = abstract new (...args: any[]) => T; | ||
@@ -5,7 +6,7 @@ declare type Constructor<T = any> = new (...args: any[]) => T; | ||
declare type ImplementationType<TClass> = Constructor<TClass> & { | ||
__singleton?: boolean; | ||
__dependencies?: Dependency[]; | ||
[singletonSymbol]?: boolean; | ||
[dependenciesSymbol]?: Dependency[]; | ||
}; | ||
declare type ImplementationTypeWithInjection<TInstance> = ImplementationType<TInstance> & { | ||
__injectionParams?: any[]; | ||
[injectionSymbol]?: any[]; | ||
}; | ||
@@ -12,0 +13,0 @@ declare type RegistrationType<TInstance> = Constructor<TInstance> | AbstractConstructor<TInstance>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const symbols_1 = require("./symbols"); | ||
//# sourceMappingURL=types.js.map |
{ | ||
"name": "cheap-di", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "JavaScript dependency injection like Autofac in .Net", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -83,2 +83,3 @@ # cheap-di | ||
```js | ||
import { dependencies } from 'cheap-di'; | ||
import { Logger } from './logger'; | ||
@@ -88,3 +89,3 @@ import { UserRepository } from './user-repository'; | ||
export class UserService { | ||
static __dependencies = [ UserRepository, Logger ]; | ||
static [dependencies] = [ UserRepository, Logger ]; | ||
@@ -170,3 +171,3 @@ constructor(userRepository, logger) { | ||
private userRepository: UserRepository, | ||
private logger: Logger | ||
private logger: Logger, | ||
) { | ||
@@ -186,2 +187,33 @@ } | ||
`@inject` decorator can be used instead of `@dependencies` like below: | ||
```ts | ||
import { inject } from 'cheap-di'; | ||
export class UserService { | ||
constructor( | ||
@inject(UserRepository) private userRepository: UserRepository, | ||
@inject(Logger) private logger: Logger, | ||
) { | ||
} | ||
... | ||
} | ||
``` | ||
This approach allows you to mix dependency with injection params with any order: | ||
```ts | ||
class Service { | ||
constructor( | ||
message1: string, | ||
@inject(Repository) public repository: Repository, | ||
message2: string, | ||
@inject(Database) public db: Database, | ||
) { | ||
} | ||
} | ||
const message1 = '123'; | ||
const message2 = '456'; | ||
container.registerType(Service).with(message1, message2); | ||
``` | ||
`@singleton` decorator allows you to inject the same instance everywhere. | ||
@@ -212,3 +244,5 @@ | ||
You can see more examples in `cheap-di/src/ContainerImpl.test.ts` | ||
You can see more examples in `cheap-di/src/container.test.ts` | ||
[Releases](./RELEASES.md) |
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
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
25262
24
269
244