@nevware21/ts-utils
    Preparing search index...

    Function getWritableDeferred

    • Create and return a writable ICachedValue instance which will cache and return the value returned by the callback function. The callback function will only be called once, multiple access of the value will not cause re-execution of the callback as the result from the first call is cached internally. Unlike getDeferred, this version allows the cached value to be changed after it's been evaluated. This is a lightweight version that does not support any expiration or invalidation.

      Type Parameters

      • R

        The type of the value to be cached

      • F extends (...args: any[]) => R = () => R

        The type of the callback function, defaults to () => T if not specified

      Parameters

      • cb: F

        The callback function to fetch the value to be lazily evaluated and cached

      • OptionalargArray: Parameters<F>

        Optional array of arguments to be passed to the callback function

      Returns ICachedValue<R>

      A new writable ICachedValue instance which wraps the callback and will be used to cache the result of the callback

      0.12.3

      // This does not cause the evaluation to occur
      let cachedValue = getWritableDeferred(() => callSomeExpensiveFunction());
      let theValue;

      // With arguments - the argument types are inferred from the callback
      let cachedValueWithArgs = getWritableDeferred(
      (id: number, name: string) => callSomeExpensiveFunction(id, name),
      [123, "test"]
      );

      // Just checking if there is an object still does not cause the evaluation
      if (cachedValue) {
      // This will cause the evaluation to occur and the result will be cached
      theValue = cachedValue.v;
      }

      // Accessing the value again will not cause the re-evaluation to occur, it will just return the same
      // result value again.
      theValue === cachedValue.v; // true

      // The cached value can be changed
      cachedValue.v = "new value";
      theValue = cachedValue.v; // "new value"