This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow scoped selected container configuration
- Loading branch information
1 parent
ad3a7b5
commit 520d6be
Showing
8 changed files
with
294 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// tslint:disable: no-implicit-dependencies | ||
import { assert } from 'chai'; | ||
import { TSDI, configure, component, external, inject } from '..'; | ||
|
||
class Component1 { | ||
// | ||
} | ||
|
||
class Component2 { | ||
// tslint:disable-next-line: no-parameter-properties | ||
constructor(public comp1: Component1, public tsdi: TSDI) { | ||
// | ||
} | ||
} | ||
|
||
@component | ||
class Component3 { | ||
@inject | ||
public comp2!: Component2; | ||
} | ||
|
||
@external | ||
class Component4 { | ||
@inject | ||
public comp3!: Component3; | ||
} | ||
|
||
class Container { | ||
@configure | ||
public comp3!: Component3; | ||
|
||
@configure | ||
public comp4!: Component4; | ||
|
||
@configure | ||
public comp1(): Component1 { | ||
return new Component1(); | ||
} | ||
|
||
@configure() | ||
public comp2(tsdi: TSDI): Component2 { | ||
return new Component2(this.comp1(), tsdi); | ||
} | ||
} | ||
|
||
describe('TSDI', () => { | ||
describe('with a configuration setup', () => { | ||
let tsdi: TSDI; | ||
|
||
beforeEach(() => { | ||
tsdi = new TSDI(new Container()); | ||
}); | ||
|
||
it('should create requested components', () => { | ||
const comp = tsdi.get(Component1); | ||
|
||
assert.instanceOf(comp, Component1); | ||
}); | ||
|
||
it('should inject dependencies', () => { | ||
const comp1 = tsdi.get(Component1); | ||
const comp2 = tsdi.get(Component2); | ||
|
||
assert.strictEqual(comp1, comp2.comp1); | ||
}); | ||
|
||
it('should support singletons', () => { | ||
const a = tsdi.get(Component1); | ||
const b = tsdi.get(Component1); | ||
|
||
assert.strictEqual(a, b); | ||
}); | ||
|
||
it('should inject itself', () => { | ||
const comp = tsdi.get(Component2); | ||
|
||
assert.strictEqual(comp.tsdi, tsdi); | ||
}); | ||
|
||
it('should allow decorated components', () => { | ||
const comp = tsdi.get(Component3); | ||
|
||
assert.strictEqual(comp.comp2.comp1, tsdi.get(Component1)); | ||
}); | ||
|
||
it('should allow external components', () => { | ||
const comp = new Component4(); | ||
|
||
assert.strictEqual(comp.comp3.comp2.comp1, tsdi.get(Component1)); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Constructable, TSDI } from '.'; | ||
|
||
export function Configure(target: Object, property: string | symbol): any; | ||
export function Configure(): any; | ||
export function Configure(...args: any[]): any { | ||
const decorate = ( | ||
target: Record<string, Function>, | ||
property: string | symbol | ||
) => { | ||
Reflect.defineMetadata('component:configured', true, target, property); | ||
|
||
const orig = target[property.toString()]; | ||
const cacheKey = `__tsdi__${property.toString()}__`; | ||
|
||
const methodReturnType = Reflect.getMetadata( | ||
'design:returntype', | ||
target, | ||
property | ||
); | ||
const propertyType = Reflect.getMetadata('design:type', target, property); | ||
|
||
function propertyCreator(this: { | ||
__tsdi__: TSDI; | ||
[cacheKey: string]: any; | ||
}): any { | ||
if (!methodReturnType) { | ||
return this.__tsdi__.get(propertyType); | ||
} | ||
|
||
if (cacheKey in this) { | ||
return this[cacheKey]; | ||
} | ||
|
||
const values = Reflect.getMetadata( | ||
'design:paramtypes', | ||
target, | ||
property | ||
).map((param: Constructable<unknown>) => this.__tsdi__.get(param)); | ||
|
||
const value = orig.call(this, ...values); | ||
Object.defineProperty(this, cacheKey, { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value, | ||
}); | ||
return value; | ||
} | ||
|
||
return { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: propertyCreator, | ||
}; | ||
}; | ||
if (args.length > 0) { | ||
return decorate(args[0], args[1]); | ||
} | ||
return (target: Object, property: string | symbol) => { | ||
return decorate(target as any, property) as any; | ||
}; | ||
} | ||
|
||
export { Configure as configure }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.