Page Contents

Home > @loopback/context > ContextView

ContextView class

ContextView provides a view for a given context chain to maintain a live list of matching bindings and their resolved values within the context hierarchy.

This class is the key utility to implement dynamic extensions for extension points. For example, the RestServer can react to controller bindings even they are added/removed/updated after the application starts.

ContextView is an event emitter that emits the following events: - ‘bind’: when a binding is added to the view - ‘unbind’: when a binding is removed from the view - ‘close’: when the view is closed (stopped observing context events) - ‘refresh’: when the view is refreshed as bindings are added/removed - ‘resolve’: when the cached values are resolved and updated

Signature:

export declare class ContextView<T = unknown> extends EventEmitter implements ContextObserver 

Extends: EventEmitter

Implements: ContextObserver

Constructors

Constructor Modifiers Description
(constructor)(context, filter, comparator, resolutionOptions)   Create a context view

Properties

Property Modifiers Type Description
_cachedBindings protected Readonly<Binding<T>>[] | undefined An array of cached bindings that matches the binding filter
_cachedValues protected Map<Readonly<Binding<T>>, T> | undefined A map of cached values by binding
bindings readonly Readonly<Binding<T>>[] Get the list of matched bindings. If they are not cached, it tries to find them from the context.
comparator? readonly BindingComparator | undefined (Optional)
context readonly Context  
filter readonly BindingFilter  

Methods

Method Modifiers Description
asGetter(session)   As a Getter function
close()   Stop listening events from the context
findBindings() protected Find matching bindings and refresh the cache
observe(event, binding, context)   Listen on bind or unbind and invalidate the cache
on(eventName, listener)   The “bind” event is emitted when a new binding is added to the view.
on(eventName, listener)   The “unbind” event is emitted a new binding is removed from the view.
on(eventName, listener)   The “refresh” event is emitted when the view is refreshed as bindings are added/removed.
on(eventName, listener)   The “resolve” event is emitted when the cached values are resolved and updated.
on(eventName, listener)   The “close” event is emitted when the view is closed (stopped observing context events)
on(event, listener)    
once(eventName, listener)   The “bind” event is emitted when a new binding is added to the view.
once(eventName, listener)   The “unbind” event is emitted a new binding is removed from the view.
once(eventName, listener)   The “refresh” event is emitted when the view is refreshed as bindings are added/removed.
once(eventName, listener)   The “resolve” event is emitted when the cached values are resolved and updated.
once(eventName, listener)   The “close” event is emitted when the view is closed (stopped observing context events)
once(event, listener)    
open()   Start listening events from the context
refresh()   Refresh the view by invalidating its cache
resolve(session)   Resolve values for the matching bindings
singleValue(session)   Get the single value
values(session)   Get the list of resolved values. If they are not cached, it tries to find and resolve them.