Skip to content

Commit 4708969

Browse files
author
Thiago Bustamante
committed
fix snapshots
1 parent 61e02c8 commit 4708969

File tree

6 files changed

+44
-18
lines changed

6 files changed

+44
-18
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typescript-ioc",
3-
"version": "3.2.0",
3+
"version": "3.2.1",
44
"description": "A Lightweight annotation-based dependency injection container for typescript.",
55
"author": "Thiago da Rosa de Bustamante <[email protected]>",
66
"scripts": {

src/container/container-binding-config.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export class IoCBindConfig implements Config {
1111
public iocScope: Scope;
1212
public decoratedConstructor: FunctionConstructor;
1313
public paramTypes: Array<any>;
14+
public namespace: string;
1415
private instanceFactory: InstanceFactory;
1516
private valueFactory: ValueFactory;
1617

@@ -85,6 +86,16 @@ export class IoCBindConfig implements Config {
8586
return this.iocScope.resolve(this.iocFactory, this.source, context);
8687
}
8788

89+
public clone() {
90+
const result = new IoCBindConfig(this.source, this.instanceFactory, this.valueFactory);
91+
result.iocFactory = this.iocFactory;
92+
result.iocScope = this.iocScope;
93+
result.targetSource = this.targetSource;
94+
result.paramTypes = this.paramTypes;
95+
result.decoratedConstructor = this.decoratedConstructor;
96+
return result;
97+
}
98+
8899
private getParameters(context: BuildContext) {
89100
if (this.paramTypes) {
90101
return this.paramTypes.map(paramType => {
@@ -102,6 +113,7 @@ export class IoCBindConfig implements Config {
102113
export class IoCBindValueConfig implements ValueConfig {
103114
public name: string;
104115
public path: string;
116+
public namespace: string;
105117
private value: any;
106118

107119
constructor(name: string) {
@@ -124,6 +136,13 @@ export class IoCBindValueConfig implements ValueConfig {
124136
}
125137
return this.value;
126138
}
139+
140+
public clone() {
141+
const result = new IoCBindValueConfig(this.name);
142+
result.path = this.path;
143+
result.value = this.value;
144+
return result;
145+
}
127146
}
128147

129148
export class PropertyPath {

src/container/container-namespaces.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import { IoCBindConfig, IoCBindValueConfig } from './container-binding-config';
22

3-
const DEFAULT_NAMESPACE = 'default';
4-
53
export class ContainerNamespaces {
6-
private defaultNamespace = new NamespaceBindings(DEFAULT_NAMESPACE);
4+
private defaultNamespace = new NamespaceBindings(null);
75
private namespaces = new Map<string, NamespaceBindings>();
86
private currentNamespace: NamespaceBindings;
97

10-
public get(type: FunctionConstructor, searchParent: boolean) {
8+
public get(type: FunctionConstructor) {
119
let result: IoCBindConfig;
1210
if (this.currentNamespace) {
1311
result = this.currentNamespace.get(type);
14-
if (result || !searchParent) {
12+
if (result) {
1513
return result;
1614
}
1715
}
@@ -22,11 +20,11 @@ export class ContainerNamespaces {
2220
(this.currentNamespace || this.defaultNamespace).set(type, bindConfig);
2321
}
2422

25-
public getValue(name: string, searchParent: boolean) {
23+
public getValue(name: string) {
2624
let result: IoCBindValueConfig;
2725
if (this.currentNamespace) {
2826
result = this.currentNamespace.getValue(name);
29-
if (result || !searchParent) {
27+
if (result) {
3028
return result;
3129
}
3230
}
@@ -38,7 +36,7 @@ export class ContainerNamespaces {
3836
}
3937

4038
public selectNamespace(name: string) {
41-
if (name && name !== DEFAULT_NAMESPACE) {
39+
if (name) {
4240
let namespace = this.namespaces.get(name);
4341
if (!namespace) {
4442
namespace = new NamespaceBindings(name);
@@ -81,6 +79,7 @@ class NamespaceBindings {
8179
}
8280

8381
public set(type: FunctionConstructor, bindConfig: IoCBindConfig) {
82+
bindConfig.namespace = this.name;
8483
this.bindings.set(type, bindConfig);
8584
}
8685

@@ -89,6 +88,7 @@ class NamespaceBindings {
8988
}
9089

9190
public setValue(name: string, bindConfig: IoCBindValueConfig) {
91+
bindConfig.namespace = this.name;
9292
this.values.set(name, bindConfig);
9393
}
9494

src/container/container.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,34 @@ import { ContainerNamespaces } from './container-namespaces';
77
* Internal implementation of IoC Container.
88
*/
99
export class IoCContainer {
10-
public static bind(source: Function, searchParent: boolean = false): IoCBindConfig {
10+
public static bind(source: Function, readOnly: boolean = false): IoCBindConfig {
1111
InjectorHandler.checkType(source);
1212
const baseSource = InjectorHandler.getConstructorFromType(source);
13-
let config: IoCBindConfig = IoCContainer.namespaces.get(baseSource, searchParent);
13+
let config: IoCBindConfig = IoCContainer.namespaces.get(baseSource);
1414
if (!config) {
1515
config = new IoCBindConfig(baseSource, IoCContainer.get, IoCContainer.getValue);
1616
config
1717
.to(source as FunctionConstructor);
1818
IoCContainer.namespaces.set(baseSource, config);
19+
} else if (!readOnly && config.namespace !== IoCContainer.namespaces.selectedNamespace()) {
20+
config = config.clone();
21+
IoCContainer.namespaces.set(baseSource, config);
1922
}
2023
return config;
2124
}
2225

23-
public static bindName(name: string, searchParent: boolean = false): IoCBindValueConfig {
26+
public static bindName(name: string, readOnly: boolean = false): IoCBindValueConfig {
2427
InjectorHandler.checkName(name);
2528
const property = PropertyPath.parse(name);
26-
let config = IoCContainer.namespaces.getValue(property.name, searchParent);
29+
let config = IoCContainer.namespaces.getValue(property.name);
2730
if (!config) {
2831
config = new IoCBindValueConfig(property.name);
2932
IoCContainer.namespaces.setValue(property.name, config);
3033
}
34+
else if (!readOnly && config.namespace !== IoCContainer.namespaces.selectedNamespace()) {
35+
config = config.clone();
36+
IoCContainer.namespaces.setValue(property.name, config);
37+
}
3138
config.path = property.path;
3239
return config;
3340
}
@@ -48,7 +55,7 @@ export class IoCContainer {
4855
public static getType(source: Function): Function {
4956
InjectorHandler.checkType(source);
5057
const baseSource = InjectorHandler.getConstructorFromType(source);
51-
const config: IoCBindConfig = IoCContainer.namespaces.get(baseSource, true);
58+
const config: IoCBindConfig = IoCContainer.namespaces.get(baseSource);
5259
if (!config) {
5360
throw new TypeError(`The type ${source.name} hasn't been registered with the IOC Container`);
5461
}

test/unit/container/container.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ describe('Container', () => {
109109
const bind = IoCContainer.bindName(`${valueName}.${path}`);
110110

111111
expect(mockCheckName).toBeCalledWith(`${valueName}.${path}`);
112-
expect(mockGetValue).toBeCalledWith(valueName, false);
112+
expect(mockGetValue).toBeCalledWith(valueName);
113113
expect(mockIoCBindValueConfig).toBeCalledWith(valueName);
114114
expect(mockSetValue).toBeCalledWith(valueName, valueConfig);
115115

@@ -205,7 +205,7 @@ describe('Container', () => {
205205

206206
expect(mockCheckType).toBeCalledWith(MyBaseType);
207207
expect(mockGetConstructorFromType).toBeCalledWith(MyBaseType);
208-
expect(mockGet).toBeCalledWith(constructor, true);
208+
expect(mockGet).toBeCalledWith(constructor);
209209
expect(result).toStrictEqual({ target: 'source' });
210210
});
211211

@@ -223,7 +223,7 @@ describe('Container', () => {
223223

224224
expect(mockCheckType).toBeCalledWith(MyBaseType);
225225
expect(mockGetConstructorFromType).toBeCalledWith(MyBaseType);
226-
expect(mockGet).toBeCalledWith(constructor, true);
226+
expect(mockGet).toBeCalledWith(constructor);
227227
expect(result).toStrictEqual({ target: 'source' });
228228
});
229229
});

0 commit comments

Comments
 (0)