@loopback/context
Advanced tools
Comparing version 0.10.1 to 0.11.0
@@ -6,2 +6,13 @@ # Change Log | ||
<a name="0.11.0"></a> | ||
# [0.11.0](https://github.com/strongloop/loopback-next/compare/@loopback/context@0.10.1...@loopback/context@0.11.0) (2018-05-14) | ||
### Features | ||
* **context:** add more utils to resolve valueOrPromises ([cc55ef5](https://github.com/strongloop/loopback-next/commit/cc55ef5)) | ||
<a name="0.10.1"></a> | ||
@@ -8,0 +19,0 @@ ## [0.10.1](https://github.com/strongloop/loopback-next/compare/@loopback/context@0.10.0...@loopback/context@0.10.1) (2018-05-08) |
@@ -146,3 +146,3 @@ import { Context } from './context'; | ||
* const result = binding.getValue(ctx); | ||
* if (isPromise(result)) { | ||
* if (isPromiseLike(result)) { | ||
* result.then(doSomething) | ||
@@ -149,0 +149,0 @@ * } else { |
@@ -132,29 +132,13 @@ "use strict"; | ||
this._cache = new WeakMap(); | ||
if (value_promise_1.isPromiseLike(result)) { | ||
return value_promise_1.transformValueOrPromise(result, val => { | ||
if (this.scope === BindingScope.SINGLETON) { | ||
// Cache the value at owning context level | ||
result = result.then(val => { | ||
this._cache.set(ctx.getOwnerContext(this.key), val); | ||
return val; | ||
}); | ||
} | ||
else if (this.scope === BindingScope.CONTEXT) { | ||
// Cache the value at the current context | ||
result = result.then(val => { | ||
this._cache.set(ctx, val); | ||
return val; | ||
}); | ||
} | ||
} | ||
else { | ||
if (this.scope === BindingScope.SINGLETON) { | ||
// Cache the value | ||
this._cache.set(ctx.getOwnerContext(this.key), result); | ||
this._cache.set(ctx.getOwnerContext(this.key), val); | ||
} | ||
else if (this.scope === BindingScope.CONTEXT) { | ||
// Cache the value at the current context | ||
this._cache.set(ctx, result); | ||
this._cache.set(ctx, val); | ||
} | ||
} | ||
return result; | ||
return val; | ||
}); | ||
} | ||
@@ -175,3 +159,3 @@ /** | ||
* const result = binding.getValue(ctx); | ||
* if (isPromise(result)) { | ||
* if (isPromiseLike(result)) { | ||
* result.then(doSomething) | ||
@@ -356,8 +340,3 @@ * } else { | ||
const providerOrPromise = resolver_1.instantiateClass(providerClass, ctx, session); | ||
if (value_promise_1.isPromiseLike(providerOrPromise)) { | ||
return providerOrPromise.then(p => p.value()); | ||
} | ||
else { | ||
return providerOrPromise.value(); | ||
} | ||
return value_promise_1.transformValueOrPromise(providerOrPromise, p => p.value()); | ||
}; | ||
@@ -364,0 +343,0 @@ return this; |
export * from '@loopback/metadata'; | ||
export { isPromiseLike, BoundValue, Constructor, ValueOrPromise, MapObject, resolveList, resolveMap, tryWithFinally, getDeepProperty } from './value-promise'; | ||
export { isPromiseLike, BoundValue, Constructor, ValueOrPromise, MapObject, resolveList, resolveMap, resolveUntil, transformValueOrPromise, tryWithFinally, getDeepProperty } from './value-promise'; | ||
export { Binding, BindingScope, BindingType, TagMap } from './binding'; | ||
@@ -4,0 +4,0 @@ export { Context } from './context'; |
@@ -15,2 +15,4 @@ "use strict"; | ||
exports.resolveMap = value_promise_1.resolveMap; | ||
exports.resolveUntil = value_promise_1.resolveUntil; | ||
exports.transformValueOrPromise = value_promise_1.transformValueOrPromise; | ||
exports.tryWithFinally = value_promise_1.tryWithFinally; | ||
@@ -17,0 +19,0 @@ exports.getDeepProperty = value_promise_1.getDeepProperty; |
@@ -39,51 +39,16 @@ "use strict"; | ||
const propertiesOrPromise = resolveInjectedProperties(ctor, ctx, session); | ||
let inst; | ||
if (value_promise_1.isPromiseLike(argsOrPromise)) { | ||
// Instantiate the class asynchronously | ||
inst = argsOrPromise.then(args => { | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('Injected arguments for %s():', ctor.name, args); | ||
} | ||
return new ctor(...args); | ||
}); | ||
} | ||
else { | ||
const inst = value_promise_1.transformValueOrPromise(argsOrPromise, args => { | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('Injected arguments for %s():', ctor.name, argsOrPromise); | ||
debug('Injected arguments for %s():', ctor.name, args); | ||
} | ||
// Instantiate the class synchronously | ||
inst = new ctor(...argsOrPromise); | ||
} | ||
if (value_promise_1.isPromiseLike(propertiesOrPromise)) { | ||
return propertiesOrPromise.then(props => { | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('Injected properties for %s:', ctor.name, props); | ||
} | ||
if (value_promise_1.isPromiseLike(inst)) { | ||
// Inject the properties asynchronously | ||
return inst.then(obj => Object.assign(obj, props)); | ||
} | ||
else { | ||
// Inject the properties synchronously | ||
return Object.assign(inst, props); | ||
} | ||
}); | ||
} | ||
else { | ||
if (value_promise_1.isPromiseLike(inst)) { | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('Injected properties for %s:', ctor.name, propertiesOrPromise); | ||
} | ||
// Inject the properties asynchronously | ||
return inst.then(obj => Object.assign(obj, propertiesOrPromise)); | ||
return new ctor(...args); | ||
}); | ||
return value_promise_1.transformValueOrPromise(propertiesOrPromise, props => { | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('Injected properties for %s:', ctor.name, props); | ||
} | ||
else { | ||
// Inject the properties synchronously | ||
return Object.assign(inst, propertiesOrPromise); | ||
} | ||
} | ||
return value_promise_1.transformValueOrPromise(inst, obj => Object.assign(obj, props)); | ||
}); | ||
} | ||
@@ -206,20 +171,9 @@ exports.instantiateClass = instantiateClass; | ||
assert(typeof targetWithMethods[method] === 'function', `Method ${method} not found`); | ||
if (value_promise_1.isPromiseLike(argsOrPromise)) { | ||
// Invoke the target method asynchronously | ||
return argsOrPromise.then(args => { | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('Injected arguments for %s:', methodName, args); | ||
} | ||
return targetWithMethods[method](...args); | ||
}); | ||
} | ||
else { | ||
return value_promise_1.transformValueOrPromise(argsOrPromise, args => { | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('Injected arguments for %s:', methodName, argsOrPromise); | ||
debug('Injected arguments for %s:', methodName, args); | ||
} | ||
// Invoke the target method synchronously | ||
return targetWithMethods[method](...argsOrPromise); | ||
} | ||
return targetWithMethods[method](...args); | ||
}); | ||
} | ||
@@ -226,0 +180,0 @@ exports.invokeMethod = invokeMethod; |
@@ -97,1 +97,16 @@ /** | ||
export declare function tryWithFinally<T>(action: () => ValueOrPromise<T>, finalAction: () => void): ValueOrPromise<T>; | ||
/** | ||
* Resolve an iterator of source values into a result until the evaluator | ||
* returns `true` | ||
* @param source The iterator of source values | ||
* @param resolver The resolve function that maps the source value to a result | ||
* @param evaluator The evaluate function that decides when to stop | ||
*/ | ||
export declare function resolveUntil<T, V>(source: Iterator<T>, resolver: (sourceVal: T) => ValueOrPromise<V | undefined>, evaluator: (sourceVal: T, targetVal: V | undefined) => boolean): ValueOrPromise<V | undefined>; | ||
/** | ||
* Transform a value or promise with a function that produces a new value or | ||
* promise | ||
* @param valueOrPromise The value or promise | ||
* @param transformer A function that maps the source value to a value or promise | ||
*/ | ||
export declare function transformValueOrPromise<T, V>(valueOrPromise: ValueOrPromise<T>, transformer: (val: T) => ValueOrPromise<V>): ValueOrPromise<V>; |
@@ -6,2 +6,6 @@ "use strict"; | ||
// License text available at https://opensource.org/licenses/MIT | ||
/** | ||
* This module contains types for values and/or promises as well as a set of | ||
* utility methods to handle values and/or promises. | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -183,2 +187,51 @@ /** | ||
exports.tryWithFinally = tryWithFinally; | ||
/** | ||
* Resolve an iterator of source values into a result until the evaluator | ||
* returns `true` | ||
* @param source The iterator of source values | ||
* @param resolver The resolve function that maps the source value to a result | ||
* @param evaluator The evaluate function that decides when to stop | ||
*/ | ||
function resolveUntil(source, resolver, evaluator) { | ||
// Do iteration in loop for synchronous values to avoid stack overflow | ||
while (true) { | ||
const next = source.next(); | ||
if (next.done) | ||
return undefined; // End of the iterator | ||
const sourceVal = next.value; | ||
const valueOrPromise = resolver(sourceVal); | ||
if (isPromiseLike(valueOrPromise)) { | ||
return valueOrPromise.then(v => { | ||
if (evaluator(sourceVal, v)) { | ||
return v; | ||
} | ||
else { | ||
return resolveUntil(source, resolver, evaluator); | ||
} | ||
}); | ||
} | ||
else { | ||
if (evaluator(sourceVal, valueOrPromise)) { | ||
return valueOrPromise; | ||
} | ||
// Continue with the while loop | ||
} | ||
} | ||
} | ||
exports.resolveUntil = resolveUntil; | ||
/** | ||
* Transform a value or promise with a function that produces a new value or | ||
* promise | ||
* @param valueOrPromise The value or promise | ||
* @param transformer A function that maps the source value to a value or promise | ||
*/ | ||
function transformValueOrPromise(valueOrPromise, transformer) { | ||
if (isPromiseLike(valueOrPromise)) { | ||
return valueOrPromise.then(transformer); | ||
} | ||
else { | ||
return transformer(valueOrPromise); | ||
} | ||
} | ||
exports.transformValueOrPromise = transformValueOrPromise; | ||
//# sourceMappingURL=value-promise.js.map |
@@ -146,3 +146,3 @@ import { Context } from './context'; | ||
* const result = binding.getValue(ctx); | ||
* if (isPromise(result)) { | ||
* if (isPromiseLike(result)) { | ||
* result.then(doSomething) | ||
@@ -149,0 +149,0 @@ * } else { |
@@ -132,29 +132,13 @@ "use strict"; | ||
this._cache = new WeakMap(); | ||
if (value_promise_1.isPromiseLike(result)) { | ||
return value_promise_1.transformValueOrPromise(result, val => { | ||
if (this.scope === BindingScope.SINGLETON) { | ||
// Cache the value at owning context level | ||
result = result.then(val => { | ||
this._cache.set(ctx.getOwnerContext(this.key), val); | ||
return val; | ||
}); | ||
} | ||
else if (this.scope === BindingScope.CONTEXT) { | ||
// Cache the value at the current context | ||
result = result.then(val => { | ||
this._cache.set(ctx, val); | ||
return val; | ||
}); | ||
} | ||
} | ||
else { | ||
if (this.scope === BindingScope.SINGLETON) { | ||
// Cache the value | ||
this._cache.set(ctx.getOwnerContext(this.key), result); | ||
this._cache.set(ctx.getOwnerContext(this.key), val); | ||
} | ||
else if (this.scope === BindingScope.CONTEXT) { | ||
// Cache the value at the current context | ||
this._cache.set(ctx, result); | ||
this._cache.set(ctx, val); | ||
} | ||
} | ||
return result; | ||
return val; | ||
}); | ||
} | ||
@@ -175,3 +159,3 @@ /** | ||
* const result = binding.getValue(ctx); | ||
* if (isPromise(result)) { | ||
* if (isPromiseLike(result)) { | ||
* result.then(doSomething) | ||
@@ -356,8 +340,3 @@ * } else { | ||
const providerOrPromise = resolver_1.instantiateClass(providerClass, ctx, session); | ||
if (value_promise_1.isPromiseLike(providerOrPromise)) { | ||
return providerOrPromise.then(p => p.value()); | ||
} | ||
else { | ||
return providerOrPromise.value(); | ||
} | ||
return value_promise_1.transformValueOrPromise(providerOrPromise, p => p.value()); | ||
}; | ||
@@ -364,0 +343,0 @@ return this; |
export * from '@loopback/metadata'; | ||
export { isPromiseLike, BoundValue, Constructor, ValueOrPromise, MapObject, resolveList, resolveMap, tryWithFinally, getDeepProperty } from './value-promise'; | ||
export { isPromiseLike, BoundValue, Constructor, ValueOrPromise, MapObject, resolveList, resolveMap, resolveUntil, transformValueOrPromise, tryWithFinally, getDeepProperty } from './value-promise'; | ||
export { Binding, BindingScope, BindingType, TagMap } from './binding'; | ||
@@ -4,0 +4,0 @@ export { Context } from './context'; |
@@ -15,2 +15,4 @@ "use strict"; | ||
exports.resolveMap = value_promise_1.resolveMap; | ||
exports.resolveUntil = value_promise_1.resolveUntil; | ||
exports.transformValueOrPromise = value_promise_1.transformValueOrPromise; | ||
exports.tryWithFinally = value_promise_1.tryWithFinally; | ||
@@ -17,0 +19,0 @@ exports.getDeepProperty = value_promise_1.getDeepProperty; |
@@ -39,51 +39,16 @@ "use strict"; | ||
const propertiesOrPromise = resolveInjectedProperties(ctor, ctx, session); | ||
let inst; | ||
if (value_promise_1.isPromiseLike(argsOrPromise)) { | ||
// Instantiate the class asynchronously | ||
inst = argsOrPromise.then(args => { | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('Injected arguments for %s():', ctor.name, args); | ||
} | ||
return new ctor(...args); | ||
}); | ||
} | ||
else { | ||
const inst = value_promise_1.transformValueOrPromise(argsOrPromise, args => { | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('Injected arguments for %s():', ctor.name, argsOrPromise); | ||
debug('Injected arguments for %s():', ctor.name, args); | ||
} | ||
// Instantiate the class synchronously | ||
inst = new ctor(...argsOrPromise); | ||
} | ||
if (value_promise_1.isPromiseLike(propertiesOrPromise)) { | ||
return propertiesOrPromise.then(props => { | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('Injected properties for %s:', ctor.name, props); | ||
} | ||
if (value_promise_1.isPromiseLike(inst)) { | ||
// Inject the properties asynchronously | ||
return inst.then(obj => Object.assign(obj, props)); | ||
} | ||
else { | ||
// Inject the properties synchronously | ||
return Object.assign(inst, props); | ||
} | ||
}); | ||
} | ||
else { | ||
if (value_promise_1.isPromiseLike(inst)) { | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('Injected properties for %s:', ctor.name, propertiesOrPromise); | ||
} | ||
// Inject the properties asynchronously | ||
return inst.then(obj => Object.assign(obj, propertiesOrPromise)); | ||
return new ctor(...args); | ||
}); | ||
return value_promise_1.transformValueOrPromise(propertiesOrPromise, props => { | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('Injected properties for %s:', ctor.name, props); | ||
} | ||
else { | ||
// Inject the properties synchronously | ||
return Object.assign(inst, propertiesOrPromise); | ||
} | ||
} | ||
return value_promise_1.transformValueOrPromise(inst, obj => Object.assign(obj, props)); | ||
}); | ||
} | ||
@@ -206,20 +171,9 @@ exports.instantiateClass = instantiateClass; | ||
assert(typeof targetWithMethods[method] === 'function', `Method ${method} not found`); | ||
if (value_promise_1.isPromiseLike(argsOrPromise)) { | ||
// Invoke the target method asynchronously | ||
return argsOrPromise.then(args => { | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('Injected arguments for %s:', methodName, args); | ||
} | ||
return targetWithMethods[method](...args); | ||
}); | ||
} | ||
else { | ||
return value_promise_1.transformValueOrPromise(argsOrPromise, args => { | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('Injected arguments for %s:', methodName, argsOrPromise); | ||
debug('Injected arguments for %s:', methodName, args); | ||
} | ||
// Invoke the target method synchronously | ||
return targetWithMethods[method](...argsOrPromise); | ||
} | ||
return targetWithMethods[method](...args); | ||
}); | ||
} | ||
@@ -226,0 +180,0 @@ exports.invokeMethod = invokeMethod; |
@@ -97,1 +97,16 @@ /** | ||
export declare function tryWithFinally<T>(action: () => ValueOrPromise<T>, finalAction: () => void): ValueOrPromise<T>; | ||
/** | ||
* Resolve an iterator of source values into a result until the evaluator | ||
* returns `true` | ||
* @param source The iterator of source values | ||
* @param resolver The resolve function that maps the source value to a result | ||
* @param evaluator The evaluate function that decides when to stop | ||
*/ | ||
export declare function resolveUntil<T, V>(source: Iterator<T>, resolver: (sourceVal: T) => ValueOrPromise<V | undefined>, evaluator: (sourceVal: T, targetVal: V | undefined) => boolean): ValueOrPromise<V | undefined>; | ||
/** | ||
* Transform a value or promise with a function that produces a new value or | ||
* promise | ||
* @param valueOrPromise The value or promise | ||
* @param transformer A function that maps the source value to a value or promise | ||
*/ | ||
export declare function transformValueOrPromise<T, V>(valueOrPromise: ValueOrPromise<T>, transformer: (val: T) => ValueOrPromise<V>): ValueOrPromise<V>; |
@@ -6,2 +6,6 @@ "use strict"; | ||
// License text available at https://opensource.org/licenses/MIT | ||
/** | ||
* This module contains types for values and/or promises as well as a set of | ||
* utility methods to handle values and/or promises. | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -183,2 +187,51 @@ /** | ||
exports.tryWithFinally = tryWithFinally; | ||
/** | ||
* Resolve an iterator of source values into a result until the evaluator | ||
* returns `true` | ||
* @param source The iterator of source values | ||
* @param resolver The resolve function that maps the source value to a result | ||
* @param evaluator The evaluate function that decides when to stop | ||
*/ | ||
function resolveUntil(source, resolver, evaluator) { | ||
// Do iteration in loop for synchronous values to avoid stack overflow | ||
while (true) { | ||
const next = source.next(); | ||
if (next.done) | ||
return undefined; // End of the iterator | ||
const sourceVal = next.value; | ||
const valueOrPromise = resolver(sourceVal); | ||
if (isPromiseLike(valueOrPromise)) { | ||
return valueOrPromise.then(v => { | ||
if (evaluator(sourceVal, v)) { | ||
return v; | ||
} | ||
else { | ||
return resolveUntil(source, resolver, evaluator); | ||
} | ||
}); | ||
} | ||
else { | ||
if (evaluator(sourceVal, valueOrPromise)) { | ||
return valueOrPromise; | ||
} | ||
// Continue with the while loop | ||
} | ||
} | ||
} | ||
exports.resolveUntil = resolveUntil; | ||
/** | ||
* Transform a value or promise with a function that produces a new value or | ||
* promise | ||
* @param valueOrPromise The value or promise | ||
* @param transformer A function that maps the source value to a value or promise | ||
*/ | ||
function transformValueOrPromise(valueOrPromise, transformer) { | ||
if (isPromiseLike(valueOrPromise)) { | ||
return valueOrPromise.then(transformer); | ||
} | ||
else { | ||
return transformer(valueOrPromise); | ||
} | ||
} | ||
exports.transformValueOrPromise = transformValueOrPromise; | ||
//# sourceMappingURL=value-promise.js.map |
{ | ||
"name": "@loopback/context", | ||
"version": "0.10.1", | ||
"version": "0.11.0", | ||
"description": "LoopBack's container for Inversion of Control", | ||
@@ -26,3 +26,3 @@ "engines": { | ||
"@loopback/dist-util": "^0.3.1", | ||
"@loopback/metadata": "^0.8.1", | ||
"@loopback/metadata": "^0.8.2", | ||
"debug": "^3.1.0", | ||
@@ -32,4 +32,4 @@ "uuid": "^3.2.1" | ||
"devDependencies": { | ||
"@loopback/build": "^0.6.3", | ||
"@loopback/testlab": "^0.10.1", | ||
"@loopback/build": "^0.6.4", | ||
"@loopback/testlab": "^0.10.2", | ||
"@types/bluebird": "^3.5.20", | ||
@@ -36,0 +36,0 @@ "@types/debug": "^0.0.30", |
@@ -16,2 +16,3 @@ // Copyright IBM Corp. 2017,2018. All Rights Reserved. | ||
MapObject, | ||
transformValueOrPromise, | ||
} from './value-promise'; | ||
@@ -174,26 +175,12 @@ import {Provider} from './provider'; | ||
if (!this._cache) this._cache = new WeakMap<Context, T>(); | ||
if (isPromiseLike(result)) { | ||
return transformValueOrPromise(result, val => { | ||
if (this.scope === BindingScope.SINGLETON) { | ||
// Cache the value at owning context level | ||
result = result.then(val => { | ||
this._cache.set(ctx.getOwnerContext(this.key)!, val); | ||
return val; | ||
}); | ||
} else if (this.scope === BindingScope.CONTEXT) { | ||
// Cache the value at the current context | ||
result = result.then(val => { | ||
this._cache.set(ctx, val); | ||
return val; | ||
}); | ||
} | ||
} else { | ||
if (this.scope === BindingScope.SINGLETON) { | ||
// Cache the value | ||
this._cache.set(ctx.getOwnerContext(this.key)!, result); | ||
this._cache.set(ctx.getOwnerContext(this.key)!, val); | ||
} else if (this.scope === BindingScope.CONTEXT) { | ||
// Cache the value at the current context | ||
this._cache.set(ctx, result); | ||
this._cache.set(ctx, val); | ||
} | ||
} | ||
return result; | ||
return val; | ||
}); | ||
} | ||
@@ -215,3 +202,3 @@ | ||
* const result = binding.getValue(ctx); | ||
* if (isPromise(result)) { | ||
* if (isPromiseLike(result)) { | ||
* result.then(doSomething) | ||
@@ -414,7 +401,3 @@ * } else { | ||
); | ||
if (isPromiseLike(providerOrPromise)) { | ||
return providerOrPromise.then(p => p.value()); | ||
} else { | ||
return providerOrPromise.value(); | ||
} | ||
return transformValueOrPromise(providerOrPromise, p => p.value()); | ||
}; | ||
@@ -421,0 +404,0 @@ return this; |
@@ -16,2 +16,4 @@ // Copyright IBM Corp. 2017,2018. All Rights Reserved. | ||
resolveMap, | ||
resolveUntil, | ||
transformValueOrPromise, | ||
tryWithFinally, | ||
@@ -18,0 +20,0 @@ getDeepProperty, |
@@ -13,5 +13,5 @@ // Copyright IBM Corp. 2017,2018. All Rights Reserved. | ||
MapObject, | ||
isPromiseLike, | ||
resolveList, | ||
resolveMap, | ||
transformValueOrPromise, | ||
} from './value-promise'; | ||
@@ -60,6 +60,5 @@ | ||
const propertiesOrPromise = resolveInjectedProperties(ctor, ctx, session); | ||
let inst: ValueOrPromise<T>; | ||
if (isPromiseLike(argsOrPromise)) { | ||
// Instantiate the class asynchronously | ||
inst = argsOrPromise.then(args => { | ||
const inst: ValueOrPromise<T> = transformValueOrPromise( | ||
argsOrPromise, | ||
args => { | ||
/* istanbul ignore if */ | ||
@@ -70,38 +69,11 @@ if (debug.enabled) { | ||
return new ctor(...args); | ||
}); | ||
} else { | ||
}, | ||
); | ||
return transformValueOrPromise(propertiesOrPromise, props => { | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('Injected arguments for %s():', ctor.name, argsOrPromise); | ||
debug('Injected properties for %s:', ctor.name, props); | ||
} | ||
// Instantiate the class synchronously | ||
inst = new ctor(...argsOrPromise); | ||
} | ||
if (isPromiseLike(propertiesOrPromise)) { | ||
return propertiesOrPromise.then(props => { | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('Injected properties for %s:', ctor.name, props); | ||
} | ||
if (isPromiseLike(inst)) { | ||
// Inject the properties asynchronously | ||
return inst.then(obj => Object.assign(obj, props)); | ||
} else { | ||
// Inject the properties synchronously | ||
return Object.assign(inst, props); | ||
} | ||
}); | ||
} else { | ||
if (isPromiseLike(inst)) { | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('Injected properties for %s:', ctor.name, propertiesOrPromise); | ||
} | ||
// Inject the properties asynchronously | ||
return inst.then(obj => Object.assign(obj, propertiesOrPromise)); | ||
} else { | ||
// Inject the properties synchronously | ||
return Object.assign(inst, propertiesOrPromise); | ||
} | ||
} | ||
return transformValueOrPromise(inst, obj => Object.assign(obj, props)); | ||
}); | ||
} | ||
@@ -263,19 +235,9 @@ | ||
); | ||
if (isPromiseLike(argsOrPromise)) { | ||
// Invoke the target method asynchronously | ||
return argsOrPromise.then(args => { | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('Injected arguments for %s:', methodName, args); | ||
} | ||
return targetWithMethods[method](...args); | ||
}); | ||
} else { | ||
return transformValueOrPromise(argsOrPromise, args => { | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('Injected arguments for %s:', methodName, argsOrPromise); | ||
debug('Injected arguments for %s:', methodName, args); | ||
} | ||
// Invoke the target method synchronously | ||
return targetWithMethods[method](...argsOrPromise); | ||
} | ||
return targetWithMethods[method](...args); | ||
}); | ||
} | ||
@@ -282,0 +244,0 @@ |
@@ -5,3 +5,2 @@ // Copyright IBM Corp. 2017,2018. All Rights Reserved. | ||
// License text available at https://opensource.org/licenses/MIT | ||
/** | ||
@@ -222,1 +221,53 @@ * This module contains types for values and/or promises as well as a set of | ||
} | ||
/** | ||
* Resolve an iterator of source values into a result until the evaluator | ||
* returns `true` | ||
* @param source The iterator of source values | ||
* @param resolver The resolve function that maps the source value to a result | ||
* @param evaluator The evaluate function that decides when to stop | ||
*/ | ||
export function resolveUntil<T, V>( | ||
source: Iterator<T>, | ||
resolver: (sourceVal: T) => ValueOrPromise<V | undefined>, | ||
evaluator: (sourceVal: T, targetVal: V | undefined) => boolean, | ||
): ValueOrPromise<V | undefined> { | ||
// Do iteration in loop for synchronous values to avoid stack overflow | ||
while (true) { | ||
const next = source.next(); | ||
if (next.done) return undefined; // End of the iterator | ||
const sourceVal = next.value; | ||
const valueOrPromise = resolver(sourceVal); | ||
if (isPromiseLike(valueOrPromise)) { | ||
return valueOrPromise.then(v => { | ||
if (evaluator(sourceVal, v)) { | ||
return v; | ||
} else { | ||
return resolveUntil(source, resolver, evaluator); | ||
} | ||
}); | ||
} else { | ||
if (evaluator(sourceVal, valueOrPromise)) { | ||
return valueOrPromise; | ||
} | ||
// Continue with the while loop | ||
} | ||
} | ||
} | ||
/** | ||
* Transform a value or promise with a function that produces a new value or | ||
* promise | ||
* @param valueOrPromise The value or promise | ||
* @param transformer A function that maps the source value to a value or promise | ||
*/ | ||
export function transformValueOrPromise<T, V>( | ||
valueOrPromise: ValueOrPromise<T>, | ||
transformer: (val: T) => ValueOrPromise<V>, | ||
): ValueOrPromise<V> { | ||
if (isPromiseLike(valueOrPromise)) { | ||
return valueOrPromise.then(transformer); | ||
} else { | ||
return transformer(valueOrPromise); | ||
} | ||
} |
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
360357
75
7586
Updated@loopback/metadata@^0.8.2