@loopback/context
Advanced tools
Comparing version 4.0.0-alpha.9 to 4.0.0-alpha.10
@@ -6,2 +6,71 @@ import { Context } from './context'; | ||
export declare type ValueOrPromise<T> = T | Promise<T>; | ||
/** | ||
* Scope for binding values | ||
*/ | ||
export declare enum BindingScope { | ||
/** | ||
* The binding provides a value that is calculated each time. This will be | ||
* the default scope if not set. | ||
* | ||
* For example, with the following context hierarchy: | ||
* | ||
* - app (with a binding 'b1' that produces sequential values 0, 1, ...) | ||
* - req1 | ||
* - req2 | ||
* | ||
* // get('b1') produces a new value each time for app and its descendants | ||
* app.get('b1') ==> 0 | ||
* req1.get('b1') ==> 1 | ||
* req2.get('b1') ==> 2 | ||
* req2.get('b1') ==> 3 | ||
* app.get('b1') ==> 4 | ||
*/ | ||
TRANSIENT = 0, | ||
/** | ||
* The binding provides a value as a singleton within each local context. The | ||
* value is calculated only once per context and cached for subsequenicial | ||
* uses. Child contexts have their own value and do not share with their | ||
* ancestors. | ||
* | ||
* For example, with the following context hierarchy: | ||
* | ||
* - app (with a binding 'b1' that produces sequential values 0, 1, ...) | ||
* - req1 | ||
* - req2 | ||
* | ||
* // 0 is the singleton for app afterward | ||
* app.get('b1') ==> 0 | ||
* | ||
* // 'b1' is found in app but not in req1, a new value 1 is calculated. | ||
* // 1 is the singleton for req1 afterward | ||
* req1.get('b1') ==> 1 | ||
* | ||
* // 'b1' is found in app but not in req2, a new value 2 is calculated. | ||
* // 2 is the singleton for req2 afterward | ||
* req2.get('b1') ==> 2 | ||
*/ | ||
CONTEXT = 1, | ||
/** | ||
* The binding provides a value as a singleton within the context hierarchy | ||
* (the owning context and its descendants). The value is calculated only | ||
* once for the owning context and cached for subsequenicial uses. Child | ||
* contexts share the same value as their ancestors. | ||
* | ||
* For example, with the following context hierarchy: | ||
* | ||
* - app (with a binding 'b1' that produces sequential values 0, 1, ...) | ||
* - req1 | ||
* - req2 | ||
* | ||
* // 0 is the singleton for app afterward | ||
* app.get('b1') ==> 0 | ||
* | ||
* // 'b1' is found in app, reuse it | ||
* req1.get('b1') ==> 0 | ||
* | ||
* // 'b1' is found in app, reuse it | ||
* req2.get('b1') ==> 0 | ||
*/ | ||
SINGLETON = 2, | ||
} | ||
export declare class Binding { | ||
@@ -25,9 +94,16 @@ isLocked: boolean; | ||
static getKeyPath(key: string): string | undefined; | ||
private readonly _key; | ||
private _tags; | ||
valueConstructor: Constructor<BoundValue>; | ||
constructor(_key: string, isLocked?: boolean); | ||
readonly key: string; | ||
readonly tags: Set<string>; | ||
scope: BindingScope; | ||
private _cache; | ||
private _getValue; | ||
valueConstructor: Constructor<BoundValue>; | ||
constructor(key: string, isLocked?: boolean); | ||
/** | ||
* Cache the resolved value by the binding scope | ||
* @param ctx The current context | ||
* @param result The calculated value for the binding | ||
*/ | ||
private _cacheValue(ctx, result); | ||
/** | ||
* This is an internal function optimized for performance. | ||
@@ -56,2 +132,3 @@ * Users should use `@inject(key)` or `ctx.get(key)` instead. | ||
tag(tagName: string | string[]): this; | ||
inScope(scope: BindingScope): void; | ||
/** | ||
@@ -58,0 +135,0 @@ * Bind the key to a constant value. |
@@ -9,10 +9,81 @@ "use strict"; | ||
const is_promise_1 = require("./is-promise"); | ||
/** | ||
* Scope for binding values | ||
*/ | ||
var BindingScope; | ||
(function (BindingScope) { | ||
/** | ||
* The binding provides a value that is calculated each time. This will be | ||
* the default scope if not set. | ||
* | ||
* For example, with the following context hierarchy: | ||
* | ||
* - app (with a binding 'b1' that produces sequential values 0, 1, ...) | ||
* - req1 | ||
* - req2 | ||
* | ||
* // get('b1') produces a new value each time for app and its descendants | ||
* app.get('b1') ==> 0 | ||
* req1.get('b1') ==> 1 | ||
* req2.get('b1') ==> 2 | ||
* req2.get('b1') ==> 3 | ||
* app.get('b1') ==> 4 | ||
*/ | ||
BindingScope[BindingScope["TRANSIENT"] = 0] = "TRANSIENT"; | ||
/** | ||
* The binding provides a value as a singleton within each local context. The | ||
* value is calculated only once per context and cached for subsequenicial | ||
* uses. Child contexts have their own value and do not share with their | ||
* ancestors. | ||
* | ||
* For example, with the following context hierarchy: | ||
* | ||
* - app (with a binding 'b1' that produces sequential values 0, 1, ...) | ||
* - req1 | ||
* - req2 | ||
* | ||
* // 0 is the singleton for app afterward | ||
* app.get('b1') ==> 0 | ||
* | ||
* // 'b1' is found in app but not in req1, a new value 1 is calculated. | ||
* // 1 is the singleton for req1 afterward | ||
* req1.get('b1') ==> 1 | ||
* | ||
* // 'b1' is found in app but not in req2, a new value 2 is calculated. | ||
* // 2 is the singleton for req2 afterward | ||
* req2.get('b1') ==> 2 | ||
*/ | ||
BindingScope[BindingScope["CONTEXT"] = 1] = "CONTEXT"; | ||
/** | ||
* The binding provides a value as a singleton within the context hierarchy | ||
* (the owning context and its descendants). The value is calculated only | ||
* once for the owning context and cached for subsequenicial uses. Child | ||
* contexts share the same value as their ancestors. | ||
* | ||
* For example, with the following context hierarchy: | ||
* | ||
* - app (with a binding 'b1' that produces sequential values 0, 1, ...) | ||
* - req1 | ||
* - req2 | ||
* | ||
* // 0 is the singleton for app afterward | ||
* app.get('b1') ==> 0 | ||
* | ||
* // 'b1' is found in app, reuse it | ||
* req1.get('b1') ==> 0 | ||
* | ||
* // 'b1' is found in app, reuse it | ||
* req2.get('b1') ==> 0 | ||
*/ | ||
BindingScope[BindingScope["SINGLETON"] = 2] = "SINGLETON"; | ||
})(BindingScope = exports.BindingScope || (exports.BindingScope = {})); | ||
// FIXME(bajtos) The binding class should be parameterized by the value | ||
// type stored | ||
class Binding { | ||
constructor(_key, isLocked = false) { | ||
constructor(key, isLocked = false) { | ||
this.isLocked = isLocked; | ||
this._tags = new Set(); | ||
Binding.validateKey(_key); | ||
this._key = _key; | ||
this.tags = new Set(); | ||
this.scope = BindingScope.TRANSIENT; | ||
Binding.validateKey(key); | ||
this.key = key; | ||
} | ||
@@ -26,3 +97,3 @@ /** | ||
throw new Error('Binding key must be provided.'); | ||
if (key.indexOf(Binding.PROPERTY_SEPARATOR) !== -1) { | ||
if (key.includes(Binding.PROPERTY_SEPARATOR)) { | ||
throw new Error(`Binding key ${key} cannot contain` | ||
@@ -54,8 +125,49 @@ + ` '${Binding.PROPERTY_SEPARATOR}'.`); | ||
} | ||
get key() { | ||
return this._key; | ||
/** | ||
* Cache the resolved value by the binding scope | ||
* @param ctx The current context | ||
* @param result The calculated value for the binding | ||
*/ | ||
_cacheValue(ctx, result) { | ||
if (is_promise_1.isPromise(result)) { | ||
if (this.scope === BindingScope.SINGLETON) { | ||
// Cache the value | ||
result = result.then(val => { | ||
this._cache = val; | ||
return val; | ||
}); | ||
} | ||
else if (this.scope === BindingScope.CONTEXT) { | ||
// Cache the value | ||
result = result.then(val => { | ||
if (ctx.contains(this.key)) { | ||
// The ctx owns the binding | ||
this._cache = val; | ||
} | ||
else { | ||
// Create a binding of the cached value for the current context | ||
ctx.bind(this.key).to(val); | ||
} | ||
return val; | ||
}); | ||
} | ||
} | ||
else { | ||
if (this.scope === BindingScope.SINGLETON) { | ||
// Cache the value | ||
this._cache = result; | ||
} | ||
else if (this.scope === BindingScope.CONTEXT) { | ||
if (ctx.contains(this.key)) { | ||
// The ctx owns the binding | ||
this._cache = result; | ||
} | ||
else { | ||
// Create a binding of the cached value for the current context | ||
ctx.bind(this.key).to(result); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
get tags() { | ||
return this._tags; | ||
} | ||
/** | ||
@@ -83,3 +195,18 @@ * This is an internal function optimized for performance. | ||
getValue(ctx) { | ||
return Promise.reject(new Error(`No value was configured for binding ${this._key}.`)); | ||
// First check cached value for non-transient | ||
if (this._cache !== undefined) { | ||
if (this.scope === BindingScope.SINGLETON) { | ||
return this._cache; | ||
} | ||
else if (this.scope === BindingScope.CONTEXT) { | ||
if (ctx.contains(this.key)) { | ||
return this._cache; | ||
} | ||
} | ||
} | ||
if (this._getValue) { | ||
const result = this._getValue(ctx); | ||
return this._cacheValue(ctx, result); | ||
} | ||
return Promise.reject(new Error(`No value was configured for binding ${this.key}.`)); | ||
} | ||
@@ -92,7 +219,7 @@ lock() { | ||
if (typeof tagName === 'string') { | ||
this._tags.add(tagName); | ||
this.tags.add(tagName); | ||
} | ||
else { | ||
tagName.forEach(t => { | ||
this._tags.add(t); | ||
this.tags.add(t); | ||
}); | ||
@@ -102,2 +229,5 @@ } | ||
} | ||
inScope(scope) { | ||
this.scope = scope; | ||
} | ||
/** | ||
@@ -115,3 +245,3 @@ * Bind the key to a constant value. | ||
to(value) { | ||
this.getValue = () => value; | ||
this._getValue = () => value; | ||
return this; | ||
@@ -139,3 +269,3 @@ } | ||
// TODO(bajtos) allow factoryFn with @inject arguments | ||
this.getValue = ctx => factoryFn(); | ||
this._getValue = ctx => factoryFn(); | ||
return this; | ||
@@ -147,3 +277,3 @@ } | ||
toProvider(providerClass) { | ||
this.getValue = ctx => { | ||
this._getValue = ctx => { | ||
const providerOrPromise = resolver_1.instantiateClass(providerClass, ctx); | ||
@@ -167,3 +297,3 @@ if (is_promise_1.isPromise(providerOrPromise)) { | ||
toClass(ctor) { | ||
this.getValue = context => resolver_1.instantiateClass(ctor, context); | ||
this._getValue = ctx => resolver_1.instantiateClass(ctor, ctx); | ||
this.valueConstructor = ctor; | ||
@@ -170,0 +300,0 @@ return this; |
@@ -5,3 +5,3 @@ import { Binding, BoundValue } from './binding'; | ||
private registry; | ||
constructor(_parent?: Context); | ||
constructor(_parent?: Context | undefined); | ||
bind(key: string): Binding; | ||
@@ -8,0 +8,0 @@ contains(key: string): boolean; |
@@ -1,2 +0,2 @@ | ||
export { Binding, BoundValue } from './binding'; | ||
export { Binding, BindingScope, BoundValue, ValueOrPromise } from './binding'; | ||
export { Context } from './context'; | ||
@@ -3,0 +3,0 @@ export { Constructor } from './resolver'; |
@@ -9,2 +9,3 @@ "use strict"; | ||
exports.Binding = binding_1.Binding; | ||
exports.BindingScope = binding_1.BindingScope; | ||
var context_1 = require("./context"); | ||
@@ -11,0 +12,0 @@ exports.Context = context_1.Context; |
@@ -1,1 +0,7 @@ | ||
export declare function isPromise<T>(value: T | Promise<T>): value is Promise<T>; | ||
/** | ||
* Check whether a value is a Promise-like instance. | ||
* Recognizes both native promises and third-party promise libraries. | ||
* | ||
* @param value The value to check. | ||
*/ | ||
export declare function isPromise<T>(value: T | PromiseLike<T>): value is PromiseLike<T>; |
@@ -7,2 +7,8 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Check whether a value is a Promise-like instance. | ||
* Recognizes both native promises and third-party promise libraries. | ||
* | ||
* @param value The value to check. | ||
*/ | ||
function isPromise(value) { | ||
@@ -9,0 +15,0 @@ if (!value) |
@@ -7,3 +7,3 @@ import 'reflect-metadata'; | ||
*/ | ||
constructor(namespace?: string); | ||
constructor(namespace?: string | undefined); | ||
private getMetadataKey(metadataKey); | ||
@@ -33,3 +33,3 @@ /** | ||
getOwnMetadataKeys(target: Object, propertyKey?: string | symbol): string[]; | ||
decorate(decorators: (PropertyDecorator | MethodDecorator)[] | ClassDecorator[], target: Object, targetKey?: string | symbol, descriptor?: PropertyDescriptor): PropertyDescriptor; | ||
decorate(decorators: (PropertyDecorator | MethodDecorator)[] | ClassDecorator[], target: Object, targetKey?: string | symbol, descriptor?: PropertyDescriptor): PropertyDescriptor | Function; | ||
metadata(metadataKey: string, metadataValue: any): { | ||
@@ -36,0 +36,0 @@ (target: Function): void; |
import { Context } from './context'; | ||
import { BoundValue } from './binding'; | ||
/** | ||
* A class constructor accepting arbitrary arguments. | ||
*/ | ||
export declare type Constructor<T> = new (...args: any[]) => T; | ||
@@ -4,0 +7,0 @@ /** |
@@ -6,2 +6,71 @@ import { Context } from './context'; | ||
export declare type ValueOrPromise<T> = T | Promise<T>; | ||
/** | ||
* Scope for binding values | ||
*/ | ||
export declare enum BindingScope { | ||
/** | ||
* The binding provides a value that is calculated each time. This will be | ||
* the default scope if not set. | ||
* | ||
* For example, with the following context hierarchy: | ||
* | ||
* - app (with a binding 'b1' that produces sequential values 0, 1, ...) | ||
* - req1 | ||
* - req2 | ||
* | ||
* // get('b1') produces a new value each time for app and its descendants | ||
* app.get('b1') ==> 0 | ||
* req1.get('b1') ==> 1 | ||
* req2.get('b1') ==> 2 | ||
* req2.get('b1') ==> 3 | ||
* app.get('b1') ==> 4 | ||
*/ | ||
TRANSIENT = 0, | ||
/** | ||
* The binding provides a value as a singleton within each local context. The | ||
* value is calculated only once per context and cached for subsequenicial | ||
* uses. Child contexts have their own value and do not share with their | ||
* ancestors. | ||
* | ||
* For example, with the following context hierarchy: | ||
* | ||
* - app (with a binding 'b1' that produces sequential values 0, 1, ...) | ||
* - req1 | ||
* - req2 | ||
* | ||
* // 0 is the singleton for app afterward | ||
* app.get('b1') ==> 0 | ||
* | ||
* // 'b1' is found in app but not in req1, a new value 1 is calculated. | ||
* // 1 is the singleton for req1 afterward | ||
* req1.get('b1') ==> 1 | ||
* | ||
* // 'b1' is found in app but not in req2, a new value 2 is calculated. | ||
* // 2 is the singleton for req2 afterward | ||
* req2.get('b1') ==> 2 | ||
*/ | ||
CONTEXT = 1, | ||
/** | ||
* The binding provides a value as a singleton within the context hierarchy | ||
* (the owning context and its descendants). The value is calculated only | ||
* once for the owning context and cached for subsequenicial uses. Child | ||
* contexts share the same value as their ancestors. | ||
* | ||
* For example, with the following context hierarchy: | ||
* | ||
* - app (with a binding 'b1' that produces sequential values 0, 1, ...) | ||
* - req1 | ||
* - req2 | ||
* | ||
* // 0 is the singleton for app afterward | ||
* app.get('b1') ==> 0 | ||
* | ||
* // 'b1' is found in app, reuse it | ||
* req1.get('b1') ==> 0 | ||
* | ||
* // 'b1' is found in app, reuse it | ||
* req2.get('b1') ==> 0 | ||
*/ | ||
SINGLETON = 2, | ||
} | ||
export declare class Binding { | ||
@@ -25,9 +94,16 @@ isLocked: boolean; | ||
static getKeyPath(key: string): string | undefined; | ||
private readonly _key; | ||
private _tags; | ||
valueConstructor: Constructor<BoundValue>; | ||
constructor(_key: string, isLocked?: boolean); | ||
readonly key: string; | ||
readonly tags: Set<string>; | ||
scope: BindingScope; | ||
private _cache; | ||
private _getValue; | ||
valueConstructor: Constructor<BoundValue>; | ||
constructor(key: string, isLocked?: boolean); | ||
/** | ||
* Cache the resolved value by the binding scope | ||
* @param ctx The current context | ||
* @param result The calculated value for the binding | ||
*/ | ||
private _cacheValue(ctx, result); | ||
/** | ||
* This is an internal function optimized for performance. | ||
@@ -56,2 +132,3 @@ * Users should use `@inject(key)` or `ctx.get(key)` instead. | ||
tag(tagName: string | string[]): this; | ||
inScope(scope: BindingScope): void; | ||
/** | ||
@@ -58,0 +135,0 @@ * Bind the key to a constant value. |
@@ -9,10 +9,81 @@ "use strict"; | ||
const is_promise_1 = require("./is-promise"); | ||
/** | ||
* Scope for binding values | ||
*/ | ||
var BindingScope; | ||
(function (BindingScope) { | ||
/** | ||
* The binding provides a value that is calculated each time. This will be | ||
* the default scope if not set. | ||
* | ||
* For example, with the following context hierarchy: | ||
* | ||
* - app (with a binding 'b1' that produces sequential values 0, 1, ...) | ||
* - req1 | ||
* - req2 | ||
* | ||
* // get('b1') produces a new value each time for app and its descendants | ||
* app.get('b1') ==> 0 | ||
* req1.get('b1') ==> 1 | ||
* req2.get('b1') ==> 2 | ||
* req2.get('b1') ==> 3 | ||
* app.get('b1') ==> 4 | ||
*/ | ||
BindingScope[BindingScope["TRANSIENT"] = 0] = "TRANSIENT"; | ||
/** | ||
* The binding provides a value as a singleton within each local context. The | ||
* value is calculated only once per context and cached for subsequenicial | ||
* uses. Child contexts have their own value and do not share with their | ||
* ancestors. | ||
* | ||
* For example, with the following context hierarchy: | ||
* | ||
* - app (with a binding 'b1' that produces sequential values 0, 1, ...) | ||
* - req1 | ||
* - req2 | ||
* | ||
* // 0 is the singleton for app afterward | ||
* app.get('b1') ==> 0 | ||
* | ||
* // 'b1' is found in app but not in req1, a new value 1 is calculated. | ||
* // 1 is the singleton for req1 afterward | ||
* req1.get('b1') ==> 1 | ||
* | ||
* // 'b1' is found in app but not in req2, a new value 2 is calculated. | ||
* // 2 is the singleton for req2 afterward | ||
* req2.get('b1') ==> 2 | ||
*/ | ||
BindingScope[BindingScope["CONTEXT"] = 1] = "CONTEXT"; | ||
/** | ||
* The binding provides a value as a singleton within the context hierarchy | ||
* (the owning context and its descendants). The value is calculated only | ||
* once for the owning context and cached for subsequenicial uses. Child | ||
* contexts share the same value as their ancestors. | ||
* | ||
* For example, with the following context hierarchy: | ||
* | ||
* - app (with a binding 'b1' that produces sequential values 0, 1, ...) | ||
* - req1 | ||
* - req2 | ||
* | ||
* // 0 is the singleton for app afterward | ||
* app.get('b1') ==> 0 | ||
* | ||
* // 'b1' is found in app, reuse it | ||
* req1.get('b1') ==> 0 | ||
* | ||
* // 'b1' is found in app, reuse it | ||
* req2.get('b1') ==> 0 | ||
*/ | ||
BindingScope[BindingScope["SINGLETON"] = 2] = "SINGLETON"; | ||
})(BindingScope = exports.BindingScope || (exports.BindingScope = {})); | ||
// FIXME(bajtos) The binding class should be parameterized by the value | ||
// type stored | ||
class Binding { | ||
constructor(_key, isLocked = false) { | ||
constructor(key, isLocked = false) { | ||
this.isLocked = isLocked; | ||
this._tags = new Set(); | ||
Binding.validateKey(_key); | ||
this._key = _key; | ||
this.tags = new Set(); | ||
this.scope = BindingScope.TRANSIENT; | ||
Binding.validateKey(key); | ||
this.key = key; | ||
} | ||
@@ -26,3 +97,3 @@ /** | ||
throw new Error('Binding key must be provided.'); | ||
if (key.indexOf(Binding.PROPERTY_SEPARATOR) !== -1) { | ||
if (key.includes(Binding.PROPERTY_SEPARATOR)) { | ||
throw new Error(`Binding key ${key} cannot contain` | ||
@@ -54,8 +125,49 @@ + ` '${Binding.PROPERTY_SEPARATOR}'.`); | ||
} | ||
get key() { | ||
return this._key; | ||
/** | ||
* Cache the resolved value by the binding scope | ||
* @param ctx The current context | ||
* @param result The calculated value for the binding | ||
*/ | ||
_cacheValue(ctx, result) { | ||
if (is_promise_1.isPromise(result)) { | ||
if (this.scope === BindingScope.SINGLETON) { | ||
// Cache the value | ||
result = result.then(val => { | ||
this._cache = val; | ||
return val; | ||
}); | ||
} | ||
else if (this.scope === BindingScope.CONTEXT) { | ||
// Cache the value | ||
result = result.then(val => { | ||
if (ctx.contains(this.key)) { | ||
// The ctx owns the binding | ||
this._cache = val; | ||
} | ||
else { | ||
// Create a binding of the cached value for the current context | ||
ctx.bind(this.key).to(val); | ||
} | ||
return val; | ||
}); | ||
} | ||
} | ||
else { | ||
if (this.scope === BindingScope.SINGLETON) { | ||
// Cache the value | ||
this._cache = result; | ||
} | ||
else if (this.scope === BindingScope.CONTEXT) { | ||
if (ctx.contains(this.key)) { | ||
// The ctx owns the binding | ||
this._cache = result; | ||
} | ||
else { | ||
// Create a binding of the cached value for the current context | ||
ctx.bind(this.key).to(result); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
get tags() { | ||
return this._tags; | ||
} | ||
/** | ||
@@ -83,3 +195,18 @@ * This is an internal function optimized for performance. | ||
getValue(ctx) { | ||
return Promise.reject(new Error(`No value was configured for binding ${this._key}.`)); | ||
// First check cached value for non-transient | ||
if (this._cache !== undefined) { | ||
if (this.scope === BindingScope.SINGLETON) { | ||
return this._cache; | ||
} | ||
else if (this.scope === BindingScope.CONTEXT) { | ||
if (ctx.contains(this.key)) { | ||
return this._cache; | ||
} | ||
} | ||
} | ||
if (this._getValue) { | ||
const result = this._getValue(ctx); | ||
return this._cacheValue(ctx, result); | ||
} | ||
return Promise.reject(new Error(`No value was configured for binding ${this.key}.`)); | ||
} | ||
@@ -92,7 +219,7 @@ lock() { | ||
if (typeof tagName === 'string') { | ||
this._tags.add(tagName); | ||
this.tags.add(tagName); | ||
} | ||
else { | ||
tagName.forEach(t => { | ||
this._tags.add(t); | ||
this.tags.add(t); | ||
}); | ||
@@ -102,2 +229,5 @@ } | ||
} | ||
inScope(scope) { | ||
this.scope = scope; | ||
} | ||
/** | ||
@@ -115,3 +245,3 @@ * Bind the key to a constant value. | ||
to(value) { | ||
this.getValue = () => value; | ||
this._getValue = () => value; | ||
return this; | ||
@@ -139,3 +269,3 @@ } | ||
// TODO(bajtos) allow factoryFn with @inject arguments | ||
this.getValue = ctx => factoryFn(); | ||
this._getValue = ctx => factoryFn(); | ||
return this; | ||
@@ -147,3 +277,3 @@ } | ||
toProvider(providerClass) { | ||
this.getValue = ctx => { | ||
this._getValue = ctx => { | ||
const providerOrPromise = resolver_1.instantiateClass(providerClass, ctx); | ||
@@ -167,3 +297,3 @@ if (is_promise_1.isPromise(providerOrPromise)) { | ||
toClass(ctor) { | ||
this.getValue = context => resolver_1.instantiateClass(ctor, context); | ||
this._getValue = ctx => resolver_1.instantiateClass(ctor, ctx); | ||
this.valueConstructor = ctor; | ||
@@ -170,0 +300,0 @@ return this; |
@@ -5,3 +5,3 @@ import { Binding, BoundValue } from './binding'; | ||
private registry; | ||
constructor(_parent?: Context); | ||
constructor(_parent?: Context | undefined); | ||
bind(key: string): Binding; | ||
@@ -8,0 +8,0 @@ contains(key: string): boolean; |
@@ -1,2 +0,2 @@ | ||
export { Binding, BoundValue } from './binding'; | ||
export { Binding, BindingScope, BoundValue, ValueOrPromise } from './binding'; | ||
export { Context } from './context'; | ||
@@ -3,0 +3,0 @@ export { Constructor } from './resolver'; |
@@ -9,2 +9,3 @@ "use strict"; | ||
exports.Binding = binding_1.Binding; | ||
exports.BindingScope = binding_1.BindingScope; | ||
var context_1 = require("./context"); | ||
@@ -11,0 +12,0 @@ exports.Context = context_1.Context; |
@@ -1,1 +0,7 @@ | ||
export declare function isPromise<T>(value: T | Promise<T>): value is Promise<T>; | ||
/** | ||
* Check whether a value is a Promise-like instance. | ||
* Recognizes both native promises and third-party promise libraries. | ||
* | ||
* @param value The value to check. | ||
*/ | ||
export declare function isPromise<T>(value: T | PromiseLike<T>): value is PromiseLike<T>; |
@@ -7,2 +7,8 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Check whether a value is a Promise-like instance. | ||
* Recognizes both native promises and third-party promise libraries. | ||
* | ||
* @param value The value to check. | ||
*/ | ||
function isPromise(value) { | ||
@@ -9,0 +15,0 @@ if (!value) |
@@ -7,3 +7,3 @@ import 'reflect-metadata'; | ||
*/ | ||
constructor(namespace?: string); | ||
constructor(namespace?: string | undefined); | ||
private getMetadataKey(metadataKey); | ||
@@ -33,3 +33,3 @@ /** | ||
getOwnMetadataKeys(target: Object, propertyKey?: string | symbol): string[]; | ||
decorate(decorators: (PropertyDecorator | MethodDecorator)[] | ClassDecorator[], target: Object, targetKey?: string | symbol, descriptor?: PropertyDescriptor): PropertyDescriptor; | ||
decorate(decorators: (PropertyDecorator | MethodDecorator)[] | ClassDecorator[], target: Object, targetKey?: string | symbol, descriptor?: PropertyDescriptor): PropertyDescriptor | Function; | ||
metadata(metadataKey: string, metadataValue: any): { | ||
@@ -36,0 +36,0 @@ (target: Function): void; |
import { Context } from './context'; | ||
import { BoundValue } from './binding'; | ||
/** | ||
* A class constructor accepting arbitrary arguments. | ||
*/ | ||
export declare type Constructor<T> = new (...args: any[]) => T; | ||
@@ -4,0 +7,0 @@ /** |
{ | ||
"name": "@loopback/context", | ||
"version": "4.0.0-alpha.9", | ||
"version": "4.0.0-alpha.10", | ||
"description": "LoopBack's container for Inversion of Control", | ||
@@ -8,7 +8,9 @@ "scripts": { | ||
"build": "npm run build:lib && npm run build:lib6", | ||
"build:current": "node ../../bin/compile-package", | ||
"build:lib": "node ../../bin/compile-package es2017", | ||
"build:lib6": "node ../../bin/compile-package es2015", | ||
"build:apidocs": "node ../../bin/generate-apidocs", | ||
"clean": "rm -rf loopback-context*.tgz lib* package", | ||
"prepublish": "npm run build", | ||
"pretest": "npm run build", | ||
"prepublish": "npm run build && npm run build:apidocs", | ||
"pretest": "npm run build:current", | ||
"test": "mocha --opts ../../test/mocha.opts 'test/unit/**/*.ts' 'test/acceptance/**/*.ts'", | ||
@@ -25,6 +27,4 @@ "unit": "mocha --opts ../../test/mocha.opts 'test/unit/**/*.ts'", | ||
"@loopback/testlab": "^4.0.0-alpha.5", | ||
"@types/bluebird": "^3.5.2", | ||
"bluebird": "^3.5.0", | ||
"mocha": "^3.2.0", | ||
"typescript": "^2.3.2" | ||
"@types/bluebird": "^3.5.8", | ||
"bluebird": "^3.5.0" | ||
}, | ||
@@ -45,3 +45,4 @@ "keywords": [ | ||
"lib", | ||
"lib6" | ||
"lib6", | ||
"api-docs" | ||
], | ||
@@ -48,0 +49,0 @@ "repository": { |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
638204
3
106
5895
1