Options
All
  • Public
  • Public/Protected
  • All
Menu

Hierarchy

  • MiddlewareContext
    • SessionContext

Index

Properties

_debug: Debugger

A debug function which can be overridden by subclasses.

example
import debugFactory from 'debug';
const debug = debugFactory('loopback:context:application');
export class Application extends Context {
super('application');
this._debug = debug;
}
_parent?: Context

Parent context

configResolver: ConfigurationResolver

Configuration resolver

name: string

Name of the context

registry: Map<string, Binding<any>>

Key to binding map as the internal registry

request: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
response: Response<any, Record<string, any>>
responseFinished: boolean

A flag to tell if the response is finished.

scope: BindingScope

Scope for binding resolution

subscriptionManager: ContextSubscriptionManager

Manager for observer subscriptions

tagIndexer: ContextTagIndexer

Indexer for bindings by tag

Accessors

  • get parent(): undefined | Context
  • internal

    Getter for ContextSubscriptionManager

    Returns undefined | Context

Methods

  • [captureRejectionSymbol]<K>(error: Error, event: string | symbol, ...args: AnyRest): void
  • Type Parameters

    • K

    Parameters

    • error: Error
    • event: string | symbol
    • Rest ...args: AnyRest

    Returns void

  • _findByTagIndex<ValueType>(tag: RegExp | BindingTag): Readonly<Binding<ValueType>>[]
  • Find bindings by tag leveraging indexes

    Type Parameters

    • ValueType = any

    Parameters

    • tag: RegExp | BindingTag

      Tag name pattern or name/value pairs

    Returns Readonly<Binding<ValueType>>[]

  • _mergeWithParent<ValueType>(childList: Readonly<Binding<ValueType>>[], parentList?: Readonly<Binding<ValueType>>[]): Readonly<Binding<ValueType>>[]
  • Type Parameters

    • ValueType

    Parameters

    • childList: Readonly<Binding<ValueType>>[]
    • Optional parentList: Readonly<Binding<ValueType>>[]

    Returns Readonly<Binding<ValueType>>[]

  • Add a binding to the context. If a locked binding already exists with the same key, an error will be thrown.

    Parameters

    • binding: Binding<unknown>

      The configured binding to be added

    Returns SessionContext

  • addListener<K>(eventName: string | symbol, listener: ((...args: any[]) => void)): SessionContext
  • Alias for emitter.on(eventName, listener).

    since

    v0.1.26

    Type Parameters

    • K

    Parameters

    • eventName: string | symbol
    • listener: ((...args: any[]) => void)
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns SessionContext

  • bind<ValueType>(key: BindingAddress<ValueType>): Binding<ValueType>
  • Create a binding with the given key in the context. If a locked binding already exists with the same key, an error will be thrown.

    Type Parameters

    • ValueType = any

    Parameters

    • key: BindingAddress<ValueType>

      Binding key

    Returns Binding<ValueType>

  • close(): void
  • Close the context: clear observers, stop notifications, and remove event listeners from its parent context.

    remarks

    This method MUST be called to avoid memory leaks once a context object is no longer needed and should be recycled. An example is the RequestContext, which is created per request.

    Returns void

  • configure<ConfigValueType>(key?: BindingAddress<unknown>): Binding<ConfigValueType>
  • Create a corresponding binding for configuration of the target bound by the given key in the context.

    For example, ctx.configure('controllers.MyController').to({x: 1}) will create binding controllers.MyController:$config with value {x: 1}.

    Type Parameters

    • ConfigValueType = any

    Parameters

    • Optional key: BindingAddress<unknown>

      The key for the binding to be configured

    Returns Binding<ConfigValueType>

  • contains(key: BindingAddress<unknown>): boolean
  • Check if a binding exists with the given key in the local context without delegating to the parent context

    Parameters

    • key: BindingAddress<unknown>

      Binding key

    Returns boolean

  • createView<T>(filter: BindingFilter, comparator?: BindingComparator, options?: Omit<ResolutionOptions, "session">): ContextView<T>
  • Create a view of the context chain with the given binding filter

    Type Parameters

    • T = unknown

    Parameters

    • filter: BindingFilter

      A function to match bindings

    • Optional comparator: BindingComparator

      A function to sort matched bindings

    • Optional options: Omit<ResolutionOptions, "session">

      Resolution options

    Returns ContextView<T>

  • debug(...args: unknown[]): void
  • Wrap the debug statement so that it always print out the context name as the prefix

    Parameters

    • Rest ...args: unknown[]

      Arguments for the debug

    Returns void

  • emit<K>(eventName: string | symbol, ...args: AnyRest): boolean
  • Synchronously calls each of the listeners registered for the event namedeventName, in the order they were registered, passing the supplied arguments to each.

    Returns true if the event had listeners, false otherwise.

    import { EventEmitter } from 'node:events';
    const myEmitter = new EventEmitter();

    // First listener
    myEmitter.on('event', function firstListener() {
    console.log('Helloooo! first listener');
    });
    // Second listener
    myEmitter.on('event', function secondListener(arg1, arg2) {
    console.log(`event with parameters ${arg1}, ${arg2} in second listener`);
    });
    // Third listener
    myEmitter.on('event', function thirdListener(...args) {
    const parameters = args.join(', ');
    console.log(`event with parameters ${parameters} in third listener`);
    });

    console.log(myEmitter.listeners('event'));

    myEmitter.emit('event', 1, 2, 3, 4, 5);

    // Prints:
    // [
    // [Function: firstListener],
    // [Function: secondListener],
    // [Function: thirdListener]
    // ]
    // Helloooo! first listener
    // event with parameters 1, 2 in second listener
    // event with parameters 1, 2, 3, 4, 5 in third listener
    since

    v0.1.26

    Type Parameters

    • K

    Parameters

    • eventName: string | symbol
    • Rest ...args: AnyRest

    Returns boolean

  • emitError(err: unknown): void
  • Emit an error event

    Parameters

    • err: unknown

      Error

    Returns void

  • emitEvent<T>(type: string, event: T): void
  • A strongly-typed method to emit context events

    Type Parameters

    • T extends ContextEvent

    Parameters

    • type: string

      Event type

    • event: T

      Context event

    Returns void

  • eventNames(): (string | symbol)[]
  • Returns an array listing the events for which the emitter has registered listeners. The values in the array are strings or Symbols.

    import { EventEmitter } from 'node:events';

    const myEE = new EventEmitter();
    myEE.on('foo', () => {});
    myEE.on('bar', () => {});

    const sym = Symbol('symbol');
    myEE.on(sym, () => {});

    console.log(myEE.eventNames());
    // Prints: [ 'foo', 'bar', Symbol(symbol) ]
    since

    v6.0.0

    Returns (string | symbol)[]

  • find<ValueType>(pattern?: string | RegExp | BindingFilter): Readonly<Binding<ValueType>>[]
  • Find bindings using a key pattern or filter function

    Type Parameters

    • ValueType = any

    Parameters

    • Optional pattern: string | RegExp | BindingFilter

      A filter function, a regexp or a wildcard pattern with optional * and ?. Find returns such bindings where the key matches the provided pattern.

      For a wildcard:

      • * matches zero or more characters except . and :
      • ? matches exactly one character except . and :

      For a filter function:

      • return true to include the binding in the results
      • return false to exclude it.

    Returns Readonly<Binding<ValueType>>[]

  • findByTag<ValueType>(tagFilter: RegExp | BindingTag): Readonly<Binding<ValueType>>[]
  • Find bindings using the tag filter. If the filter matches one of the binding tags, the binding is included.

    Type Parameters

    • ValueType = any

    Parameters

    • tagFilter: RegExp | BindingTag

      A filter for tags. It can be in one of the following forms:

      • A regular expression, such as /controller/
      • A wildcard pattern string with optional * and ?, such as 'con*' For a wildcard:
        • * matches zero or more characters except . and :
        • ? matches exactly one character except . and :
      • An object containing tag name/value pairs, such as {name: 'my-controller'}

    Returns Readonly<Binding<ValueType>>[]

  • findOrCreateBinding<T>(key: BindingAddress<T>, policy?: BindingCreationPolicy): Binding<T>
  • Find or create a binding for the given key

    Type Parameters

    • T

    Parameters

    • key: BindingAddress<T>

      Binding address

    • Optional policy: BindingCreationPolicy

      Binding creation policy

    Returns Binding<T>

  • get<ValueType>(keyWithPath: BindingAddress<ValueType>, session?: ResolutionSession): Promise<ValueType>
  • get<ValueType>(keyWithPath: BindingAddress<ValueType>, options: ResolutionOptions): Promise<undefined | ValueType>
  • Get the value bound to the given key, throw an error when no value is bound for the given key.

    example
    // get the value bound to "application.instance"
    const app = await ctx.get<Application>('application.instance');

    // get "rest" property from the value bound to "config"
    const config = await ctx.get<RestComponentConfig>('config#rest');

    // get "a" property of "numbers" property from the value bound to "data"
    ctx.bind('data').to({numbers: {a: 1, b: 2}, port: 3000});
    const a = await ctx.get<number>('data#numbers.a');

    Type Parameters

    • ValueType

    Parameters

    • keyWithPath: BindingAddress<ValueType>

      The binding key, optionally suffixed with a path to the (deeply) nested property to retrieve.

    • Optional session: ResolutionSession

      Optional session for resolution (accepted for backward compatibility)

    Returns Promise<ValueType>

    A promise of the bound value.

  • Get the value bound to the given key, optionally return a (deep) property of the bound value.

    example
    // get "rest" property from the value bound to "config"
    // use `undefined` when no config is provided
    const config = await ctx.get<RestComponentConfig>('config#rest', {
    optional: true
    });

    Type Parameters

    • ValueType

    Parameters

    • keyWithPath: BindingAddress<ValueType>

      The binding key, optionally suffixed with a path to the (deeply) nested property to retrieve.

    • options: ResolutionOptions

      Options for resolution.

    Returns Promise<undefined | ValueType>

    A promise of the bound value, or a promise of undefined when the optional binding is not found.

  • getBinding<ValueType>(key: BindingAddress<ValueType>): Binding<ValueType>
  • getBinding<ValueType>(key: BindingAddress<ValueType>, options?: { optional?: boolean }): undefined | Binding<ValueType>
  • Look up a binding by key in the context and its ancestors. If no matching binding is found, an error will be thrown.

    Type Parameters

    • ValueType = any

    Parameters

    • key: BindingAddress<ValueType>

      Binding key

    Returns Binding<ValueType>

  • Look up a binding by key in the context and its ancestors. If no matching binding is found and options.optional is not set to true, an error will be thrown.

    Type Parameters

    • ValueType

    Parameters

    • key: BindingAddress<ValueType>

      Binding key

    • Optional options: { optional?: boolean }

      Options to control if the binding is optional. If options.optional is set to true, the method will return undefined instead of throwing an error if the binding key is not found.

      • Optional optional?: boolean

    Returns undefined | Binding<ValueType>

  • getConfig<ConfigValueType>(key: BindingAddress<unknown>, propertyPath?: string, resolutionOptions?: ResolutionOptions): Promise<undefined | ConfigValueType>
  • Resolve configuration for the binding by key

    Type Parameters

    • ConfigValueType

    Parameters

    • key: BindingAddress<unknown>

      Binding key

    • Optional propertyPath: string

      Property path for the option. For example, x.y requests for <config>.x.y. If not set, the <config> object will be returned.

    • Optional resolutionOptions: ResolutionOptions

      Options for the resolution.

    Returns Promise<undefined | ConfigValueType>

  • getConfigAsValueOrPromise<ConfigValueType>(key: BindingAddress<unknown>, propertyPath?: string, resolutionOptions?: ResolutionOptions): ValueOrPromise<undefined | ConfigValueType>
  • Get the value or promise of configuration for a given binding by key

    Type Parameters

    • ConfigValueType

    Parameters

    • key: BindingAddress<unknown>

      Binding key

    • Optional propertyPath: string

      Property path for the option. For example, x.y requests for <config>.x.y. If not set, the <config> object will be returned.

    • Optional resolutionOptions: ResolutionOptions

      Options for the resolution.

      • optional: if not set or set to true, undefined will be returned if no corresponding value is found. Otherwise, an error will be thrown.

    Returns ValueOrPromise<undefined | ConfigValueType>

  • getConfigSync<ConfigValueType>(key: BindingAddress<unknown>, propertyPath?: string, resolutionOptions?: ResolutionOptions): undefined | ConfigValueType
  • Resolve configuration synchronously for the binding by key

    Type Parameters

    • ConfigValueType

    Parameters

    • key: BindingAddress<unknown>

      Binding key

    • Optional propertyPath: string

      Property path for the option. For example, x.y requests for config.x.y. If not set, the config object will be returned.

    • Optional resolutionOptions: ResolutionOptions

      Options for the resolution.

    Returns undefined | ConfigValueType

  • getDebugNamespace(): string
  • Get the debug namespace for the context class. Subclasses can override this method to supply its own namespace.

    example
    export class Application extends Context {
    super('application');
    }

    protected getDebugNamespace() {
    return 'loopback:context:application';
    }

    Returns string

  • getMaxListeners(): number
  • Returns the current max listener value for the EventEmitter which is either set by emitter.setMaxListeners(n) or defaults to {@link defaultMaxListeners}.

    since

    v1.0.0

    Returns number

  • getOwnerContext(keyOrBinding: BindingAddress<unknown> | Readonly<Binding<unknown>>): undefined | Context
  • Get the owning context for a binding or its key

    Parameters

    • keyOrBinding: BindingAddress<unknown> | Readonly<Binding<unknown>>

      Binding object or key

    Returns undefined | Context

  • getResolutionContext(binding: Readonly<Binding<unknown>>): undefined | Context
  • Locate the resolution context for the given binding. Only bindings in the resolution context and its ancestors are visible as dependencies to resolve the given binding

    Parameters

    • binding: Readonly<Binding<unknown>>

      Binding object

    Returns undefined | Context

  • getScopedContext(scope: APPLICATION | SERVER | REQUEST): undefined | Context
  • Get the context matching the scope

    Parameters

    • scope: APPLICATION | SERVER | REQUEST

      Binding scope

    Returns undefined | Context

  • getSync<ValueType>(keyWithPath: BindingAddress<ValueType>, session?: ResolutionSession): ValueType
  • getSync<ValueType>(keyWithPath: BindingAddress<ValueType>, options?: ResolutionOptions): undefined | ValueType
  • Get the synchronous value bound to the given key, optionally return a (deep) property of the bound value.

    This method throws an error if the bound value requires async computation (returns a promise). You should never rely on sync bindings in production code.

    example
    // get the value bound to "application.instance"
    const app = ctx.getSync<Application>('application.instance');

    // get "rest" property from the value bound to "config"
    const config = await ctx.getSync<RestComponentConfig>('config#rest');

    Type Parameters

    • ValueType

    Parameters

    • keyWithPath: BindingAddress<ValueType>

      The binding key, optionally suffixed with a path to the (deeply) nested property to retrieve.

    • Optional session: ResolutionSession

      Session for resolution (accepted for backward compatibility)

    Returns ValueType

    A promise of the bound value.

  • Get the synchronous value bound to the given key, optionally return a (deep) property of the bound value.

    This method throws an error if the bound value requires async computation (returns a promise). You should never rely on sync bindings in production code.

    example
    // get "rest" property from the value bound to "config"
    // use "undefined" when no config is provided
    const config = await ctx.getSync<RestComponentConfig>('config#rest', {
    optional: true
    });

    Type Parameters

    • ValueType

    Parameters

    • keyWithPath: BindingAddress<ValueType>

      The binding key, optionally suffixed with a path to the (deeply) nested property to retrieve.

    • Optional options: ResolutionOptions

      Options for resolution.

    Returns undefined | ValueType

    The bound value, or undefined when an optional binding is not found.

  • getValueOrPromise<ValueType>(keyWithPath: BindingAddress<ValueType>, optionsOrSession?: ResolutionOptionsOrSession): ValueOrPromise<undefined | ValueType>
  • Get the value bound to the given key.

    This is an internal version that preserves the dual sync/async result of Binding#getValue(). Users should use get() or getSync() instead.

    example
    // get the value bound to "application.instance"
    ctx.getValueOrPromise<Application>('application.instance');

    // get "rest" property from the value bound to "config"
    ctx.getValueOrPromise<RestComponentConfig>('config#rest');

    // get "a" property of "numbers" property from the value bound to "data"
    ctx.bind('data').to({numbers: {a: 1, b: 2}, port: 3000});
    ctx.getValueOrPromise<number>('data#numbers.a');
    internal

    Type Parameters

    • ValueType

    Parameters

    • keyWithPath: BindingAddress<ValueType>

      The binding key, optionally suffixed with a path to the (deeply) nested property to retrieve.

    • Optional optionsOrSession: ResolutionOptionsOrSession

      Options for resolution or a session

    Returns ValueOrPromise<undefined | ValueType>

    The bound value or a promise of the bound value, depending on how the binding is configured.

  • inspect(options?: ContextInspectOptions): JSONObject
  • Inspect the context and dump out a JSON object representing the context hierarchy

    Parameters

    • Optional options: ContextInspectOptions

      Options for inspect

    Returns JSONObject

  • isBound(key: BindingAddress<unknown>): boolean
  • Check if a key is bound in the context or its ancestors

    Parameters

    • key: BindingAddress<unknown>

      Binding key

    Returns boolean

  • isSubscribed(observer: ContextObserver): boolean
  • Check if an observer is subscribed to this context

    Parameters

    • observer: ContextObserver

      Context observer

    Returns boolean

  • isVisibleTo(ctx: Context): boolean
  • Check if this context is visible (same or ancestor) to the given one

    Parameters

    • ctx: Context

      Another context object

    Returns boolean

  • listenerCount<K>(eventName: string | symbol, listener?: Function): number
  • Returns the number of listeners listening for the event named eventName. If listener is provided, it will return how many times the listener is found in the list of the listeners of the event.

    since

    v3.2.0

    Type Parameters

    • K

    Parameters

    • eventName: string | symbol

      The name of the event being listened for

    • Optional listener: Function

      The event handler function

    Returns number

  • listeners<K>(eventName: string | symbol): Function[]
  • Returns a copy of the array of listeners for the event named eventName.

    server.on('connection', (stream) => {
    console.log('someone connected!');
    });
    console.log(util.inspect(server.listeners('connection')));
    // Prints: [ [Function] ]
    since

    v0.1.26

    Type Parameters

    • K

    Parameters

    • eventName: string | symbol

    Returns Function[]

  • off<K>(eventName: string | symbol, listener: ((...args: any[]) => void)): SessionContext
  • Alias for emitter.removeListener().

    since

    v10.0.0

    Type Parameters

    • K

    Parameters

    • eventName: string | symbol
    • listener: ((...args: any[]) => void)
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns SessionContext

  • on(eventName: "bind" | "unbind", listener: ContextEventListener): SessionContext
  • on(event: string | symbol, listener: ((...args: any[]) => void)): SessionContext
  • The "bind" event is emitted when a new binding is added to the context. The "unbind" event is emitted when an existing binding is removed.

    Parameters

    • eventName: "bind" | "unbind"

      The name of the event - always bind or unbind.

    • listener: ContextEventListener

      The listener function to call when the event is emitted.

    Returns SessionContext

  • Parameters

    • event: string | symbol
    • listener: ((...args: any[]) => void)
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns SessionContext

  • once(eventName: "bind" | "unbind", listener: ContextEventListener): SessionContext
  • once(event: string | symbol, listener: ((...args: any[]) => void)): SessionContext
  • The "bind" event is emitted when a new binding is added to the context. The "unbind" event is emitted when an existing binding is removed.

    Parameters

    • eventName: "bind" | "unbind"

      The name of the event - always bind or unbind.

    • listener: ContextEventListener

      The listener function to call when the event is emitted.

    Returns SessionContext

  • Parameters

    • event: string | symbol
    • listener: ((...args: any[]) => void)
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns SessionContext

  • prependListener<K>(eventName: string | symbol, listener: ((...args: any[]) => void)): SessionContext
  • Adds the listener function to the beginning of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventNameand listener will result in the listener being added, and called, multiple times.

    server.prependListener('connection', (stream) => {
    console.log('someone connected!');
    });

    Returns a reference to the EventEmitter, so that calls can be chained.

    since

    v6.0.0

    Type Parameters

    • K

    Parameters

    • eventName: string | symbol

      The name of the event.

    • listener: ((...args: any[]) => void)

      The callback function

        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns SessionContext

  • prependOnceListener<K>(eventName: string | symbol, listener: ((...args: any[]) => void)): SessionContext
  • Adds a one-timelistener function for the event named eventName to the beginning of the listeners array. The next time eventName is triggered, this listener is removed, and then invoked.

    server.prependOnceListener('connection', (stream) => {
    console.log('Ah, we have our first user!');
    });

    Returns a reference to the EventEmitter, so that calls can be chained.

    since

    v6.0.0

    Type Parameters

    • K

    Parameters

    • eventName: string | symbol

      The name of the event.

    • listener: ((...args: any[]) => void)

      The callback function

        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns SessionContext

  • rawListeners<K>(eventName: string | symbol): Function[]
  • Returns a copy of the array of listeners for the event named eventName, including any wrappers (such as those created by .once()).

    import { EventEmitter } from 'node:events';
    const emitter = new EventEmitter();
    emitter.once('log', () => console.log('log once'));

    // Returns a new Array with a function `onceWrapper` which has a property
    // `listener` which contains the original listener bound above
    const listeners = emitter.rawListeners('log');
    const logFnWrapper = listeners[0];

    // Logs "log once" to the console and does not unbind the `once` event
    logFnWrapper.listener();

    // Logs "log once" to the console and removes the listener
    logFnWrapper();

    emitter.on('log', () => console.log('log persistently'));
    // Will return a new Array with a single function bound by `.on()` above
    const newListeners = emitter.rawListeners('log');

    // Logs "log persistently" twice
    newListeners[0]();
    emitter.emit('log');
    since

    v9.4.0

    Type Parameters

    • K

    Parameters

    • eventName: string | symbol

    Returns Function[]

  • Removes all listeners, or those of the specified eventName.

    It is bad practice to remove listeners added elsewhere in the code, particularly when the EventEmitter instance was created by some other component or module (e.g. sockets or file streams).

    Returns a reference to the EventEmitter, so that calls can be chained.

    since

    v0.1.26

    Parameters

    • Optional eventName: string | symbol

    Returns SessionContext

  • removeListener<K>(eventName: string | symbol, listener: ((...args: any[]) => void)): SessionContext
  • Removes the specified listener from the listener array for the event namedeventName.

    const callback = (stream) => {
    console.log('someone connected!');
    };
    server.on('connection', callback);
    // ...
    server.removeListener('connection', callback);

    removeListener() will remove, at most, one instance of a listener from the listener array. If any single listener has been added multiple times to the listener array for the specified eventName, then removeListener() must be called multiple times to remove each instance.

    Once an event is emitted, all listeners attached to it at the time of emitting are called in order. This implies that anyremoveListener() or removeAllListeners() calls after emitting and before the last listener finishes execution will not remove them fromemit() in progress. Subsequent events behave as expected.

    import { EventEmitter } from 'node:events';
    class MyEmitter extends EventEmitter {}
    const myEmitter = new MyEmitter();

    const callbackA = () => {
    console.log('A');
    myEmitter.removeListener('event', callbackB);
    };

    const callbackB = () => {
    console.log('B');
    };

    myEmitter.on('event', callbackA);

    myEmitter.on('event', callbackB);

    // callbackA removes listener callbackB but it will still be called.
    // Internal listener array at time of emit [callbackA, callbackB]
    myEmitter.emit('event');
    // Prints:
    // A
    // B

    // callbackB is now removed.
    // Internal listener array [callbackA]
    myEmitter.emit('event');
    // Prints:
    // A

    Because listeners are managed using an internal array, calling this will change the position indices of any listener registered after the listener being removed. This will not impact the order in which listeners are called, but it means that any copies of the listener array as returned by the emitter.listeners() method will need to be recreated.

    When a single function has been added as a handler multiple times for a single event (as in the example below), removeListener() will remove the most recently added instance. In the example the once('ping')listener is removed:

    import { EventEmitter } from 'node:events';
    const ee = new EventEmitter();

    function pong() {
    console.log('pong');
    }

    ee.on('ping', pong);
    ee.once('ping', pong);
    ee.removeListener('ping', pong);

    ee.emit('ping');
    ee.emit('ping');

    Returns a reference to the EventEmitter, so that calls can be chained.

    since

    v0.1.26

    Type Parameters

    • K

    Parameters

    • eventName: string | symbol
    • listener: ((...args: any[]) => void)
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns SessionContext

  • By default EventEmitters will print a warning if more than 10 listeners are added for a particular event. This is a useful default that helps finding memory leaks. The emitter.setMaxListeners() method allows the limit to be modified for this specific EventEmitter instance. The value can be set toInfinity (or 0) to indicate an unlimited number of listeners.

    Returns a reference to the EventEmitter, so that calls can be chained.

    since

    v0.3.5

    Parameters

    • n: number

    Returns SessionContext

  • setupBindings(): void
  • Returns void

  • setupConfigurationResolverIfNeeded(): ConfigurationResolver
  • Set up the configuration resolver if needed

    Returns ConfigurationResolver

  • subscribe(observer: ContextEventObserver): Subscription
  • Add a context event observer to the context

    Parameters

    • observer: ContextEventObserver

      Context observer instance or function

    Returns Subscription

  • toJSON(): JSONObject
  • Create a plain JSON object for the context

    Returns JSONObject

  • unbind(key: BindingAddress<unknown>): boolean
  • Unbind a binding from the context. No parent contexts will be checked.

    remarks

    If you need to unbind a binding owned by a parent context, use the code below:

    const ownerCtx = ctx.getOwnerContext(key);
    return ownerCtx != null && ownerCtx.unbind(key);

    Parameters

    • key: BindingAddress<unknown>

      Binding key

    Returns boolean

    true if the binding key is found and removed from this context

  • unsubscribe(observer: ContextEventObserver): boolean
  • Remove the context event observer from the context

    Parameters

    • observer: ContextEventObserver

      Context event observer

    Returns boolean

Generated using TypeDoc