UNPKG

3.51 kBTypeScriptView Raw
1import { Binding, Context, ContextView } from '@loopback/context';
2import { LifeCycleObserver } from './lifecycle';
3/**
4 * A group of life cycle observers
5 */
6export type LifeCycleObserverGroup = {
7 /**
8 * Observer group name
9 */
10 group: string;
11 /**
12 * Bindings for observers within the group
13 */
14 bindings: Readonly<Binding<LifeCycleObserver>>[];
15};
16export type LifeCycleObserverOptions = {
17 /**
18 * Control the order of observer groups for notifications. For example,
19 * with `['datasource', 'server']`, the observers in `datasource` group are
20 * notified before those in `server` group during `start`. Please note that
21 * observers are notified in the reverse order during `stop`.
22 */
23 orderedGroups: string[];
24 /**
25 * Override and disable lifecycle observer groups. This setting applies to
26 * both ordered groups (i.e. those defined in `orderedGroups`) and unordered
27 * groups.
28 */
29 disabledGroups?: string[];
30 /**
31 * Notify observers of the same group in parallel, default to `true`
32 */
33 parallel?: boolean;
34};
35export declare const DEFAULT_ORDERED_GROUPS: string[];
36/**
37 * A context-based registry for life cycle observers
38 */
39export declare class LifeCycleObserverRegistry implements LifeCycleObserver {
40 protected readonly context: Context;
41 protected readonly observersView: ContextView<LifeCycleObserver>;
42 protected readonly options: LifeCycleObserverOptions;
43 constructor(context: Context, observersView: ContextView<LifeCycleObserver>, options?: LifeCycleObserverOptions);
44 setOrderedGroups(groups: string[]): void;
45 /**
46 * Get observer groups ordered by the group
47 */
48 getObserverGroupsByOrder(): LifeCycleObserverGroup[];
49 /**
50 * Get the group for a given life cycle observer binding
51 * @param binding - Life cycle observer binding
52 */
53 protected getObserverGroup(binding: Readonly<Binding<LifeCycleObserver>>): string;
54 /**
55 * Sort the life cycle observer bindings so that we can start/stop them
56 * in the right order. By default, we can start other observers before servers
57 * and stop them in the reverse order
58 * @param bindings - Life cycle observer bindings
59 */
60 protected sortObserverBindingsByGroup(bindings: Readonly<Binding<LifeCycleObserver>>[]): LifeCycleObserverGroup[];
61 /**
62 * Notify an observer group of the given event
63 * @param group - A group of bindings for life cycle observers
64 * @param event - Event name
65 */
66 protected notifyObservers(observers: LifeCycleObserver[], bindings: Readonly<Binding<LifeCycleObserver>>[], event: keyof LifeCycleObserver): Promise<void>;
67 /**
68 * Invoke an observer for the given event
69 * @param observer - A life cycle observer
70 * @param event - Event name
71 */
72 protected invokeObserver(observer: LifeCycleObserver, event: keyof LifeCycleObserver): Promise<void>;
73 /**
74 * Emit events to the observer groups
75 * @param events - Event names
76 * @param groups - Observer groups
77 */
78 protected notifyGroups(events: (keyof LifeCycleObserver)[], groups: LifeCycleObserverGroup[], reverse?: boolean): Promise<void>;
79 /**
80 * Notify all life cycle observers by group of `init`
81 */
82 init(): Promise<void>;
83 /**
84 * Notify all life cycle observers by group of `start`
85 */
86 start(): Promise<void>;
87 /**
88 * Notify all life cycle observers by group of `stop`
89 */
90 stop(): Promise<void>;
91}