* Bind the key to a value computed by a Provider. * * * @example * * ```ts * export class DateProvider implements Provider { * constructor(@inject('stringDate') private param: String){} * value(): Date { * return new Date(param); * } * } * ``` *
(providerClass: Constructor<Provider<T>>)
| 845 | * @param provider - The value provider to use. |
| 846 | */ |
| 847 | toProvider(providerClass: Constructor<Provider<T>>): this { |
| 848 | /* istanbul ignore if */ |
| 849 | if (debug.enabled) { |
| 850 | debug('Bind %s to provider %s', this.key, providerClass.name); |
| 851 | } |
| 852 | this._source = { |
| 853 | type: BindingType.PROVIDER, |
| 854 | value: providerClass, |
| 855 | }; |
| 856 | this._setValueGetter(resolutionCtx => { |
| 857 | const providerOrPromise = instantiateClass<Provider<T>>( |
| 858 | providerClass, |
| 859 | resolutionCtx.context, |
| 860 | resolutionCtx.options.session, |
| 861 | ); |
| 862 | const value = transformValueOrPromise(providerOrPromise, p => p.value()); |
| 863 | return Binding.valueOrProxy(resolutionCtx, value); |
| 864 | }); |
| 865 | return this; |
| 866 | } |
| 867 | |
| 868 | /** |
| 869 | * Bind the key to an instance of the given class. |