Function lazySafeGetInst

  • Create and return an readonly ILazyValue instance which will cache and return the named global value if available, will return null if the named global object is not available or if the runtime throws an exception when attempting to access the global object. Unlike getInst the value is cached after the first access, so if the global value changes after the initial fetch the original cached value is still returned.

    Type Parameters

    • T

    Parameters

    • name: string | number | symbol

      The name of the global object to get, may be any valid PropertyKey (string, number or symbol)

    Returns ILazyValue<T>

    A new readonly ILazyValue instance which will lazily attempt to return the globally available named instance.

    Since

    0.9.5

    Example

    // This does not cause the evaluation to occur
    window.myGlobal = "Hello";
    let cachedValue = lazySafeGetInst("myGlobal");
    // cachedValue.v === "Hello"

    window.myGlobal = "Darkness";
    // cachedValue.v === "Hello"

    let promiseCls = lazySafeGetInst("Promise");
    // null if Promise is not supported in the runtime
    // otherwise the Promise class.