/
ServiceManager.ts
129 lines (94 loc) · 3.7 KB
/
ServiceManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { ServiceManagerInterface } from './ServiceManagerInterface';
import { ServicesMapType, FactoriesMapType, AliasesType, ServiceKeyType, ServiceFactoryType, SharedMapType, ServiceManagerConfigType } from './ServiceManagerConfigInterface';
import { NotFoundError } from '../Error';
import { Instantiable } from '../Core/Types';
/**
* @export
* @class ServiceManager
* @implements {ServiceManagerInterface}
*/
export class ServiceManager implements ServiceManagerInterface {
private services: ServicesMapType = new Map();
private factories: FactoriesMapType = new Map();
private aliases: AliasesType = {};
private shared: SharedMapType = new Map();
private sharedByDefault: boolean = true;
protected creationContext: ServiceManager;
constructor(config?: ServiceManagerConfigType) {
this.creationContext = this;
if (config) {
this.configure(config);
}
}
public get<T>(Service: ServiceKeyType<T>, forceTransient: boolean = false): T {
const resolvedName = this.resolveName(Service) as ServiceKeyType<T>;
if (!this.has(resolvedName)) {
throw new NotFoundError(`Unable to locate service "${typeof Service === 'string' ? Service : Service.name}".`);
}
if (forceTransient || !this.services.has(resolvedName)) {
const service = this.factories.get(resolvedName)(this.creationContext) as T;
const shared = this.shared.has(resolvedName) ? this.shared.get(resolvedName) : this.sharedByDefault;
// We just needed a new instance or we don't want our instances to be shared.. Return service.
if (forceTransient || !shared) {
return service;
}
this.services.set(resolvedName, service);
}
return this.services.get(resolvedName) as T;
}
has<T>(Service: ServiceKeyType<T>): boolean {
const resolvedName = this.resolveName(Service) as ServiceKeyType<T>;
return this.services.has(resolvedName) || this.factories.has(resolvedName);
}
public registerFactory(key: Function | string, value: ServiceFactoryType<Object>): this {
this.factories.set(key, value);
return this;
}
public registerFactories(factories: FactoriesMapType): this {
factories.forEach((value: ServiceFactoryType<Object>, key: Function | string) => {
this.registerFactory(key, value);
});
return this;
}
public registerService(key: Function | string, service: Object): this {
this.services.set(key, service);
return this;
}
public configure(config: ServiceManagerConfigType): this {
if (typeof config.sharedByDefault === 'boolean') {
this.sharedByDefault = config.sharedByDefault;
}
if (config.shared instanceof Map) {
config.shared.forEach((value, key: ServiceKeyType<Object>) => this.shared.set(key, value));
}
if (config.services instanceof Map) {
config.services.forEach((value, key: ServiceKeyType<Object>) => this.services.set(key, value));
}
if (config.invokables instanceof Map) {
config.invokables.forEach((value: Instantiable<Object>, key: ServiceKeyType<Object>) => {
this.factories.set(key, () => new value);
});
}
if (config.factories instanceof Map) {
this.registerFactories(config.factories);
}
if (config.aliases) {
Object.assign(this.aliases, config.aliases);
}
return this;
}
public registerAliases(aliases: AliasesType): this {
Object.assign(this.aliases, aliases);
return this;
}
public registerAlias(alias: string, to: string | Function): this {
this.aliases[alias] = to;
return this;
}
private resolveName<T>(name: ServiceKeyType<T>): ServiceKeyType<T> {
if (typeof name !== 'string') {
return name;
}
return this.aliases[name] as ServiceKeyType<T> || name;
}
}