(constructor: ConstructorOf<T>, scope: Scope)
| 25 | } |
| 26 | |
| 27 | function createInstanceInScope<T>(constructor: ConstructorOf<T>, scope: Scope): T { |
| 28 | const paramTypes: ConstructorOf<any>[] = |
| 29 | Reflect.getMetadata('design:paramtypes', constructor) || [] |
| 30 | |
| 31 | // parameters decorated by @Inject |
| 32 | const paramAnnotations = Reflect.getMetadata('parameters', constructor) || [] |
| 33 | |
| 34 | const sameScopeParams: number[] = Reflect.getMetadata(SameScopeMetadataKey, constructor) || [] |
| 35 | |
| 36 | const deps = paramTypes.map((type, index) => { |
| 37 | if (sameScopeParams[index]) { |
| 38 | return createOrGetInstanceInScope(type, scope) |
| 39 | } |
| 40 | |
| 41 | if (paramAnnotations[index] !== null) { |
| 42 | let metadata: any[] = paramAnnotations[index] |
| 43 | metadata = Array.isArray(metadata) ? metadata : [metadata] |
| 44 | const inject: Inject | undefined = metadata.find((factory) => factory instanceof Inject) |
| 45 | if (inject && inject.token) { |
| 46 | return InjectableFactory.getInstanceByToken(inject.token) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return InjectableFactory.getInstance(type) |
| 51 | }) |
| 52 | |
| 53 | const newInstance = new constructor(...deps) |
| 54 | |
| 55 | setInstanceInScope(constructor, scope, newInstance) |
| 56 | return newInstance |
| 57 | } |
| 58 | |
| 59 | function setInstanceInScope<T>(constructor: ConstructorOf<T>, scope: Scope, newInstance: Instance) { |
| 60 | const scopeMap: ScopeMap<Scope, Instance> = map.get(constructor) || new Map() |
no test coverage detected