Page Contents

Home > @loopback/context > Context

Context class

Context provides an implementation of Inversion of Control (IoC) container

Signature:

export declare class Context extends EventEmitter 

Constructors

Constructor Modifiers Description
(constructor)(_parent, name)   Create a new context.

Properties

Property Modifiers Type Description
_parent   Context Parent context
_parentEventListeners   Map<string, (...args: any[]) => void> | undefined Event listeners for parent context keyed by event names. It keeps track of listeners from this context against its parent so that we can remove these listeners when this context is closed.
configResolver   ConfigurationResolver  
name   string Name of the context
observers   Set<ContextEventObserver> | undefined A list of registered context observers. The Set will be created when the first observer is added.
registry   Map<string, Binding> Key to binding map as the internal registry

Methods

Method Modifiers Description
_mergeWithParent(childList, parentList)    
add(binding)   Add a binding to the context. If a locked binding already exists with the same key, an error will be thrown.
bind(key)   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.
close()   Close the context: clear observers, stop notifications, and remove event listeners from its parent context.
configure(key)   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}.
contains(key)   Check if a binding exists with the given key in the local context without delegating to the parent context
createView(filter, comparator)   Create a view of the context chain with the given binding filter
find(pattern)   Find bindings using a key pattern or filter function
findByTag(tagFilter)   Find bindings using the tag filter. If the filter matches one of the binding tags, the binding is included.
findOrCreateBinding(key, policy)   Find or create a binding for the given key
get(keyWithPath, session)   Get the value bound to the given key, throw an error when no value is bound for the given key.
get(keyWithPath, options)   Get the value bound to the given key, optionally return a (deep) property of the bound value.
getBinding(key)   Look up a binding by key in the context and its ancestors. If no matching binding is found, an error will be thrown.
getBinding(key, options)   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.
getConfig(key, configPath, resolutionOptions)   Resolve configuration for the binding by key
getConfigAsValueOrPromise(key, configPath, resolutionOptions)   Get the value or promise of configuration for a given binding by key
getConfigSync(key, configPath, resolutionOptions)   Resolve configuration synchronously for the binding by key
getOwnerContext(key)   Get the owning context for a binding key
getSync(keyWithPath, session)   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.
getSync(keyWithPath, options)   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.
isBound(key)   Check if a key is bound in the context or its ancestors
isSubscribed(observer)   Check if an observer is subscribed to this context
notifyObservers(eventType, binding, context, observers)   Publish an event to the registered observers. Please note the notification is queued and performed asynchronously so that we allow fluent APIs such as ctx.bind('key').to(...).tag(...); and give observers the fully populated binding.
setupConfigurationResolverIfNeeded()   Set up the configuration resolver if needed
subscribe(observer)   Add a context event observer to the context
toJSON()   Create a plain JSON object for the context
unbind(key)   Unbind a binding from the context. No parent contexts will be checked.
unsubscribe(observer)   Remove the context event observer from the context
waitUntilPendingNotificationsDone(timeout)   Wait until observers are notified for all of currently pending notification events.This method is for test only to perform assertions after observers are notified for relevant events.