@loopback/context
Advanced tools
Comparing version 3.3.0 to 3.4.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [3.4.0](https://github.com/strongloop/loopback-next/compare/@loopback/context@3.3.0...@loopback/context@3.4.0) (2020-04-11) | ||
### Features | ||
* **context:** expose debug method to subclasses ([f651e5b](https://github.com/strongloop/loopback-next/commit/f651e5be28efe624e83a7139152ed00554580dca)) | ||
# [3.3.0](https://github.com/strongloop/loopback-next/compare/@loopback/context@3.2.0...@loopback/context@3.3.0) (2020-04-08) | ||
@@ -8,0 +19,0 @@ |
/// <reference types="node" /> | ||
import { Debugger } from 'debug'; | ||
import { EventEmitter } from 'events'; | ||
@@ -45,2 +46,16 @@ import { Binding, BindingInspectOptions, BindingTag } from './binding'; | ||
/** | ||
* A debug function which can be overridden by subclasses. | ||
* | ||
* @example | ||
* ```ts | ||
* import debugFactory from 'debug'; | ||
* const debug = debugFactory('loopback:context:application'); | ||
* export class Application extends Context { | ||
* super('application'); | ||
* this._debug = debug; | ||
* } | ||
* ``` | ||
*/ | ||
protected _debug: Debugger; | ||
/** | ||
* Create a new context. | ||
@@ -68,2 +83,18 @@ * | ||
constructor(_parent?: Context | string, name?: string); | ||
/** | ||
* Get the debug namespace for the context class. Subclasses can override | ||
* this method to supply its own namespace. | ||
* | ||
* @example | ||
* ```ts | ||
* export class Application extends Context { | ||
* super('application'); | ||
* } | ||
* | ||
* protected getDebugNamespace() { | ||
* return 'loopback:context:application'; | ||
* } | ||
* ``` | ||
*/ | ||
protected getDebugNamespace(): string; | ||
private generateName; | ||
@@ -80,3 +111,3 @@ /** | ||
*/ | ||
private _debug; | ||
protected debug(...args: unknown[]): void; | ||
/** | ||
@@ -83,0 +114,0 @@ * A strongly-typed method to emit context events |
@@ -21,3 +21,2 @@ "use strict"; | ||
const value_promise_1 = require("./value-promise"); | ||
const debug = debug_1.default('loopback:context'); | ||
/** | ||
@@ -68,9 +67,30 @@ * Context provides an implementation of Inversion of Control (IoC) container | ||
this.subscriptionManager = new context_subscription_1.ContextSubscriptionManager(this); | ||
this._debug = debug_1.default(this.getDebugNamespace()); | ||
} | ||
/** | ||
* Get the debug namespace for the context class. Subclasses can override | ||
* this method to supply its own namespace. | ||
* | ||
* @example | ||
* ```ts | ||
* export class Application extends Context { | ||
* super('application'); | ||
* } | ||
* | ||
* protected getDebugNamespace() { | ||
* return 'loopback:context:application'; | ||
* } | ||
* ``` | ||
*/ | ||
getDebugNamespace() { | ||
if (this.constructor === Context) | ||
return 'loopback:context'; | ||
const name = this.constructor.name.toLowerCase(); | ||
return `loopback:context:${name}`; | ||
} | ||
generateName() { | ||
const id = uuid_1.v1(); | ||
let prefix = `${this.constructor.name}-`; | ||
if (prefix === 'Context-') | ||
prefix = ''; | ||
return `${prefix}${id}`; | ||
const id = uuid_1.v4(); | ||
if (this.constructor === Context) | ||
return id; | ||
return `${this.constructor.name}-${id}`; | ||
} | ||
@@ -89,12 +109,12 @@ /** | ||
*/ | ||
_debug(...args) { | ||
debug(...args) { | ||
/* istanbul ignore if */ | ||
if (!debug.enabled) | ||
if (!this._debug.enabled) | ||
return; | ||
const formatter = args.shift(); | ||
if (typeof formatter === 'string') { | ||
debug(`[%s] ${formatter}`, this.name, ...args); | ||
this._debug(`[%s] ${formatter}`, this.name, ...args); | ||
} | ||
else { | ||
debug('[%s] ', this.name, formatter, ...args); | ||
this._debug('[%s] ', this.name, formatter, ...args); | ||
} | ||
@@ -135,3 +155,3 @@ } | ||
const key = binding.key; | ||
this._debug('[%s] Adding binding: %s', key); | ||
this.debug('[%s] Adding binding: %s', key); | ||
let existingBinding; | ||
@@ -197,3 +217,3 @@ const keyExists = this.registry.has(key); | ||
if (configResolver) { | ||
debug('Custom ConfigurationResolver is loaded from %s.', keys_1.ContextBindings.CONFIGURATION_RESOLVER.toString()); | ||
this.debug('Custom ConfigurationResolver is loaded from %s.', keys_1.ContextBindings.CONFIGURATION_RESOLVER.toString()); | ||
this.configResolver = configResolver; | ||
@@ -203,3 +223,3 @@ } | ||
// Fallback to DefaultConfigurationResolver | ||
debug('DefaultConfigurationResolver is used.'); | ||
this.debug('DefaultConfigurationResolver is used.'); | ||
this.configResolver = new binding_config_1.DefaultConfigurationResolver(this); | ||
@@ -255,3 +275,3 @@ } | ||
unbind(key) { | ||
this._debug('Unbind %s', key); | ||
this.debug('Unbind %s', key); | ||
key = binding_key_1.BindingKey.validate(key); | ||
@@ -292,3 +312,3 @@ const binding = this.registry.get(key); | ||
close() { | ||
this._debug('Closing context...'); | ||
this.debug('Closing context...'); | ||
this.subscriptionManager.close(); | ||
@@ -413,3 +433,3 @@ this.tagIndexer.close(); | ||
async get(keyWithPath, optionsOrSession) { | ||
this._debug('Resolving binding: %s', keyWithPath); | ||
this.debug('Resolving binding: %s', keyWithPath); | ||
return this.getValueOrPromise(keyWithPath, optionsOrSession); | ||
@@ -419,3 +439,3 @@ } | ||
getSync(keyWithPath, optionsOrSession) { | ||
this._debug('Resolving binding synchronously: %s', keyWithPath); | ||
this.debug('Resolving binding synchronously: %s', keyWithPath); | ||
const valueOrPromise = this.getValueOrPromise(keyWithPath, optionsOrSession); | ||
@@ -422,0 +442,0 @@ if (value_promise_1.isPromiseLike(valueOrPromise)) { |
{ | ||
"name": "@loopback/context", | ||
"version": "3.3.0", | ||
"version": "3.4.0", | ||
"description": "LoopBack's container for Inversion of Control", | ||
@@ -21,3 +21,4 @@ "engines": { | ||
"dependencies": { | ||
"@loopback/metadata": "^2.0.3", | ||
"@loopback/metadata": "^2.0.4", | ||
"@types/debug": "^4.1.5", | ||
"debug": "^4.1.1", | ||
@@ -31,5 +32,4 @@ "p-event": "^4.1.0", | ||
"@loopback/eslint-config": "^6.0.3", | ||
"@loopback/testlab": "^3.0.0", | ||
"@loopback/testlab": "^3.0.1", | ||
"@types/bluebird": "^3.5.30", | ||
"@types/debug": "^4.1.5", | ||
"@types/node": "^10.17.19", | ||
@@ -61,3 +61,3 @@ "@types/uuid": "^7.0.2", | ||
}, | ||
"gitHead": "744a85c76a2dd260f6dc21e5b497f14eccaf099d" | ||
"gitHead": "7784cf1ef0c58220b32e2d9e35e5b0ef34825483" | ||
} |
@@ -6,5 +6,5 @@ // Copyright IBM Corp. 2017,2020. All Rights Reserved. | ||
import debugFactory from 'debug'; | ||
import debugFactory, {Debugger} from 'debug'; | ||
import {EventEmitter} from 'events'; | ||
import {v1 as uuidv1} from 'uuid'; | ||
import {v4 as uuidv4} from 'uuid'; | ||
import {Binding, BindingInspectOptions, BindingTag} from './binding'; | ||
@@ -44,4 +44,2 @@ import { | ||
const debug = debugFactory('loopback:context'); | ||
/** | ||
@@ -82,2 +80,17 @@ * Context provides an implementation of Inversion of Control (IoC) container | ||
/** | ||
* A debug function which can be overridden by subclasses. | ||
* | ||
* @example | ||
* ```ts | ||
* import debugFactory from 'debug'; | ||
* const debug = debugFactory('loopback:context:application'); | ||
* export class Application extends Context { | ||
* super('application'); | ||
* this._debug = debug; | ||
* } | ||
* ``` | ||
*/ | ||
protected _debug: Debugger; | ||
/** | ||
* Create a new context. | ||
@@ -119,9 +132,30 @@ * | ||
this.subscriptionManager = new ContextSubscriptionManager(this); | ||
this._debug = debugFactory(this.getDebugNamespace()); | ||
} | ||
/** | ||
* Get the debug namespace for the context class. Subclasses can override | ||
* this method to supply its own namespace. | ||
* | ||
* @example | ||
* ```ts | ||
* export class Application extends Context { | ||
* super('application'); | ||
* } | ||
* | ||
* protected getDebugNamespace() { | ||
* return 'loopback:context:application'; | ||
* } | ||
* ``` | ||
*/ | ||
protected getDebugNamespace() { | ||
if (this.constructor === Context) return 'loopback:context'; | ||
const name = this.constructor.name.toLowerCase(); | ||
return `loopback:context:${name}`; | ||
} | ||
private generateName() { | ||
const id = uuidv1(); | ||
let prefix = `${this.constructor.name}-`; | ||
if (prefix === 'Context-') prefix = ''; | ||
return `${prefix}${id}`; | ||
const id = uuidv4(); | ||
if (this.constructor === Context) return id; | ||
return `${this.constructor.name}-${id}`; | ||
} | ||
@@ -142,10 +176,10 @@ | ||
*/ | ||
private _debug(...args: unknown[]) { | ||
protected debug(...args: unknown[]) { | ||
/* istanbul ignore if */ | ||
if (!debug.enabled) return; | ||
if (!this._debug.enabled) return; | ||
const formatter = args.shift(); | ||
if (typeof formatter === 'string') { | ||
debug(`[%s] ${formatter}`, this.name, ...args); | ||
this._debug(`[%s] ${formatter}`, this.name, ...args); | ||
} else { | ||
debug('[%s] ', this.name, formatter, ...args); | ||
this._debug('[%s] ', this.name, formatter, ...args); | ||
} | ||
@@ -192,3 +226,3 @@ } | ||
const key = binding.key; | ||
this._debug('[%s] Adding binding: %s', key); | ||
this.debug('[%s] Adding binding: %s', key); | ||
let existingBinding: Binding | undefined; | ||
@@ -270,3 +304,3 @@ const keyExists = this.registry.has(key); | ||
if (configResolver) { | ||
debug( | ||
this.debug( | ||
'Custom ConfigurationResolver is loaded from %s.', | ||
@@ -278,3 +312,3 @@ ContextBindings.CONFIGURATION_RESOLVER.toString(), | ||
// Fallback to DefaultConfigurationResolver | ||
debug('DefaultConfigurationResolver is used.'); | ||
this.debug('DefaultConfigurationResolver is used.'); | ||
this.configResolver = new DefaultConfigurationResolver(this); | ||
@@ -351,3 +385,3 @@ } | ||
unbind(key: BindingAddress): boolean { | ||
this._debug('Unbind %s', key); | ||
this.debug('Unbind %s', key); | ||
key = BindingKey.validate(key); | ||
@@ -390,3 +424,3 @@ const binding = this.registry.get(key); | ||
close() { | ||
this._debug('Closing context...'); | ||
this.debug('Closing context...'); | ||
this.subscriptionManager.close(); | ||
@@ -590,3 +624,3 @@ this.tagIndexer.close(); | ||
): Promise<ValueType | undefined> { | ||
this._debug('Resolving binding: %s', keyWithPath); | ||
this.debug('Resolving binding: %s', keyWithPath); | ||
return this.getValueOrPromise<ValueType | undefined>( | ||
@@ -659,3 +693,3 @@ keyWithPath, | ||
): ValueType | undefined { | ||
this._debug('Resolving binding synchronously: %s', keyWithPath); | ||
this.debug('Resolving binding synchronously: %s', keyWithPath); | ||
@@ -662,0 +696,0 @@ const valueOrPromise = this.getValueOrPromise<ValueType>( |
Sorry, the diff of this file is not supported yet
621009
7
13707
6
+ Added@types/debug@^4.1.5
+ Added@types/debug@4.1.12(transitive)
+ Added@types/ms@0.7.34(transitive)
Updated@loopback/metadata@^2.0.4