dependency-injection-cat
Advanced tools
Comparing version 0.2.10 to 0.2.11-dev
# Changelog | ||
## 0.2.11 | ||
- [Feature] Added Context Lifecycle methods **@PostConstruct** and **@BeforeDestruct** decorators | ||
## 0.2.10 | ||
@@ -4,0 +8,0 @@ |
@@ -10,3 +10,3 @@ "use strict"; | ||
var CompilationContext_1 = require("../../compilation-context/CompilationContext"); | ||
function getQualifierValueFromFunctionArgument(parameter, beanDescriptor) { | ||
function getQualifierValueFromFunctionArgument(parameter, contextDescriptor) { | ||
var _a, _b; | ||
@@ -21,4 +21,4 @@ var qualifierDecorators = (_b = (_a = parameter.decorators) === null || _a === void 0 ? void 0 : _a.filter(isParameterQualifierDecorator_1.isParameterQualifierDecorator)) !== null && _b !== void 0 ? _b : []; | ||
message: 'Parameter Qualifier should not have more than 1 @Qualifier decorator', | ||
filePath: beanDescriptor.contextDescriptor.absolutePath, | ||
relatedContextPath: beanDescriptor.contextDescriptor.absolutePath, | ||
filePath: contextDescriptor.absolutePath, | ||
relatedContextPath: contextDescriptor.absolutePath, | ||
}); | ||
@@ -32,4 +32,4 @@ return null; | ||
message: 'You should call @Qualifier with string, when decorating parameter', | ||
filePath: beanDescriptor.contextDescriptor.absolutePath, | ||
relatedContextPath: beanDescriptor.contextDescriptor.absolutePath, | ||
filePath: contextDescriptor.absolutePath, | ||
relatedContextPath: contextDescriptor.absolutePath, | ||
}); | ||
@@ -44,4 +44,4 @@ return null; | ||
message: '@Qualifier should have only 1 argument', | ||
filePath: beanDescriptor.contextDescriptor.absolutePath, | ||
relatedContextPath: beanDescriptor.contextDescriptor.absolutePath, | ||
filePath: contextDescriptor.absolutePath, | ||
relatedContextPath: contextDescriptor.absolutePath, | ||
}); | ||
@@ -54,4 +54,4 @@ return null; | ||
message: '@Qualifier should have only 1 argument', | ||
filePath: beanDescriptor.contextDescriptor.absolutePath, | ||
relatedContextPath: beanDescriptor.contextDescriptor.absolutePath, | ||
filePath: contextDescriptor.absolutePath, | ||
relatedContextPath: contextDescriptor.absolutePath, | ||
}); | ||
@@ -65,4 +65,4 @@ return null; | ||
message: 'Qualifier should be a string literal', | ||
filePath: beanDescriptor.contextDescriptor.absolutePath, | ||
relatedContextPath: beanDescriptor.contextDescriptor.absolutePath, | ||
filePath: contextDescriptor.absolutePath, | ||
relatedContextPath: contextDescriptor.absolutePath, | ||
}); | ||
@@ -69,0 +69,0 @@ return null; |
@@ -11,3 +11,3 @@ "use strict"; | ||
parameters.forEach(function (parameter) { | ||
var qualifier = getQualifierValueFromFunctionArgument_1.getQualifierValueFromFunctionArgument(parameter, descriptor); | ||
var qualifier = getQualifierValueFromFunctionArgument_1.getQualifierValueFromFunctionArgument(parameter, descriptor.contextDescriptor); | ||
var type = getParameterType_1.getParameterType(parameter); | ||
@@ -14,0 +14,0 @@ if (type === null) { |
@@ -11,3 +11,3 @@ "use strict"; | ||
parameters.forEach(function (parameter) { | ||
var qualifier = getQualifierValueFromFunctionArgument_1.getQualifierValueFromFunctionArgument(parameter, descriptor); | ||
var qualifier = getQualifierValueFromFunctionArgument_1.getQualifierValueFromFunctionArgument(parameter, descriptor.contextDescriptor); | ||
var type = getParameterType_1.getParameterType(parameter); | ||
@@ -14,0 +14,0 @@ if (type === null) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.restrictedBeanNames = void 0; | ||
exports.restrictedBeanNames = ['getBean', 'getBean', 'config', 'getPrivateBean', 'getBeanConfiguration']; | ||
exports.restrictedClassMemberNames = void 0; | ||
exports.restrictedClassMemberNames = [ | ||
'getBean', | ||
'config', | ||
'getPrivateBean', | ||
'getBeanConfiguration', | ||
'___postConstruct', | ||
'___beforeDestruct', | ||
]; |
@@ -22,3 +22,3 @@ "use strict"; | ||
var classElementName = classElement.name.getText(); | ||
if (constants_1.restrictedBeanNames.includes(classElementName)) { | ||
if (constants_1.restrictedClassMemberNames.includes(classElementName)) { | ||
CompilationContext_1.CompilationContext.reportError({ | ||
@@ -25,0 +25,0 @@ node: classElement, |
@@ -22,3 +22,3 @@ "use strict"; | ||
var classElementName = classElement.name.getText(); | ||
if (constants_1.restrictedBeanNames.includes(classElementName)) { | ||
if (constants_1.restrictedClassMemberNames.includes(classElementName)) { | ||
CompilationContext_1.CompilationContext.reportError({ | ||
@@ -25,0 +25,0 @@ node: classElement, |
@@ -22,3 +22,3 @@ "use strict"; | ||
var classElementName = classElement.name.getText(); | ||
if (constants_1.restrictedBeanNames.includes(classElementName)) { | ||
if (constants_1.restrictedClassMemberNames.includes(classElementName)) { | ||
CompilationContext_1.CompilationContext.reportError({ | ||
@@ -25,0 +25,0 @@ node: classElement, |
@@ -45,3 +45,3 @@ "use strict"; | ||
var classElementName = classElement.name.getText(); | ||
if (constants_1.restrictedBeanNames.includes(classElementName)) { | ||
if (constants_1.restrictedClassMemberNames.includes(classElementName)) { | ||
CompilationContext_1.CompilationContext.reportError({ | ||
@@ -48,0 +48,0 @@ node: classElement, |
@@ -26,2 +26,6 @@ "use strict"; | ||
var transformExpressionBeans_1 = require("./transformers/transformExpressionBeans"); | ||
var registerContextLifecycleMethods_1 = require("../context-lifecycle/registerContextLifecycleMethods"); | ||
var transformLifecycleMethods_1 = require("./transformers/transformLifecycleMethods"); | ||
var transformLifecycleArrowFunctions_1 = require("./transformers/transformLifecycleArrowFunctions"); | ||
var addLifecycleConfiguration_1 = require("./transformers/addLifecycleConfiguration"); | ||
function registerAndTransformContext(context, sourceFile) { | ||
@@ -40,2 +44,3 @@ var _a, _b; | ||
buildDependencyGraphAndFillQualifiedBeans_1.buildDependencyGraphAndFillQualifiedBeans(newGlobalContextDescriptor); | ||
registerContextLifecycleMethods_1.registerContextLifecycleMethods(newGlobalContextDescriptor); | ||
reportAboutCyclicDependencies_1.reportAboutCyclicDependencies(newGlobalContextDescriptor); | ||
@@ -65,5 +70,7 @@ var contextDescriptorToIdentifierList_1 = []; | ||
buildDependencyGraphAndFillQualifiedBeans_1.buildDependencyGraphAndFillQualifiedBeans(contextDescriptor); | ||
registerContextLifecycleMethods_1.registerContextLifecycleMethods(contextDescriptor); | ||
reportAboutCyclicDependencies_1.reportAboutCyclicDependencies(contextDescriptor); | ||
var contextDescriptorToIdentifierList = []; | ||
var transformers = [ | ||
addLifecycleConfiguration_1.addLifecycleConfiguration(contextDescriptor), | ||
addContextPool_1.addContextPool(contextDescriptor), | ||
@@ -75,2 +82,4 @@ replaceExtendingFromCatContext_1.replaceExtendingFromCatContext(contextDescriptor), | ||
transformExpressionBeans_1.transformExpressionBeans(), | ||
transformLifecycleMethods_1.transformLifecycleMethods(contextDescriptorToIdentifierList), | ||
transformLifecycleArrowFunctions_1.transformLifecycleArrowFunctions(contextDescriptorToIdentifierList), | ||
addNecessaryImports_1.addNecessaryImports(contextDescriptorToIdentifierList), | ||
@@ -77,0 +86,0 @@ ]; |
@@ -48,2 +48,3 @@ "use strict"; | ||
var getBeanConfigObjectLiteral_1 = require("./getBeanConfigObjectLiteral"); | ||
var addLifecycleConfiguration_1 = require("./addLifecycleConfiguration"); | ||
exports.CONTEXT_POOL_POSTFIX = "_POOL" + constants_1.PRIVATE_TOKEN; | ||
@@ -62,4 +63,5 @@ var addContextPool = function (contextDescriptor) { | ||
getBeanConfigObjectLiteral_1.getBeanConfigObjectLiteral(contextDescriptor), | ||
typescript_1.factory.createIdentifier(addLifecycleConfiguration_1.LIFECYCLE_CONFIG_VARIABLE_NAME), | ||
contextDescriptor.node.name, | ||
]))], typescript_1.default.NodeFlags.Const)); | ||
} |
@@ -59,3 +59,7 @@ "use strict"; | ||
getBeanConfigObjectLiteral_1.getBeanConfigObjectLiteral(contextDescriptor), | ||
typescript_1.factory.createObjectLiteralExpression([ | ||
typescript_1.factory.createPropertyAssignment(typescript_1.factory.createStringLiteral('post-construct'), typescript_1.factory.createArrayLiteralExpression([], false)), | ||
typescript_1.factory.createPropertyAssignment(typescript_1.factory.createStringLiteral('before-destruct'), typescript_1.factory.createArrayLiteralExpression([], false)) | ||
], true), | ||
]))], typescript_1.default.NodeFlags.Const)); | ||
} |
@@ -37,3 +37,3 @@ "use strict"; | ||
node: node, | ||
message: 'Property Bean should hold value', | ||
message: 'Arrow function Bean should be initialized', | ||
filePath: node.getSourceFile().fileName, | ||
@@ -40,0 +40,0 @@ }); |
@@ -0,1 +1,4 @@ | ||
export declare type TBeanName = string; | ||
export declare type TLifecycle = "post-construct" | "before-destruct"; | ||
export declare type TLifecycleConfiguration = Record<TLifecycle, TBeanName[]>; | ||
export interface IFullBeanConfig { | ||
@@ -9,5 +12,7 @@ scope?: "prototype" | "singleton"; | ||
getBeans(): Record<string, any>; | ||
___postConstruct(): void; | ||
___beforeDestruct(): void; | ||
} | ||
export declare type TInternalCatContext = { | ||
new (contextName: string, beanConfigurationRecord: Record<string, IFullBeanConfig>): IInternalCatContext; | ||
new (contextName: string, beanConfigurationRecord: Record<string, IFullBeanConfig>, lifecycleConfiguration: TLifecycleConfiguration): IInternalCatContext; | ||
}; | ||
@@ -22,6 +27,7 @@ export interface IContextProps { | ||
private beanConfigurationRecord; | ||
private lifecycleConfiguration; | ||
private context; | ||
private DEFAULT_CONTEXT_KEY; | ||
private pool; | ||
constructor(contextName: string, beanConfigurationRecord: Record<TBeanName, IFullBeanConfig>, context: TInternalCatContext); | ||
constructor(contextName: string, beanConfigurationRecord: Record<TBeanName, IFullBeanConfig>, lifecycleConfiguration: TLifecycleConfiguration, context: TInternalCatContext); | ||
initContext({ key, config, }: IContextProps): any; | ||
@@ -28,0 +34,0 @@ getContext({ key }: IContextProps): any; |
@@ -6,5 +6,6 @@ "use strict"; | ||
var ContextPool = /** @class */ (function () { | ||
function ContextPool(contextName, beanConfigurationRecord, context) { | ||
function ContextPool(contextName, beanConfigurationRecord, lifecycleConfiguration, context) { | ||
this.contextName = contextName; | ||
this.beanConfigurationRecord = beanConfigurationRecord; | ||
this.lifecycleConfiguration = lifecycleConfiguration; | ||
this.context = context; | ||
@@ -16,5 +17,6 @@ this.DEFAULT_CONTEXT_KEY = {}; | ||
var _b = _a.key, key = _b === void 0 ? this.DEFAULT_CONTEXT_KEY : _b, config = _a.config; | ||
var newContext = new this.context(this.contextName, this.beanConfigurationRecord); | ||
var newContext = new this.context(this.contextName, this.beanConfigurationRecord, this.lifecycleConfiguration); | ||
newContext.config = config; | ||
this.pool.set(key, newContext); | ||
newContext.___postConstruct(); | ||
return newContext; | ||
@@ -36,10 +38,11 @@ }; | ||
} | ||
var newContext = new this.context(this.contextName, this.beanConfigurationRecord); | ||
newContext.config = config; | ||
this.pool.set(key, newContext); | ||
return newContext; | ||
return this.initContext({ | ||
key: key, | ||
config: config, | ||
}); | ||
}; | ||
ContextPool.prototype.clearContext = function (_a) { | ||
var _b = _a.key, key = _b === void 0 ? this.DEFAULT_CONTEXT_KEY : _b; | ||
if (!this.pool.has(key)) { | ||
var contextInstance = this.pool.get(key); | ||
if (!contextInstance) { | ||
if (this.isDefaultKey(key)) { | ||
@@ -52,2 +55,3 @@ console.warn("Trying to clear not initialized context, contextName: " + this.contextName); | ||
} | ||
contextInstance === null || contextInstance === void 0 ? void 0 : contextInstance.___beforeDestruct(); | ||
this.pool.delete(key); | ||
@@ -54,0 +58,0 @@ }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Qualifier = exports.Bean = void 0; | ||
exports.BeforeDestruct = exports.PostConstruct = exports.Qualifier = exports.Bean = void 0; | ||
var Bean_1 = require("./Bean"); | ||
@@ -8,1 +8,5 @@ Object.defineProperty(exports, "Bean", { enumerable: true, get: function () { return Bean_1.Bean; } }); | ||
Object.defineProperty(exports, "Qualifier", { enumerable: true, get: function () { return Qualifier_1.Qualifier; } }); | ||
var PostConstruct_1 = require("./PostConstruct"); | ||
Object.defineProperty(exports, "PostConstruct", { enumerable: true, get: function () { return PostConstruct_1.PostConstruct; } }); | ||
var BeforeDesctruct_1 = require("./BeforeDesctruct"); | ||
Object.defineProperty(exports, "BeforeDestruct", { enumerable: true, get: function () { return BeforeDesctruct_1.BeforeDestruct; } }); |
@@ -9,9 +9,16 @@ export interface IFullBeanConfig { | ||
getBeans(): Record<string, any>; | ||
___postConstruct(): void; | ||
___beforeDestruct(): void; | ||
} | ||
export declare type TBeanName = string; | ||
export declare type TLifecycle = "post-construct" | "before-destruct"; | ||
export declare type TLifecycleConfiguration = Record<TLifecycle, TBeanName[]>; | ||
export declare abstract class InternalCatContext implements IInternalCatContext { | ||
private contextName; | ||
private beanConfigurationRecord; | ||
private lifecycleConfiguration; | ||
[beanName: string]: any; | ||
constructor(contextName: string, beanConfigurationRecord: Record<TBeanName, IFullBeanConfig>); | ||
constructor(contextName: string, beanConfigurationRecord: Record<TBeanName, IFullBeanConfig>, lifecycleConfiguration: TLifecycleConfiguration); | ||
___postConstruct(): void; | ||
___beforeDestruct(): void; | ||
private singletonMap; | ||
@@ -18,0 +25,0 @@ private _config; |
@@ -18,8 +18,21 @@ "use strict"; | ||
var InternalCatContext = /** @class */ (function () { | ||
function InternalCatContext(contextName, beanConfigurationRecord) { | ||
function InternalCatContext(contextName, beanConfigurationRecord, lifecycleConfiguration) { | ||
this.contextName = contextName; | ||
this.beanConfigurationRecord = beanConfigurationRecord; | ||
this.lifecycleConfiguration = lifecycleConfiguration; | ||
this.singletonMap = new Map(); | ||
this._config = 'UNINITIALIZED_CONFIG'; | ||
} | ||
InternalCatContext.prototype.___postConstruct = function () { | ||
var _this = this; | ||
this.lifecycleConfiguration['post-construct'].forEach(function (methodName) { | ||
_this[methodName](); | ||
}); | ||
}; | ||
InternalCatContext.prototype.___beforeDestruct = function () { | ||
var _this = this; | ||
this.lifecycleConfiguration['before-destruct'].forEach(function (methodName) { | ||
_this[methodName](); | ||
}); | ||
}; | ||
Object.defineProperty(InternalCatContext.prototype, "config", { | ||
@@ -26,0 +39,0 @@ get: function () { |
@@ -52,3 +52,7 @@ export interface IDIContext<TBeans> { | ||
export declare const Qualifier: <T extends string>(beanName: T extends "" ? never : T) => ParameterDecorator; | ||
export declare type TPostConstruct = PropertyDecorator & MethodDecorator; | ||
export declare const PostConstruct: TPostConstruct; | ||
export declare type TBeforeDestruct = PropertyDecorator & MethodDecorator; | ||
export declare const BeforeDestruct: TBeforeDestruct; | ||
export {}; |
{ | ||
"name": "dependency-injection-cat", | ||
"version": "0.2.10", | ||
"version": "0.2.11-dev", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "types": "index.d.ts", |
@@ -6,5 +6,3 @@ # Dependency Injection Cat | ||
----- | ||
![npm](https://img.shields.io/npm/v/dependency-injection-cat?style=flat) | ||
[![npm](https://img.shields.io/npm/v/dependency-injection-cat?style=flat)](https://www.npmjs.com/package/dependency-injection-cat) | ||
----- | ||
@@ -16,3 +14,3 @@ | ||
//ApplicationContext.di.ts | ||
export class ApplicationContext extends CatContext<IBeans> { | ||
class ApplicationContext extends CatContext<IBeans> { | ||
useCase = Bean<IUseCase>(UseCase) | ||
@@ -309,3 +307,3 @@ mobxRepository = Bean<IRepository>(MobxRepository) | ||
//GlobalApplicationContext.di.ts | ||
export class GlobalApplicationContext extends GlobalCatContext { | ||
class GlobalApplicationContext extends GlobalCatContext { | ||
@@ -319,3 +317,3 @@ @Bean | ||
//ApplicationContext.di.ts | ||
export class AppContext extends CatContext<IBeans> { | ||
class AppContext extends CatContext<IBeans> { | ||
@@ -509,3 +507,3 @@ @Bean | ||
```typescript | ||
export class ApplicationContext extends CatContext<IBeans> { | ||
class ApplicationContext extends CatContext<IBeans> { | ||
//First argument passed in Bean should be a class, di-container will try to resolve class dependencies | ||
@@ -554,3 +552,3 @@ useCase = Bean(UseCaseClass); | ||
export class ApplicationContext extends CatContext<IBeans> { | ||
class ApplicationContext extends CatContext<IBeans> { | ||
useCaseDependency = Bean<IUseCaseDependency>(UseCaseDependency); | ||
@@ -574,3 +572,3 @@ | ||
```typescript | ||
export class ApplicationContext extends CatContext<IBeans> { | ||
class ApplicationContext extends CatContext<IBeans> { | ||
//Bean don't have any dependencies | ||
@@ -611,3 +609,3 @@ @Bean | ||
```typescript | ||
export class ApplicationContext extends CatContext<IBeans> { | ||
class ApplicationContext extends CatContext<IBeans> { | ||
//Bean don't have any dependencies | ||
@@ -647,3 +645,3 @@ @Bean useCase = (): IUseCase => new UseCase(); | ||
```typescript | ||
export class ApplicationContext extends CatContext<IBeans> { | ||
class ApplicationContext extends CatContext<IBeans> { | ||
@Bean useCase: IUseCase = new UseCase(); | ||
@@ -657,4 +655,34 @@ @Bean importedInstanceOfUseCase: IUseCase = importedInstanceOfUseCase; | ||
## Qualifier | ||
## Context Lifecycle | ||
Context have its own lifecycle, when you're initializing context with `container.initContext` or `container.getOrInitContext` | ||
and when you're clearing context with `container.clearContext` | ||
If you need to make some subscriptions/unsubscription/side effects - you can use **@PostConstruct** and **@BeforeDestruct** decorators | ||
One method can be decorated with **@PostConstruct** and **@BeforeDestruct** at the same time | ||
### PostConstruct, BeforeDestruct | ||
PostConstruct decorator allows you to call method right after context initialization (after calling `container.initContext`), it can be used to make some subscriptions, etc... | ||
BeforeDestruct decorator allows you to call method right before context clearing (after calling `container.clearContext`), it can be used to make some unsubscription, etc... | ||
You can add arguments to annotated method, beans will be injected instead of arguments | ||
You can have several Lifecycle methods, they will be called in order of declaring | ||
#### Syntax | ||
```typescript | ||
import { Bean, PostConstruct, BeforeDestruct, CatContext } from 'dependency-injection-cat'; | ||
class ApplicationContext extends CatContext<IBeans> { | ||
@PostConstruct /* OR / AND */ @BeforeDestruct | ||
subscribeToEvents(subscriber: ISubscriber): void { | ||
subscriber.subscribe(); | ||
} | ||
//OR | ||
@PostConstruct /* OR / AND */ @BeforeDestruct | ||
subscribeToEvents = (subscriber: ISubscriber): void => subscriber.subscribe(); | ||
@Bean | ||
subscriber: ISubscriber = Bean(Subscriber); | ||
} | ||
``` | ||
### Qualifier | ||
Qualifier needed, when you have 2 or more **Beans** in the **Context** with same type. By default, the qualifier is the | ||
@@ -674,3 +702,3 @@ name of the parameter in the class constructor, or in the **Method Bean**` | ||
//Implicit declaration of the qualifier | ||
export class ApplicationContext extends CatContext<IBeans> { | ||
class ApplicationContext extends CatContext<IBeans> { | ||
httpRequester: IRequester = Bean(HttpRequester); | ||
@@ -690,3 +718,3 @@ graphQLRequester: IRequester = Bean(GraphQLRequester); | ||
//When Bean placed in current context | ||
export class ApplicationContext extends CatContext<IBeans> { | ||
class ApplicationContext extends CatContext<IBeans> { | ||
httpRequester: IRequester = Bean(HttpRequester); | ||
@@ -708,3 +736,3 @@ graphQLRequester: IRequester = Bean(GraphQLRequester); | ||
//GlobalApplicationContext.di.ts | ||
export class GlobalApplicationContext extends GlobalCatContext { | ||
class GlobalApplicationContext extends GlobalCatContext { | ||
graphQLRequester: IRequester = Bean(GraphQLRequester); | ||
@@ -715,3 +743,3 @@ httpRequester: IRequester = Bean(HttpRequester); | ||
//ApplicationContext.di.ts | ||
export class ApplicationContext extends CatContext<IBeans> { | ||
class ApplicationContext extends CatContext<IBeans> { | ||
@Bean | ||
@@ -718,0 +746,0 @@ useCase( |
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
304808
148
5622
740