* Resolve the value or promise for a given injection * @param ctx - Context * @param injection - Descriptor of the injection * @param session - Optional session for binding and dependency resolution
( ctx: Context, injection: Readonly<Injection>, session?: ResolutionSession, )
| 125 | * @param session - Optional session for binding and dependency resolution |
| 126 | */ |
| 127 | function resolve<T>( |
| 128 | ctx: Context, |
| 129 | injection: Readonly<Injection>, |
| 130 | session?: ResolutionSession, |
| 131 | ): ValueOrPromise<T> { |
| 132 | /* istanbul ignore if */ |
| 133 | if (debug.enabled) { |
| 134 | debug( |
| 135 | 'Resolving an injection:', |
| 136 | ResolutionSession.describeInjection(injection), |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | ctx = resolveContext(ctx, injection, session); |
| 141 | const resolved = ResolutionSession.runWithInjection( |
| 142 | s => { |
| 143 | if (injection.resolve) { |
| 144 | // A custom resolve function is provided |
| 145 | return injection.resolve(ctx, injection, s); |
| 146 | } else { |
| 147 | // Default to resolve the value from the context by binding key |
| 148 | assert( |
| 149 | isBindingAddress(injection.bindingSelector), |
| 150 | 'The binding selector must be an address (string or BindingKey)', |
| 151 | ); |
| 152 | const key = injection.bindingSelector as BindingAddress; |
| 153 | const options: ResolutionOptions = { |
| 154 | session: s, |
| 155 | ...injection.metadata, |
| 156 | }; |
| 157 | return ctx.getValueOrPromise(key, options); |
| 158 | } |
| 159 | }, |
| 160 | injection, |
| 161 | session, |
| 162 | ); |
| 163 | return resolved; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Given a function with arguments decorated with `@inject`, |