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

    Function safeGetWritableLazy

    • Create and return an writable ILazyValue 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 does not cause re-execution of the callback as the result from the first call is cached internally. If the callback throws the default value will be returned.

      Type Parameters

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

      Parameters

      • cb: F

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

      • defValue: T

        The default value to return when an exception is thrown

      • OptionalargArray: Parameters<F>

        An optional array of arguments to be passed to the callback function (since 0.12.3)

      Returns ILazyValue<T>

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

      0.11.7

      // This does not cause the evaluation to occur
      let cachedValue = safeGetWritableLazt(() => callSomeExpensiveFunctionWhichMightThrow(), "someDefaultValue");
      let theValue;

      // 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

      // Changing the value is allowed
      cachedValue.v = "new value";

      // Accessing the value again will return the new value
      theValue === cachedValue.v; // true