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

Commit

Permalink
feat: add classic scope, singleton and lifecycles
Browse files Browse the repository at this point in the history
  • Loading branch information
KnisterPeter committed Dec 3, 2018
1 parent 1c3cdd6 commit f9117dd
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
35 changes: 35 additions & 0 deletions lib/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,41 @@ export class Component {
}

export class ClassicComponent extends Component {
public get meta(): {
singleton?: boolean;
scope?: string;
} {
const meta: {
singleton?: boolean;
scope?: string;
} = {
singleton: undefined,
scope: undefined
};

const component = getDecorator('component', this.type);
if (component) {
const parameters = getDecoratorParameters(component);
if (
parameters.length > 0 &&
ts.isObjectLiteralExpression(parameters[0])
) {
const config = parameters[0] as ts.ObjectLiteralExpression;

meta.singleton = isSingleton(config);

const scopeNode = getValueFromObjectLiteral(config, 'scope');
if (scopeNode) {
if (ts.isStringLiteral(scopeNode)) {
meta.scope = scopeNode.text;
}
}
}
}

return meta;
}

public get propertyDependencies(): {
property: string;
type: ts.ClassDeclaration;
Expand Down
45 changes: 44 additions & 1 deletion tests/compiler/compiler.compatibility.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,34 @@ test.only('TSDI compiler generates configuration for runtime components', async
const files: { [name: string]: string } = {
'/file.ts': `
import { container } from '/decorators';
import { component, inject } from 'tsdi';
import { component, inject, initialize, destroy } from 'tsdi';
let initCalled = false;
let disposeCalled = false;
@component({scope: 'scope'})
export class ComponentWithScopeAndLifecycle {
@initialize
protected init(): void {
initCalled = true;
disposeCalled = true;
}
@destroy
protected dispose(): void {
disposeCalled = true;
}
public call(): void {
//
}
}
@component({singleton: false})
export class NonSingleton {
}
@component
export class Dependency {
Expand All @@ -21,10 +48,26 @@ test.only('TSDI compiler generates configuration for runtime components', async
@container({ units: [] })
export abstract class Container {
public abstract component: Component;
public abstract dependency: Dependency;
public abstract scoped: ComponentWithScopeAndLifecycle;
public abstract nonSingleton: NonSingleton;
public getTSDI(): TSDI {
return this.tsdi;
}
}
export function test(expect, container) {
expect(container.component).toBeInstanceOf(Component);
expect(container.component.dependency).toBe(container.dependency);
expect(() => container.scoped.call()).toThrow();
container.getTSDI().getScope('scope').enter();
container.scoped.call()
container.getTSDI().getScope('scope').leave();
expect(container.nonSingleton).not.toBe(container.nonSingleton);
}
`
};
Expand Down

0 comments on commit f9117dd

Please sign in to comment.