| 499 | |
| 500 | // Implementation |
| 501 | getValue( |
| 502 | ctx: Context, |
| 503 | optionsOrSession?: ResolutionOptionsOrSession, |
| 504 | ): ValueOrPromise<T | undefined> { |
| 505 | /* istanbul ignore if */ |
| 506 | if (debug.enabled) { |
| 507 | debug('Get value for binding %s', this.key); |
| 508 | } |
| 509 | |
| 510 | const options = asResolutionOptions(optionsOrSession); |
| 511 | const resolutionCtx = this.getResolutionContext(ctx, options); |
| 512 | if (resolutionCtx == null) return undefined; |
| 513 | |
| 514 | // Keep a snapshot for proxy |
| 515 | const savedSession = |
| 516 | ResolutionSession.fork(options.session) ?? new ResolutionSession(); |
| 517 | |
| 518 | // First check cached value for non-transient |
| 519 | if (this._cache) { |
| 520 | if (this.scope !== BindingScope.TRANSIENT) { |
| 521 | if (resolutionCtx && this._cache.has(resolutionCtx)) { |
| 522 | const value = this._cache.get(resolutionCtx)!; |
| 523 | return this.getValueOrProxy( |
| 524 | resolutionCtx, |
| 525 | {...options, session: savedSession}, |
| 526 | value, |
| 527 | ); |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | const resolutionMetadata = { |
| 532 | context: resolutionCtx!, |
| 533 | binding: this, |
| 534 | options, |
| 535 | }; |
| 536 | if (typeof this._getValue === 'function') { |
| 537 | const result = ResolutionSession.runWithBinding( |
| 538 | s => { |
| 539 | const optionsWithSession = { |
| 540 | ...options, |
| 541 | session: s, |
| 542 | // Force to be the non-proxy version |
| 543 | asProxyWithInterceptors: false, |
| 544 | }; |
| 545 | // We already test `this._getValue` is a function. It's safe to assert |
| 546 | // that `this._getValue` is not undefined. |
| 547 | return this._getValue!({ |
| 548 | ...resolutionMetadata, |
| 549 | options: optionsWithSession, |
| 550 | }); |
| 551 | }, |
| 552 | this, |
| 553 | options.session, |
| 554 | ); |
| 555 | const value = this._cacheValue(resolutionCtx!, result); |
| 556 | return this.getValueOrProxy( |
| 557 | resolutionCtx, |
| 558 | {...options, session: savedSession}, |