-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for mixed sync/async bindings (#193)
* Add support for mixed sync/async bindings Allow dynamic bindings to be created using an async function returning a promise. Modify `ctx.get` to always return a promise, regardless of whether the binding is sync or async. Modify dependency injection code to detect sync vs. async dependencies and optimise the code building the dependencies to prefer sync flow if allowed by dependencies.
- Loading branch information
Showing
22 changed files
with
375 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright IBM Corp. 2013,2017. All Rights Reserved. | ||
// Node module: loopback | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
export function isPromise<T>(value: T | Promise<T>): value is Promise<T> { | ||
if (!value) | ||
return false; | ||
if (typeof value !== 'object' && typeof value !== 'function') | ||
return false; | ||
return typeof (value as Promise<T>).then === 'function'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright IBM Corp. 2013,2017. All Rights Reserved. | ||
// Node module: loopback | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import { Context } from './context'; | ||
import { Binding, BoundValue } from './binding'; | ||
import { isPromise } from './isPromise'; | ||
import { describeInjectedArguments } from './inject'; | ||
|
||
// tslint:disable-next-line:no-any | ||
export type Constructor<T> = new(...args: any[]) => T; | ||
|
||
/** | ||
* Create an instance of a class which constructor has arguments | ||
* decorated with `@inject`. | ||
* | ||
* The function returns a class when all dependencies were | ||
* resolved synchronously, or a Promise otherwise. | ||
* | ||
* @param ctor The class constructor to call. | ||
* @param ctx The context containing values for `@inject` resolution | ||
*/ | ||
export function instantiateClass<T>(ctor: Constructor<T>, ctx: Context): T | Promise<T> { | ||
const argsOrPromise = resolveInjectedArguments(ctor, ctx); | ||
if (isPromise(argsOrPromise)) { | ||
return argsOrPromise.then(args => new ctor(...args)); | ||
} else { | ||
return new ctor(...argsOrPromise); | ||
} | ||
} | ||
|
||
/** | ||
* Given a function with arguments decorated with `@inject`, | ||
* return the list of arguments resolved using the values | ||
* bound in `ctx`. | ||
* The function returns an argument array when all dependencies were | ||
* resolved synchronously, or a Promise otherwise. | ||
* | ||
* @param fn The function for which the arguments should be resolved. | ||
* @param ctx The context containing values for `@inject` resolution | ||
*/ | ||
export function resolveInjectedArguments(fn: Function, ctx: Context): BoundValue[] | Promise<BoundValue[]> { | ||
// NOTE: the array may be sparse, i.e. | ||
// Object.keys(injectedArgs).length !== injectedArgs.length | ||
// Example value: | ||
// [ , 'key1', , 'key2'] | ||
const injectedArgs = describeInjectedArguments(fn); | ||
|
||
const args: BoundValue[] = new Array(fn.length); | ||
let asyncResolvers: Promise<void>[] | undefined = undefined; | ||
|
||
for (let ix = 0; ix < fn.length; ix++) { | ||
const bindingKey = injectedArgs[ix]; | ||
if (!bindingKey) { | ||
throw new Error( | ||
`Cannot resolve injected arguments for function ${fn.name}: ` + | ||
`The argument ${ix + 1} was not decorated for dependency injection.`); | ||
} | ||
|
||
const binding = ctx.getBinding(bindingKey); | ||
const valueOrPromise = binding.getValue(); | ||
if (isPromise(valueOrPromise)) { | ||
if (!asyncResolvers) asyncResolvers = []; | ||
asyncResolvers.push(valueOrPromise.then((v: BoundValue) => args[ix] = v)); | ||
} else { | ||
args[ix] = valueOrPromise as BoundValue; | ||
} | ||
} | ||
|
||
if (asyncResolvers) { | ||
return Promise.all(asyncResolvers).then(() => args); | ||
} else { | ||
return args; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.