UNPKG

1.51 kBPlain TextView Raw
1// Copyright IBM Corp. and LoopBack contributors 2019,2020. All Rights Reserved.
2// Node module: @loopback/context
3// This file is licensed under the MIT License.
4// License text available at https://opensource.org/licenses/MIT
5
6import {Binding} from './binding';
7import {BindingFilter} from './binding-filter';
8import {Context} from './context';
9import {ValueOrPromise} from './value-promise';
10
11/**
12 * Context event types. We support `bind` and `unbind` for now but
13 * keep it open for new types
14 */
15export type ContextEventType = 'bind' | 'unbind' | string;
16
17/**
18 * Listen on `bind`, `unbind`, or other events
19 * @param eventType - Context event type
20 * @param binding - The binding as event source
21 * @param context - Context object for the binding event
22 */
23export type ContextObserverFn = (
24 eventType: ContextEventType,
25 binding: Readonly<Binding<unknown>>,
26 context: Context,
27) => ValueOrPromise<void>;
28
29/**
30 * Observers of context bind/unbind events
31 */
32export interface ContextObserver {
33 /**
34 * An optional filter function to match bindings. If not present, the listener
35 * will be notified of all binding events.
36 */
37 filter?: BindingFilter;
38
39 /**
40 * Listen on `bind`, `unbind`, or other events
41 * @param eventType - Context event type
42 * @param binding - The binding as event source
43 */
44 observe: ContextObserverFn;
45}
46
47/**
48 * Context event observer type - An instance of `ContextObserver` or a function
49 */
50export type ContextEventObserver = ContextObserver | ContextObserverFn;