Initializable decorator improvements

This commit is contained in:
sanex3339
2018-02-09 01:55:40 +03:00
parent e44c160286
commit eb68e7ea57
4 changed files with 61 additions and 9998 deletions
+1 -9988
View File
File diff suppressed because one or more lines are too long
+7 -3
View File
@@ -53,7 +53,7 @@ export function initializable (
* @param metadataValue
* @param {IInitializable} target
*/
function initializeTargetMetadata (metadataKey: string, metadataValue: any, target: IInitializable, ): void {
function initializeTargetMetadata (metadataKey: string, metadataValue: any, target: IInitializable): void {
const hasInitializedMetadata: boolean = Reflect.hasMetadata(metadataKey, target);
if (!hasInitializedMetadata) {
@@ -129,10 +129,14 @@ function wrapInitializeMethodInInitializeCheck (
Object.defineProperty(target, initializeMethodName, {
...methodDescriptor,
value: function (): void {
const result: any = originalMethod.apply(this, arguments);
/**
* should define metadata before `initialize` method call,
* because of cases when other methods will called inside `initialize` method
*/
Reflect.defineMetadata(initializedTargetMetadataKey, true, this);
const result: any = originalMethod.apply(this, arguments);
if (this[propertyKey]) {}
return result;
@@ -46,8 +46,7 @@ export class CustomNodeGroupStorage extends MapStorage <ICustomNodeGroup> {
@postConstruct()
public initialize (): void {
this.storage = new Map <string, ICustomNodeGroup>();
this.storageId = this.randomGenerator.getRandomString(6);
super.initialize();
CustomNodeGroupStorage.customNodeGroupsList.forEach((customNodeGroupName: CustomNodeGroup) => {
const customNodeGroup: ICustomNodeGroup = this.customNodeGroupFactory(
@@ -60,7 +59,5 @@ export class CustomNodeGroupStorage extends MapStorage <ICustomNodeGroup> {
this.storage.set(customNodeGroupName, customNodeGroup);
});
console.log(123, this.constructor.name);
}
}
@@ -50,12 +50,61 @@ describe('@initializable', () => {
foo.bar();
};
it('should throws an error if `initialize` method wasn\'t called first', () => {
it('shouldn\'t throw an error if `initialize` method was called first', () => {
assert.doesNotThrow(testFunc, /Class should be initialized/);
});
});
describe('variant #2: `initialize` method wasn\'t called first', () => {
describe('variant #2: other method was called inside `initialize` method with initialization of the property', () => {
const testFunc: () => void = () => {
class Foo {
@initializable()
public property!: string;
public initialize (property: string): void {
this.innerInitialize(property);
}
public innerInitialize (property: string): void {
this.property = property;
}
}
const foo: Foo = new Foo();
foo.initialize('baz');
};
it('shouldn\'t throw an error if other method was called inside `initialize` method', () => {
assert.doesNotThrow(testFunc, /Class should be initialized/);
});
});
describe('variant #3: other method was called inside `initialize` method without initialization of the property', () => {
const testFunc: () => void = () => {
class Foo {
@initializable()
public property!: string;
public initialize (property: string): void {
this.innerInitialize(property);
}
public innerInitialize (property: string): void {
}
}
const foo: Foo = new Foo();
foo.initialize('baz');
};
it('should throws an error if other method was called inside `initialize` method without initialization of the property', () => {
assert.throws(testFunc, /Property `property` is not initialized/);
});
});
describe('variant #4: `initialize` method wasn\'t called first', () => {
const testFunc: () => void = () => {
class Foo {
@initializable()
@@ -79,7 +128,7 @@ describe('@initializable', () => {
});
});
describe('variant #3: `initialize` method wasn\'t called', () => {
describe('variant #5: `initialize` method wasn\'t called', () => {
const testFunc: () => void = () => {
class Foo {
@initializable()