MCPcopy
hub / github.com/nestjs/nest / loadInstance

Method loadInstance

packages/core/injector/injector.ts:139–223  ·  view source on GitHub ↗
(
    wrapper: InstanceWrapper<T>,
    collection: Map<InjectionToken, InstanceWrapper>,
    moduleRef: Module,
    resolutionContext: ResolutionContext = { contextId: STATIC_CONTEXT },
  )

Source from the content-addressed store, hash-verified

137 }
138
139 public async loadInstance<T>(
140 wrapper: InstanceWrapper<T>,
141 collection: Map<InjectionToken, InstanceWrapper>,
142 moduleRef: Module,
143 resolutionContext: ResolutionContext = { contextId: STATIC_CONTEXT },
144 ) {
145 const inquirerId = this.getContextInquirerId(resolutionContext);
146 const instanceHost = wrapper.getInstanceByContextId(
147 this.getContextId(resolutionContext.contextId, wrapper),
148 inquirerId,
149 );
150
151 if (instanceHost.isPending) {
152 const settlementSignal = wrapper.settlementSignal;
153 if (
154 resolutionContext.inquirer &&
155 settlementSignal?.isCycle(resolutionContext.inquirer.id)
156 ) {
157 throw new CircularDependencyException(`"${wrapper.name}"`);
158 }
159
160 return instanceHost.donePromise!.then((err?: unknown) => {
161 if (err) {
162 throw err;
163 }
164 });
165 }
166
167 const settlementSignal = this.applySettlementSignal(instanceHost, wrapper);
168 const token = wrapper.token || wrapper.name;
169
170 const { inject } = wrapper;
171 const targetWrapper = collection.get(token);
172 if (isUndefined(targetWrapper)) {
173 throw new RuntimeException();
174 }
175 if (instanceHost.isResolved) {
176 return settlementSignal.complete();
177 }
178 try {
179 const t0 = this.getNowTimestamp();
180 const localResolutionContext = this.createResolutionContext(
181 resolutionContext.contextId,
182 wrapper,
183 inquirerId,
184 );
185 const callback = async (instances: unknown[]) => {
186 const properties = await this.resolveProperties(
187 wrapper,
188 moduleRef,
189 inject as InjectionToken[],
190 localResolutionContext,
191 resolutionContext.inquirer,
192 );
193 const instance = await this.instantiateClass(
194 instances,
195 wrapper,
196 targetWrapper,

Callers 5

loadMiddlewareMethod · 0.95
loadPerContextMethod · 0.95
loadEnhancerMethod · 0.95
injector.spec.tsFile · 0.80

Calls 12

getContextInquirerIdMethod · 0.95
getContextIdMethod · 0.95
applySettlementSignalMethod · 0.95
getNowTimestampMethod · 0.95
isUndefinedFunction · 0.90
isCycleMethod · 0.80
getMethod · 0.65
errorMethod · 0.65
completeMethod · 0.45

Tested by

no test coverage detected