als-unhooked
    Preparing search index...

    Class Namespace<K, V>

    The backbone of the Legacy API, this class represents an object with the same interface as the namespace objects in cls-hooked, including getting/setting values and running functions in the context of the associated store.

    const ns = new Namespace('myNamespace');	// or createNamespace('myNamespace')
    ns.run(() => {
    ns.set('key', 'value');
    console.log(ns.get('key')); // Outputs: 'value'
    });

    Type Parameters

    • K = any

      The type of keys used in the context.

    • V = any

      The type of values stored in the context.

    Hierarchy (View Summary)

    Index

    Accessors

    Constructors

    Instance Methods

    • Retrieves a value from the current context.

      Type Parameters

      • T = V

      Parameters

      • key: K

        The key to retrieve.

      Returns undefined | T

      The value if found, otherwise undefined.

      const value = ns.get('key');
      
    • Sets a value in the current context.

      Type Parameters

      • T = V

      Parameters

      • key: K

        The key to set.

      • value: T

        The value to set.

      Returns void

      ns.set('key', 'value');
      
    • Creates a new context copied from the currently active context. Use this with Namespace#bind, if you want to have a fresh context at invocation time, as opposed to binding time:

      Type Parameters

      Returns T

      The created context.

      function doSomething(p) {
      * console.log("%s = %s", p, ns.get(p));
      }

      function bindLater(callback) {
      return ns.bind(callback, ns.createContext());
      }

      setInterval(function () {
      var bound = bindLater(doSomething);
      bound('test');
      }, 100);
    • Runs a function within the current context.

      Type Parameters

      • T = any

      Parameters

      • fn: (ctx?: T) => unknown

        The function to run.

      Returns T

      The current context.

      ns.run(() => {
      // code to run within the context
      });
    • Runs a function within the current context and returns its result.

      Type Parameters

      • T

      Parameters

      • fn: (ctx?: T) => T

        The function to run.

      Returns T

      The result of the function.

      const result = ns.runAndReturn(() => {
      // code to run within the context
      return 'result';
      });
    • Runs a function that returns a promise within the current context. Assumes the Promise is als friendly or already wrapped.

      Type Parameters

      • T

      Parameters

      • fn: (ctx: any) => Promise<T>

        The function to run.

      Returns Promise<T>

      The promise returned by the function.

      ns.runPromise(async () => {
      // async code to run within the context
      });
    • Binds a function to a given context. Errors thrown by the bound function will have their context attached on [ERROR_SYMBOL].

      Type Parameters

      • T extends () => any

      Parameters

      • fn: T

        The function to bind.

      • Optionalcontext: Dictionary<K, V>

        The context to bind to.

      Returns T

      The bound function.

      const boundFn = ns.bind(() => {
      // code to run within the context
      });
      boundFn();
    • Retrieves the context from an exception.

      Parameters

      • exception: Error

        The exception to retrieve the context from.

      Returns any

      The context.

      const context = ns.fromException(error);
      

    Properties

    name: string
    asyncLocalStorage: AsyncLocalStorage<Dictionary<K, V>>