Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
feat: allow non singleton factories
Browse files Browse the repository at this point in the history
  • Loading branch information
KnisterPeter committed Dec 3, 2018
1 parent 17e30fd commit a05bd30
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
26 changes: 24 additions & 2 deletions lib/compiler/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,27 @@ export class Compiler {
await Promise.all(
unit.members.map(async member => {
if (ts.isMethodDeclaration(member)) {
if (hasDecorator('provides', member)) {
const provides = getDecorator('provides', member);
if (provides) {
// -- is singleton?
const singleton = (() => {
const parameters = getDecoratorParameters(provides);
if (parameters.length > 0) {
const config = parameters[0];
if (!ts.isObjectLiteralExpression(config)) {
throw new Error('Invalid @provides decorator');
}

const singleton = getValueFromObjectLiteral(
config,
'singleton'
);
return singleton.kind !== ts.SyntaxKind.FalseKeyword;
}
return true;
})();
// /- is singleton?

const parameterTypes = await this.getMethodParameterTypes(
member
);
Expand All @@ -169,7 +189,9 @@ export class Compiler {
type: returnType,
constructorDependencies: [],
propertyDependencies: [],
meta: {}
meta: {
singleton
}
};
components.set(returnType, component);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/compiler/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ export function managed(
//
}

export function meta(): ClassDecorator {
export function meta(_options: { singleton?: boolean }): ClassDecorator {
return () => undefined;
}

export function provides(_target: any, _prop: any): void {
//
export function provides(_option?: { singleton?: boolean }): MethodDecorator {
return () => undefined;
}

export function unit(_target: any): void {
Expand Down
6 changes: 5 additions & 1 deletion lib/compiler/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ export class Generator {
}
let provider = '';
const meta = `{
singleton: ${Boolean(component.meta.singleton)}
singleton: ${
typeof component.meta.singleton === 'boolean'
? component.meta.singleton
: true
}
}`;
if (component.provider) {
provider = `
Expand Down
32 changes: 32 additions & 0 deletions tests/compiler/compiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,36 @@ describe('TSDI compiler', () => {

await testContainer(code, files, expect);
});

it('supports factories which produces non singletons', async () => {
const files = {
'/file.ts': `
import { container, unit, provides } from '/decorators';
export class Entry {
}
@unit
export class Unit {
@provides({singleton: false})
public entry(): Entry {
return new Entry();
}
}
@container({ units: [Unit] })
export abstract class Container {
public abstract entry: Entry;
public test(expect): void {
expect(this.entry).not.toBe(this.entry);
}
}
`
};

const code = await runCompiler(files);

await testContainer(code, files, expect);
});
});

0 comments on commit a05bd30

Please sign in to comment.