-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for mixed sync/async bindings #193
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 createClassInstance<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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name can be
instantiate
.