UNPKG

2.17 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
3// Node module: @loopback/core
4// This file is licensed under the MIT License.
5// License text available at https://opensource.org/licenses/MIT
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.lifeCycleObserver = exports.lifeCycleObserverFilter = exports.asLifeCycleObserver = exports.isLifeCycleObserverClass = exports.isLifeCycleObserver = void 0;
8const context_1 = require("@loopback/context");
9const keys_1 = require("./keys");
10const lifeCycleMethods = ['init', 'start', 'stop'];
11/**
12 * Test if an object implements LifeCycleObserver
13 * @param obj - An object
14 */
15function isLifeCycleObserver(obj) {
16 const candidate = obj;
17 return lifeCycleMethods.some(m => typeof candidate[m] === 'function');
18}
19exports.isLifeCycleObserver = isLifeCycleObserver;
20/**
21 * Test if a class implements LifeCycleObserver
22 * @param ctor - A class
23 */
24function isLifeCycleObserverClass(ctor) {
25 return ctor.prototype && isLifeCycleObserver(ctor.prototype);
26}
27exports.isLifeCycleObserverClass = isLifeCycleObserverClass;
28/**
29 * A `BindingTemplate` function to configure the binding as life cycle observer
30 * by tagging it with `CoreTags.LIFE_CYCLE_OBSERVER`.
31 *
32 * @param binding - Binding object
33 */
34function asLifeCycleObserver(binding) {
35 return binding.tag(keys_1.CoreTags.LIFE_CYCLE_OBSERVER);
36}
37exports.asLifeCycleObserver = asLifeCycleObserver;
38/**
39 * Find all life cycle observer bindings. By default, a binding tagged with
40 * `CoreTags.LIFE_CYCLE_OBSERVER`. It's used as `BindingFilter`.
41 */
42exports.lifeCycleObserverFilter = (0, context_1.filterByTag)(keys_1.CoreTags.LIFE_CYCLE_OBSERVER);
43/**
44 * Sugar decorator to mark a class as life cycle observer
45 * @param group - Optional observer group name
46 * @param specs - Optional bindings specs
47 */
48function lifeCycleObserver(group = '', ...specs) {
49 return (0, context_1.injectable)(asLifeCycleObserver, {
50 tags: {
51 [keys_1.CoreTags.LIFE_CYCLE_OBSERVER_GROUP]: group,
52 },
53 }, ...specs);
54}
55exports.lifeCycleObserver = lifeCycleObserver;
56//# sourceMappingURL=lifecycle.js.map
\No newline at end of file