MCPcopy Create free account
hub / github.com/LeetCode-OpenSource/ayanami / createInstanceInScope

Function createInstanceInScope

src/core/scope/utils.ts:27–57  ·  view source on GitHub ↗
(constructor: ConstructorOf<T>, scope: Scope)

Source from the content-addressed store, hash-verified

25}
26
27function 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
59function setInstanceInScope<T>(constructor: ConstructorOf<T>, scope: Scope, newInstance: Instance) {
60 const scopeMap: ScopeMap<Scope, Instance> = map.get(constructor) || new Map()

Callers 1

Calls 2

setInstanceInScopeFunction · 0.85

Tested by

no test coverage detected