Skip to content

Commit

Permalink
feat(context): tidy up binding information for inspection
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Feb 4, 2020
1 parent 4422c90 commit 15d698b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
8 changes: 5 additions & 3 deletions packages/context/src/binding-filter.ts
Expand Up @@ -4,7 +4,7 @@
// License text available at https://opensource.org/licenses/MIT

import {Binding, BindingTag} from './binding';
import {BindingAddress} from './binding-key';
import {BindingAddress, BindingKey} from './binding-key';
import {MapObject} from './value-promise';

/**
Expand Down Expand Up @@ -52,12 +52,14 @@ export type BindingSelector<ValueType = unknown> =

/**
* Type guard for binding address
* @param bindingSelector
* @param bindingSelector - Binding key or filter function
*/
export function isBindingAddress(
bindingSelector: BindingSelector,
): bindingSelector is BindingAddress {
return typeof bindingSelector !== 'function';
return (
typeof bindingSelector === 'string' || bindingSelector instanceof BindingKey
);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions packages/context/src/binding.ts
Expand Up @@ -210,6 +210,7 @@ export class Binding<T = BoundValue> extends EventEmitter {

private _valueConstructor?: Constructor<T>;
private _providerConstructor?: Constructor<Provider<T>>;
private _alias?: BindingAddress<T>;

/**
* For bindings bound via `toClass()`, this property contains the constructor
Expand Down Expand Up @@ -595,6 +596,7 @@ export class Binding<T = BoundValue> extends EventEmitter {
debug('Bind %s to alias %s', this.key, keyWithPath);
}
this._type = BindingType.ALIAS;
this._alias = keyWithPath;
this._setValueGetter((ctx, options) => {
return ctx.getValueOrPromise(keyWithPath, options);
});
Expand Down Expand Up @@ -649,6 +651,9 @@ export class Binding<T = BoundValue> extends EventEmitter {
if (this._providerConstructor != null) {
json.providerConstructor = this._providerConstructor.name;
}
if (this._alias != null) {
json.alias = this._alias.toString();
}
return json;
}

Expand Down
11 changes: 4 additions & 7 deletions packages/context/src/resolution-session.ts
Expand Up @@ -155,9 +155,7 @@ export class ResolutionSession {
* Describe the injection for debugging purpose
* @param injection - Injection object
*/
static describeInjection(injection?: Readonly<Injection>) {
/* istanbul ignore if */
if (injection == null) return {};
static describeInjection(injection: Readonly<Injection>) {
const name = getTargetName(
injection.target,
injection.member,
Expand All @@ -166,8 +164,7 @@ export class ResolutionSession {
return {
targetName: name,
bindingSelector: injection.bindingSelector,
// Cast to Object so that we don't have to expose InjectionMetadata
metadata: injection.metadata as object,
metadata: injection.metadata,
};
}

Expand Down Expand Up @@ -300,14 +297,14 @@ export class ResolutionSession {
*/
getInjectionPath() {
return this.injectionStack
.map(i => ResolutionSession.describeInjection(i)!.targetName)
.map(i => ResolutionSession.describeInjection(i).targetName)
.join(' --> ');
}

private static describe(e: ResolutionElement) {
switch (e.type) {
case 'injection':
return '@' + ResolutionSession.describeInjection(e.value)!.targetName;
return '@' + ResolutionSession.describeInjection(e.value).targetName;
case 'binding':
return e.value.key;
}
Expand Down

0 comments on commit 15d698b

Please sign in to comment.