@loopback/core
Advanced tools
Comparing version 2.5.0 to 2.6.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [2.6.0](https://github.com/strongloop/loopback-next/compare/@loopback/core@2.5.0...@loopback/core@2.6.0) (2020-05-19) | ||
### Features | ||
* **core:** allow extensionFilter to take a list of extension point names ([8f315eb](https://github.com/strongloop/loopback-next/commit/8f315eb46bee7365da5325a23b948df9d477bfdb)) | ||
# [2.5.0](https://github.com/strongloop/loopback-next/compare/@loopback/core@2.4.2...@loopback/core@2.5.0) (2020-05-07) | ||
@@ -8,0 +19,0 @@ |
@@ -7,2 +7,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Application = void 0; | ||
const tslib_1 = require("tslib"); | ||
@@ -12,3 +13,3 @@ const context_1 = require("@loopback/context"); | ||
const debug_1 = tslib_1.__importDefault(require("debug")); | ||
const p_event_1 = tslib_1.__importDefault(require("p-event")); | ||
const events_1 = require("events"); | ||
const component_1 = require("./component"); | ||
@@ -226,3 +227,3 @@ const keys_1 = require("./keys"); | ||
async awaitState(state) { | ||
await p_event_1.default(this, state); | ||
await events_1.once(this, state); | ||
} | ||
@@ -229,0 +230,0 @@ /** |
@@ -7,2 +7,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.mountComponent = void 0; | ||
const context_1 = require("@loopback/context"); | ||
@@ -9,0 +10,0 @@ /** |
@@ -102,5 +102,5 @@ import { Binding, BindingFilter, BindingFromClassOptions, BindingSpec, BindingTemplate, Constructor, Context } from '@loopback/context'; | ||
* extension point | ||
* @param extensionPointName - Name of the extension point | ||
* @param extensionPointNames - A list of names of extension points | ||
*/ | ||
export declare function extensionFilter(extensionPointName: string): BindingFilter; | ||
export declare function extensionFilter(...extensionPointNames: string[]): BindingFilter; | ||
/** | ||
@@ -107,0 +107,0 @@ * A factory function to create binding template for extensions of the given |
@@ -7,2 +7,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.addExtension = exports.extensionFor = exports.extensionFilter = exports.extensions = exports.extensionPoint = void 0; | ||
const context_1 = require("@loopback/context"); | ||
@@ -168,7 +169,7 @@ const keys_1 = require("./keys"); | ||
* extension point | ||
* @param extensionPointName - Name of the extension point | ||
* @param extensionPointNames - A list of names of extension points | ||
*/ | ||
function extensionFilter(extensionPointName) { | ||
function extensionFilter(...extensionPointNames) { | ||
return context_1.filterByTag({ | ||
[keys_1.CoreTags.EXTENSION_FOR]: context_1.includesTagValue(extensionPointName), | ||
[keys_1.CoreTags.EXTENSION_FOR]: context_1.includesTagValue(...extensionPointNames), | ||
}); | ||
@@ -175,0 +176,0 @@ } |
@@ -19,3 +19,4 @@ /** | ||
export * from './lifecycle-registry'; | ||
export * from './mixin-target'; | ||
export * from './server'; | ||
export * from './service'; |
@@ -28,3 +28,5 @@ "use strict"; | ||
tslib_1.__exportStar(require("./lifecycle-registry"), exports); | ||
tslib_1.__exportStar(require("./mixin-target"), exports); | ||
tslib_1.__exportStar(require("./server"), exports); | ||
tslib_1.__exportStar(require("./service"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -7,2 +7,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CoreTags = exports.CoreBindings = void 0; | ||
const context_1 = require("@loopback/context"); | ||
@@ -9,0 +10,0 @@ /** |
@@ -7,2 +7,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.LifeCycleObserverRegistry = exports.DEFAULT_ORDERED_GROUPS = void 0; | ||
const tslib_1 = require("tslib"); | ||
@@ -18,152 +19,155 @@ const context_1 = require("@loopback/context"); | ||
*/ | ||
let LifeCycleObserverRegistry = class LifeCycleObserverRegistry { | ||
constructor(observersView, options = { | ||
parallel: true, | ||
orderedGroups: exports.DEFAULT_ORDERED_GROUPS, | ||
}) { | ||
this.observersView = observersView; | ||
this.options = options; | ||
} | ||
setOrderedGroups(groups) { | ||
this.options.orderedGroups = groups; | ||
} | ||
/** | ||
* Get observer groups ordered by the group | ||
*/ | ||
getObserverGroupsByOrder() { | ||
const bindings = this.observersView.bindings; | ||
const groups = this.sortObserverBindingsByGroup(bindings); | ||
if (debug.enabled) { | ||
debug('Observer groups: %j', groups.map(g => ({ | ||
group: g.group, | ||
bindings: g.bindings.map(b => b.key), | ||
}))); | ||
let LifeCycleObserverRegistry = /** @class */ (() => { | ||
let LifeCycleObserverRegistry = class LifeCycleObserverRegistry { | ||
constructor(observersView, options = { | ||
parallel: true, | ||
orderedGroups: exports.DEFAULT_ORDERED_GROUPS, | ||
}) { | ||
this.observersView = observersView; | ||
this.options = options; | ||
} | ||
return groups; | ||
} | ||
/** | ||
* Get the group for a given life cycle observer binding | ||
* @param binding - Life cycle observer binding | ||
*/ | ||
getObserverGroup(binding) { | ||
// First check if there is an explicit group name in the tag | ||
let group = binding.tagMap[keys_1.CoreTags.LIFE_CYCLE_OBSERVER_GROUP]; | ||
if (!group) { | ||
// Fall back to a tag that matches one of the groups | ||
group = this.options.orderedGroups.find(g => binding.tagMap[g] === g); | ||
setOrderedGroups(groups) { | ||
this.options.orderedGroups = groups; | ||
} | ||
group = group || ''; | ||
debug('Binding %s is configured with observer group %s', binding.key, group); | ||
return group; | ||
} | ||
/** | ||
* Sort the life cycle observer bindings so that we can start/stop them | ||
* in the right order. By default, we can start other observers before servers | ||
* and stop them in the reverse order | ||
* @param bindings - Life cycle observer bindings | ||
*/ | ||
sortObserverBindingsByGroup(bindings) { | ||
// Group bindings in a map | ||
const groupMap = new Map(); | ||
context_1.sortBindingsByPhase(bindings, keys_1.CoreTags.LIFE_CYCLE_OBSERVER_GROUP, this.options.orderedGroups); | ||
for (const binding of bindings) { | ||
const group = this.getObserverGroup(binding); | ||
let bindingsInGroup = groupMap.get(group); | ||
if (bindingsInGroup == null) { | ||
bindingsInGroup = []; | ||
groupMap.set(group, bindingsInGroup); | ||
/** | ||
* Get observer groups ordered by the group | ||
*/ | ||
getObserverGroupsByOrder() { | ||
const bindings = this.observersView.bindings; | ||
const groups = this.sortObserverBindingsByGroup(bindings); | ||
if (debug.enabled) { | ||
debug('Observer groups: %j', groups.map(g => ({ | ||
group: g.group, | ||
bindings: g.bindings.map(b => b.key), | ||
}))); | ||
} | ||
bindingsInGroup.push(binding); | ||
return groups; | ||
} | ||
// Create an array for group entries | ||
const groups = []; | ||
for (const [group, bindingsInGroup] of groupMap) { | ||
groups.push({ group, bindings: bindingsInGroup }); | ||
/** | ||
* Get the group for a given life cycle observer binding | ||
* @param binding - Life cycle observer binding | ||
*/ | ||
getObserverGroup(binding) { | ||
// First check if there is an explicit group name in the tag | ||
let group = binding.tagMap[keys_1.CoreTags.LIFE_CYCLE_OBSERVER_GROUP]; | ||
if (!group) { | ||
// Fall back to a tag that matches one of the groups | ||
group = this.options.orderedGroups.find(g => binding.tagMap[g] === g); | ||
} | ||
group = group || ''; | ||
debug('Binding %s is configured with observer group %s', binding.key, group); | ||
return group; | ||
} | ||
return groups; | ||
} | ||
/** | ||
* Notify an observer group of the given event | ||
* @param group - A group of bindings for life cycle observers | ||
* @param event - Event name | ||
*/ | ||
async notifyObservers(observers, bindings, event) { | ||
if (!this.options.parallel) { | ||
let index = 0; | ||
for (const observer of observers) { | ||
debug('Invoking %s observer for binding %s', event, bindings[index].key); | ||
index++; | ||
await this.invokeObserver(observer, event); | ||
/** | ||
* Sort the life cycle observer bindings so that we can start/stop them | ||
* in the right order. By default, we can start other observers before servers | ||
* and stop them in the reverse order | ||
* @param bindings - Life cycle observer bindings | ||
*/ | ||
sortObserverBindingsByGroup(bindings) { | ||
// Group bindings in a map | ||
const groupMap = new Map(); | ||
context_1.sortBindingsByPhase(bindings, keys_1.CoreTags.LIFE_CYCLE_OBSERVER_GROUP, this.options.orderedGroups); | ||
for (const binding of bindings) { | ||
const group = this.getObserverGroup(binding); | ||
let bindingsInGroup = groupMap.get(group); | ||
if (bindingsInGroup == null) { | ||
bindingsInGroup = []; | ||
groupMap.set(group, bindingsInGroup); | ||
} | ||
bindingsInGroup.push(binding); | ||
} | ||
return; | ||
// Create an array for group entries | ||
const groups = []; | ||
for (const [group, bindingsInGroup] of groupMap) { | ||
groups.push({ group, bindings: bindingsInGroup }); | ||
} | ||
return groups; | ||
} | ||
// Parallel invocation | ||
const notifiers = observers.map((observer, index) => { | ||
debug('Invoking %s observer for binding %s', event, bindings[index].key); | ||
return this.invokeObserver(observer, event); | ||
}); | ||
await Promise.all(notifiers); | ||
} | ||
/** | ||
* Invoke an observer for the given event | ||
* @param observer - A life cycle observer | ||
* @param event - Event name | ||
*/ | ||
async invokeObserver(observer, event) { | ||
if (typeof observer[event] === 'function') { | ||
await observer[event](); | ||
/** | ||
* Notify an observer group of the given event | ||
* @param group - A group of bindings for life cycle observers | ||
* @param event - Event name | ||
*/ | ||
async notifyObservers(observers, bindings, event) { | ||
if (!this.options.parallel) { | ||
let index = 0; | ||
for (const observer of observers) { | ||
debug('Invoking %s observer for binding %s', event, bindings[index].key); | ||
index++; | ||
await this.invokeObserver(observer, event); | ||
} | ||
return; | ||
} | ||
// Parallel invocation | ||
const notifiers = observers.map((observer, index) => { | ||
debug('Invoking %s observer for binding %s', event, bindings[index].key); | ||
return this.invokeObserver(observer, event); | ||
}); | ||
await Promise.all(notifiers); | ||
} | ||
} | ||
/** | ||
* Emit events to the observer groups | ||
* @param events - Event names | ||
* @param groups - Observer groups | ||
*/ | ||
async notifyGroups(events, groups, reverse = false) { | ||
const observers = await this.observersView.values(); | ||
const bindings = this.observersView.bindings; | ||
if (reverse) { | ||
// Do not reverse the original `groups` in place | ||
groups = [...groups].reverse(); | ||
/** | ||
* Invoke an observer for the given event | ||
* @param observer - A life cycle observer | ||
* @param event - Event name | ||
*/ | ||
async invokeObserver(observer, event) { | ||
if (typeof observer[event] === 'function') { | ||
await observer[event](); | ||
} | ||
} | ||
for (const group of groups) { | ||
const observersForGroup = []; | ||
const bindingsInGroup = reverse | ||
? group.bindings.reverse() | ||
: group.bindings; | ||
for (const binding of bindingsInGroup) { | ||
const index = bindings.indexOf(binding); | ||
observersForGroup.push(observers[index]); | ||
/** | ||
* Emit events to the observer groups | ||
* @param events - Event names | ||
* @param groups - Observer groups | ||
*/ | ||
async notifyGroups(events, groups, reverse = false) { | ||
const observers = await this.observersView.values(); | ||
const bindings = this.observersView.bindings; | ||
if (reverse) { | ||
// Do not reverse the original `groups` in place | ||
groups = [...groups].reverse(); | ||
} | ||
for (const event of events) { | ||
debug('Beginning notification %s of %s...', event); | ||
await this.notifyObservers(observersForGroup, group.bindings, event); | ||
debug('Finished notification %s of %s', event); | ||
for (const group of groups) { | ||
const observersForGroup = []; | ||
const bindingsInGroup = reverse | ||
? group.bindings.reverse() | ||
: group.bindings; | ||
for (const binding of bindingsInGroup) { | ||
const index = bindings.indexOf(binding); | ||
observersForGroup.push(observers[index]); | ||
} | ||
for (const event of events) { | ||
debug('Beginning notification %s of %s...', event); | ||
await this.notifyObservers(observersForGroup, group.bindings, event); | ||
debug('Finished notification %s of %s', event); | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
* Notify all life cycle observers by group of `start` | ||
*/ | ||
async start() { | ||
debug('Starting the %s...'); | ||
const groups = this.getObserverGroupsByOrder(); | ||
await this.notifyGroups(['start'], groups); | ||
} | ||
/** | ||
* Notify all life cycle observers by group of `stop` | ||
*/ | ||
async stop() { | ||
debug('Stopping the %s...'); | ||
const groups = this.getObserverGroupsByOrder(); | ||
// Stop in the reverse order | ||
await this.notifyGroups(['stop'], groups, true); | ||
} | ||
}; | ||
LifeCycleObserverRegistry = tslib_1.__decorate([ | ||
tslib_1.__param(0, context_1.inject.view(lifecycle_1.lifeCycleObserverFilter)), | ||
tslib_1.__param(1, context_1.inject(keys_1.CoreBindings.LIFE_CYCLE_OBSERVER_OPTIONS, { optional: true })), | ||
tslib_1.__metadata("design:paramtypes", [context_1.ContextView, Object]) | ||
], LifeCycleObserverRegistry); | ||
/** | ||
* Notify all life cycle observers by group of `start` | ||
*/ | ||
async start() { | ||
debug('Starting the %s...'); | ||
const groups = this.getObserverGroupsByOrder(); | ||
await this.notifyGroups(['start'], groups); | ||
} | ||
/** | ||
* Notify all life cycle observers by group of `stop` | ||
*/ | ||
async stop() { | ||
debug('Stopping the %s...'); | ||
const groups = this.getObserverGroupsByOrder(); | ||
// Stop in the reverse order | ||
await this.notifyGroups(['stop'], groups, true); | ||
} | ||
}; | ||
LifeCycleObserverRegistry = tslib_1.__decorate([ | ||
tslib_1.__param(0, context_1.inject.view(lifecycle_1.lifeCycleObserverFilter)), | ||
tslib_1.__param(1, context_1.inject(keys_1.CoreBindings.LIFE_CYCLE_OBSERVER_OPTIONS, { optional: true })), | ||
tslib_1.__metadata("design:paramtypes", [context_1.ContextView, Object]) | ||
], LifeCycleObserverRegistry); | ||
return LifeCycleObserverRegistry; | ||
})(); | ||
exports.LifeCycleObserverRegistry = LifeCycleObserverRegistry; | ||
//# sourceMappingURL=lifecycle-registry.js.map |
@@ -7,2 +7,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.lifeCycleObserver = exports.lifeCycleObserverFilter = exports.asLifeCycleObserver = exports.isLifeCycleObserverClass = exports.isLifeCycleObserver = void 0; | ||
const context_1 = require("@loopback/context"); | ||
@@ -9,0 +10,0 @@ const keys_1 = require("./keys"); |
@@ -7,2 +7,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.asService = exports.createServiceBinding = exports.filterByServiceInterface = exports.service = void 0; | ||
const context_1 = require("@loopback/context"); | ||
@@ -9,0 +10,0 @@ const keys_1 = require("./keys"); |
{ | ||
"name": "@loopback/core", | ||
"version": "2.5.0", | ||
"version": "2.6.0", | ||
"description": "LoopBack 4 core", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"engines": { | ||
@@ -22,11 +24,10 @@ "node": ">=10" | ||
"dependencies": { | ||
"@loopback/context": "^3.7.0", | ||
"@loopback/context": "^3.8.0", | ||
"debug": "^4.1.1", | ||
"p-event": "^4.1.0", | ||
"tslib": "^1.11.2" | ||
"tslib": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"@loopback/build": "^5.3.1", | ||
"@loopback/eslint-config": "^6.0.6", | ||
"@loopback/testlab": "^3.1.3", | ||
"@loopback/build": "^5.4.0", | ||
"@loopback/eslint-config": "^7.0.0", | ||
"@loopback/testlab": "^3.1.4", | ||
"@types/debug": "^4.1.5", | ||
@@ -37,4 +38,2 @@ "@types/node": "^10.17.21" | ||
"README.md", | ||
"index.js", | ||
"index.d.ts", | ||
"dist", | ||
@@ -49,3 +48,3 @@ "src", | ||
}, | ||
"gitHead": "50c3f06d942f8bae8f0c32a3775ff98a7498e4fd" | ||
"gitHead": "c8681026187471f0ceeee7c101d66feae295116b" | ||
} |
@@ -21,3 +21,3 @@ // Copyright IBM Corp. 2017,2020. All Rights Reserved. | ||
import debugFactory from 'debug'; | ||
import pEvent from 'p-event'; | ||
import {once} from 'events'; | ||
import {Component, mountComponent} from './component'; | ||
@@ -289,3 +289,3 @@ import {CoreBindings, CoreTags} from './keys'; | ||
protected async awaitState(state: string) { | ||
await pEvent(this, state); | ||
await once(this, state); | ||
} | ||
@@ -292,0 +292,0 @@ |
@@ -230,7 +230,9 @@ // Copyright IBM Corp. 2017,2020. All Rights Reserved. | ||
* extension point | ||
* @param extensionPointName - Name of the extension point | ||
* @param extensionPointNames - A list of names of extension points | ||
*/ | ||
export function extensionFilter(extensionPointName: string): BindingFilter { | ||
export function extensionFilter( | ||
...extensionPointNames: string[] | ||
): BindingFilter { | ||
return filterByTag({ | ||
[CoreTags.EXTENSION_FOR]: includesTagValue(extensionPointName), | ||
[CoreTags.EXTENSION_FOR]: includesTagValue(...extensionPointNames), | ||
}); | ||
@@ -237,0 +239,0 @@ } |
@@ -27,3 +27,4 @@ // Copyright IBM Corp. 2017,2020. All Rights Reserved. | ||
export * from './lifecycle-registry'; | ||
export * from './mixin-target'; | ||
export * from './server'; | ||
export * from './service'; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
192146
3
44
3788
- Removedp-event@^4.1.0
- Removedtslib@1.14.1(transitive)
Updated@loopback/context@^3.8.0
Updatedtslib@^2.0.0