MCPcopy
hub / github.com/loopbackio/loopback-next / toDynamicValue

Method toDynamicValue

packages/context/src/binding.ts:794–817  ·  view source on GitHub ↗

* 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>,
  )

Source from the content-addressed store, hash-verified

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,

Callers 15

asClassOrProviderFunction · 0.80
givenContextFunction · 0.80
givenContextFunction · 0.80
resolver.unit.tsFile · 0.80
binding.unit.tsFile · 0.80
toBeBoundFunction · 0.80
context.unit.tsFile · 0.80
setupBindingsFunction · 0.80
givenContextFunction · 0.80
createContextFunction · 0.80

Calls 4

_setValueGetterMethod · 0.95
toValueFactoryFunction · 0.85
valueOrProxyMethod · 0.80

Tested by 10

givenContextFunction · 0.64
givenContextFunction · 0.64
toBeBoundFunction · 0.64
setupBindingsFunction · 0.64
givenContextFunction · 0.64
createContextFunction · 0.64
createDynamicBindingFunction · 0.64
givenHandlerFunction · 0.64
givenTestAppFunction · 0.64