Skip to content

Commit

Permalink
Fix OnlyInstantiableByContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiago Bustamante committed Feb 28, 2020
1 parent 980b9f8 commit 089b685
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-ioc",
"version": "3.0.2",
"version": "3.0.3",
"description": "A Lightweight annotation-based dependency injection container for typescript.",
"author": "Thiago da Rosa de Bustamante <[email protected]>",
"scripts": {
Expand Down
11 changes: 6 additions & 5 deletions src/container/container-binding-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ export class IoCBindConfig implements Config {

public factory(factory: ObjectFactory) {
this.iocFactory = (context) => {
InjectorHandler.unblockInstantiation();
const instance = factory(context);
InjectorHandler.injectContext(instance, context);
InjectorHandler.blockInstantiation();
return instance;
};
if (this.iocScope) {
Expand Down Expand Up @@ -82,15 +84,14 @@ export class IoCBindConfig implements Config {
if (!this.iocScope) {
this.scope(Scope.Local);
}
InjectorHandler.unblockInstantiation();
const instance = this.iocScope.resolve(this.iocFactory, this.source, context);
InjectorHandler.blockInstantiation();
return instance;
return this.iocScope.resolve(this.iocFactory, this.source, context);
}

private getParameters(context: BuildContext) {
if (this.paramTypes) {
return this.paramTypes.map(paramType => this.instanceFactory(paramType, context));
const params = this.paramTypes.map(paramType => this.instanceFactory(paramType, context));
InjectorHandler.unblockInstantiation();
return params;
}
return null;
}
Expand Down
5 changes: 3 additions & 2 deletions test/unit/container/container-binding-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ describe('IoCBindConfig', () => {
const factory = jest.fn().mockReturnValue(instance);
expect(bindConfig.factory(factory)).toEqual(bindConfig);
expect(bindConfig.iocFactory(context)).toEqual(instance);
expect(mockInjectorUnBlockInstantiation).toBeCalled();
expect(factory).toBeCalledWith(context);
expect(mockInjectorInjectContext).toBeCalledWith(instance, context);
expect(mockInjectorBlockInstantiation).toBeCalled();
});

it('should call scope.reset after changing the factory', () => {
Expand Down Expand Up @@ -141,9 +143,7 @@ describe('IoCBindConfig', () => {
bindConfig.instrumentConstructor();

expect(bindConfig.getInstance(buildContext)).toEqual(instance);
expect(mockInjectorBlockInstantiation).toBeCalled();
expect(bindConfig.iocScope.resolve).toBeCalledWith(bindConfig.iocFactory, MyBaseType, buildContext);
expect(mockInjectorUnBlockInstantiation).toBeCalledWith();
});
});

Expand Down Expand Up @@ -194,6 +194,7 @@ describe('IoCBindConfig', () => {
expect(newInstance.date).toBeDefined();
expect(mockInstanceFactory).toBeCalledWith(Date, buildContext);
expect(mockInjectorInjectContext).toBeCalledWith(MyType, buildContext);
expect(mockInjectorUnBlockInstantiation).toBeCalled();
expect(mockInjectorRemoveContext).toBeCalledWith(MyType);
});

Expand Down

0 comments on commit 089b685

Please sign in to comment.