1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import {
|
7 | Application,
|
8 | config,
|
9 | Constructor,
|
10 | CoreBindings,
|
11 | inject,
|
12 | isLifeCycleObserverClass,
|
13 | LifeCycleObserver,
|
14 | } from '@loopback/core';
|
15 | import debugFactory from 'debug';
|
16 | import {BootBindings} from '../keys';
|
17 | import {ArtifactOptions, booter} from '../types';
|
18 | import {BaseArtifactBooter} from './base-artifact.booter';
|
19 |
|
20 | const debug = debugFactory('loopback:boot:lifecycle-observer-booter');
|
21 |
|
22 | type LifeCycleObserverClass = Constructor<LifeCycleObserver>;
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | @booter('observers')
|
34 | export class LifeCycleObserverBooter extends BaseArtifactBooter {
|
35 | observers: LifeCycleObserverClass[];
|
36 |
|
37 | constructor(
|
38 | @inject(CoreBindings.APPLICATION_INSTANCE)
|
39 | public app: Application,
|
40 | @inject(BootBindings.PROJECT_ROOT) projectRoot: string,
|
41 | @config()
|
42 | public observerConfig: ArtifactOptions = {},
|
43 | ) {
|
44 | super(
|
45 | projectRoot,
|
46 |
|
47 | Object.assign({}, LifeCycleObserverDefaults, observerConfig),
|
48 | );
|
49 | }
|
50 |
|
51 | |
52 |
|
53 |
|
54 |
|
55 | async load() {
|
56 | await super.load();
|
57 |
|
58 | this.observers = this.classes.filter(isLifeCycleObserverClass);
|
59 | for (const observer of this.observers) {
|
60 | debug('Bind life cycle observer: %s', observer.name);
|
61 | const binding = this.app.lifeCycleObserver(observer);
|
62 | debug('Binding created for life cycle observer: %j', binding);
|
63 | }
|
64 | }
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | export const LifeCycleObserverDefaults: ArtifactOptions = {
|
71 | dirs: ['observers'],
|
72 | extensions: ['.observer.js'],
|
73 | nested: true,
|
74 | };
|