|
1 |
| -import {ServiceMetadata} from "./types/ServiceMetadata"; |
2 |
| -import {ObjectType} from "./types/ObjectType"; |
| 1 | +import {Container} from "./Container"; |
| 2 | +import {MissingProvidedServiceTypeError} from "./error/MissingProvidedServiceTypeError"; |
| 3 | +import {ServiceNotFoundError} from "./error/ServiceNotFoundError"; |
3 | 4 | import {Token} from "./Token";
|
| 5 | +import {ObjectType} from "./types/ObjectType"; |
4 | 6 | import {ServiceIdentifier} from "./types/ServiceIdentifier";
|
5 |
| -import {ServiceNotFoundError} from "./error/ServiceNotFoundError"; |
6 |
| -import {MissingProvidedServiceTypeError} from "./error/MissingProvidedServiceTypeError"; |
7 |
| -import {Container} from "./Container"; |
| 7 | +import {ServiceMetadata} from "./types/ServiceMetadata"; |
8 | 8 |
|
9 | 9 | /**
|
10 | 10 | * TypeDI can have multiple containers.
|
@@ -90,7 +90,13 @@ export class ContainerInstance {
|
90 | 90 | * Retrieves the service with given name or type from the service container.
|
91 | 91 | * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
|
92 | 92 | */
|
93 |
| - get<T>(identifier: ServiceIdentifier): T { |
| 93 | + get<T>(id: { service: T }): T; |
| 94 | + |
| 95 | + /** |
| 96 | + * Retrieves the service with given name or type from the service container. |
| 97 | + * Optionally, parameters can be passed in case if instance is initialized in the container for the first time. |
| 98 | + */ |
| 99 | + get<T>(identifier: ServiceIdentifier<T>): T { |
94 | 100 |
|
95 | 101 | const globalContainer = Container.of(undefined);
|
96 | 102 | let service = globalContainer.findService(identifier);
|
@@ -174,12 +180,15 @@ export class ContainerInstance {
|
174 | 180 | if (typeof identifierOrServiceMetadata === "string" || identifierOrServiceMetadata instanceof Token) {
|
175 | 181 | return this.set({ id: identifierOrServiceMetadata, value: value });
|
176 | 182 | }
|
| 183 | + if (typeof identifierOrServiceMetadata === "object" && (identifierOrServiceMetadata as { service: Token<any> }).service) { |
| 184 | + return this.set({ id: (identifierOrServiceMetadata as { service: Token<any> }).service, value: value }); |
| 185 | + } |
177 | 186 | if (identifierOrServiceMetadata instanceof Function) {
|
178 | 187 | return this.set({ type: identifierOrServiceMetadata, id: identifierOrServiceMetadata, value: value });
|
179 | 188 | }
|
180 | 189 |
|
181 | 190 | // const newService: ServiceMetadata<any, any> = arguments.length === 1 && typeof identifierOrServiceMetadata === "object" && !(identifierOrServiceMetadata instanceof Token) ? identifierOrServiceMetadata : undefined;
|
182 |
| - const newService: ServiceMetadata<any, any> = identifierOrServiceMetadata; |
| 191 | + const newService: ServiceMetadata<any, any> = identifierOrServiceMetadata as any; |
183 | 192 | const service = this.findService(newService.id);
|
184 | 193 | if (service && service.multiple !== true) {
|
185 | 194 | Object.assign(service, newService);
|
@@ -234,8 +243,15 @@ export class ContainerInstance {
|
234 | 243 | */
|
235 | 244 | private findService(identifier: ServiceIdentifier): ServiceMetadata<any, any>|undefined {
|
236 | 245 | return this.services.find(service => {
|
237 |
| - if (service.id) |
| 246 | + if (service.id) { |
| 247 | + if (identifier instanceof Object && |
| 248 | + service.id instanceof Token && |
| 249 | + (identifier as any).service instanceof Token) { |
| 250 | + return service.id === (identifier as any).service; |
| 251 | + } |
| 252 | + |
238 | 253 | return service.id === identifier;
|
| 254 | + } |
239 | 255 |
|
240 | 256 | if (service.type && identifier instanceof Function)
|
241 | 257 | return service.type === identifier; // todo: not sure why it was here || identifier.prototype instanceof service.type;
|
@@ -270,6 +286,9 @@ export class ContainerInstance {
|
270 | 286 |
|
271 | 287 | } else if (identifier instanceof Function) {
|
272 | 288 | type = identifier;
|
| 289 | + |
| 290 | + // } else if (identifier instanceof Object && (identifier as { service: Token<any> }).service instanceof Token) { |
| 291 | + // type = (identifier as { service: Token<any> }).service; |
273 | 292 | }
|
274 | 293 |
|
275 | 294 | // if service was not found then create a new one and register it
|
@@ -300,7 +319,7 @@ export class ContainerInstance {
|
300 | 319 | value = (this.get(service.factory[0]) as any)[service.factory[1]](...params);
|
301 | 320 |
|
302 | 321 | } else { // regular factory function
|
303 |
| - value = service.factory(...params); |
| 322 | + value = service.factory(...params, this); |
304 | 323 | }
|
305 | 324 |
|
306 | 325 | } else { // otherwise simply create a new object instance
|
|
0 commit comments