Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface CLS<Context>

An interface that represents a background mechanism which is capable of storing, propagating and retrieving arbitrary continuation-local data (also called "context").

A continuation refers to a logical tree of execution paths; a function passed to CLS#runWithCurrentContext is considered the root of such a tree, and subsequent child nodes are functions that are "triggered" because of asynchronous operations started by their parent (for example, a function that calls fs.readFile "triggers" the callback that is passed to it). The exact definition of "trigger" is implementation-dependent.

CLS stands for continuation-local storage.

Type parameters

  • Context: __type

Hierarchy

  • CLS

Implemented by

Index

Methods

bindWithCurrentContext

  • bindWithCurrentContext<T>(fn: Func<T>): Func<T>
  • Binds a function to the current continuation. This should be used when the CLS implementation's propagating mechanism doesn't automatically do so. If not called from within a continuation, behavior is implementation- defined.

    Type parameters

    • T

    Parameters

    • fn: Func<T>

      The function to bind.

    Returns Func<T>

    A wrapped version of the given function with the same signature.

disable

  • disable(): void
  • Disables this instance. Behavior of the API other than enable() is implementation-dependent when this instance is disabled.

    Returns void

enable

  • enable(): void

getContext

  • getContext(): Context
  • Gets the current continuation-local value. If not called from within a continuation, a default value should be returned. If called before setContext has been called within a continuation, the default value should be returned as well.

    Returns Context

isEnabled

  • isEnabled(): boolean
  • Returns whether this continuation-local storage mechanism is enabled.

    Returns boolean

patchEmitterToPropagateContext

  • patchEmitterToPropagateContext(ee: EventEmitter): void
  • Patches an EventEmitter to lazily bind all future event listeners on this instance so that they belong in the same continuation as the execution path in which they were attached to the EventEmitter object.

    Parameters

    • ee: EventEmitter

      The EventEmitter to bind. This instance will be mutated.

    Returns void

runWithContext

  • runWithContext<T>(fn: Func<T>, value: Context): T
  • Runs the given function as the start of a new continuation.

    Type parameters

    • T

    Parameters

    • fn: Func<T>

      The function to run synchronously.

    • value: Context

      The value to set as the context in that continuation.

    Returns T

    The return result of running fn.

Generated using TypeDoc