* Bind the key to a computed (dynamic) value. * * @param factoryFn - The factory function creating the value. * Both sync and async functions are supported. * * @example * * ```ts * // synchronous * ctx.bind('now').toDynamicValue(() => Date.now()); * * // asynchron
(
factory: ValueFactory<T> | DynamicValueProviderClass<T>,
)
| 792 | * ``` |
| 793 | */ |
| 794 | toDynamicValue( |
| 795 | factory: ValueFactory<T> | DynamicValueProviderClass<T>, |
| 796 | ): this { |
| 797 | /* istanbul ignore if */ |
| 798 | if (debug.enabled) { |
| 799 | debug('Bind %s to dynamic value:', this.key, factory); |
| 800 | } |
| 801 | this._source = { |
| 802 | type: BindingType.DYNAMIC_VALUE, |
| 803 | value: factory, |
| 804 | }; |
| 805 | |
| 806 | let factoryFn: ValueFactory<T>; |
| 807 | if (isDynamicValueProviderClass(factory)) { |
| 808 | factoryFn = toValueFactory(factory); |
| 809 | } else { |
| 810 | factoryFn = factory; |
| 811 | } |
| 812 | this._setValueGetter(resolutionCtx => { |
| 813 | const value = factoryFn(resolutionCtx); |
| 814 | return Binding.valueOrProxy(resolutionCtx, value); |
| 815 | }); |
| 816 | return this; |
| 817 | } |
| 818 | |
| 819 | private static valueOrProxy<V>( |
| 820 | resolutionCtx: ResolutionContext, |