-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #169 from inversify/refactor/update-resolution-flo…
…w-with-binding-activations Update resolution flow with binding activations
- Loading branch information
Showing
21 changed files
with
310 additions
and
55 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
4 changes: 3 additions & 1 deletion
4
packages/container/libraries/core/src/binding/models/BindingActivation.ts
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
export type BindingActivation<T = unknown> = (injectable: T) => T | Promise<T>; | ||
import { Resolved } from '../../resolution/models/Resolved'; | ||
|
||
export type BindingActivation<T = unknown> = (injectable: T) => Resolved<T>; |
14 changes: 9 additions & 5 deletions
14
packages/container/libraries/core/src/binding/models/ConstantValueBinding.ts
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 |
---|---|---|
@@ -1,9 +1,13 @@ | ||
import { Resolved } from '../../resolution/models/Resolved'; | ||
import { bindingScopeValues } from './BindingScope'; | ||
import { bindingTypeValues } from './BindingType'; | ||
import { ScopedBinding } from './ScopedBinding'; | ||
|
||
export type ConstantValueBinding<TActivated> = ScopedBinding< | ||
typeof bindingTypeValues.ConstantValue, | ||
typeof bindingScopeValues.Singleton, | ||
TActivated | Promise<TActivated> | ||
>; | ||
export interface ConstantValueBinding<TActivated> | ||
extends ScopedBinding< | ||
typeof bindingTypeValues.ConstantValue, | ||
typeof bindingScopeValues.Singleton, | ||
TActivated | Promise<TActivated> | ||
> { | ||
readonly value: Resolved<TActivated>; | ||
} |
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
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
77 changes: 77 additions & 0 deletions
77
packages/container/libraries/core/src/resolution/actions/resolveBindingActivations.ts
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 @@ | ||
import { ServiceIdentifier } from '@inversifyjs/common'; | ||
|
||
import { BindingActivation } from '../../binding/models/BindingActivation'; | ||
import { isPromise } from '../../common/calculations/isPromise'; | ||
import { ResolutionParams } from '../models/ResolutionParams'; | ||
import { Resolved } from '../models/Resolved'; | ||
|
||
export function resolveBindingActivations<TActivated>( | ||
params: ResolutionParams, | ||
serviceIdentifier: ServiceIdentifier<TActivated>, | ||
value: Resolved<TActivated>, | ||
): Resolved<TActivated> { | ||
const activations: Iterable<BindingActivation<TActivated>> | undefined = | ||
params.getActivations(serviceIdentifier); | ||
|
||
if (activations !== undefined) { | ||
if (isPromise(value)) { | ||
return resolveBindingActivationsFromIteratorAsync( | ||
value, | ||
activations[Symbol.iterator](), | ||
); | ||
} else { | ||
return resolveBindingActivationsFromIterator( | ||
value, | ||
activations[Symbol.iterator](), | ||
); | ||
} | ||
} | ||
|
||
return value; | ||
} | ||
|
||
function resolveBindingActivationsFromIterator<TActivated>( | ||
value: Awaited<TActivated>, | ||
activationsIterator: Iterator<BindingActivation<TActivated>>, | ||
): Resolved<TActivated> { | ||
let activatedValue: Awaited<TActivated> = value; | ||
|
||
let activationIteratorResult: IteratorResult<BindingActivation<TActivated>> = | ||
activationsIterator.next(); | ||
|
||
while (activationIteratorResult.done !== true) { | ||
const nextActivatedValue: Resolved<TActivated> = | ||
activationIteratorResult.value(activatedValue); | ||
|
||
if (isPromise(nextActivatedValue)) { | ||
return resolveBindingActivationsFromIteratorAsync( | ||
nextActivatedValue, | ||
activationsIterator, | ||
); | ||
} else { | ||
activatedValue = nextActivatedValue; | ||
} | ||
|
||
activationIteratorResult = activationsIterator.next(); | ||
} | ||
|
||
return activatedValue; | ||
} | ||
|
||
async function resolveBindingActivationsFromIteratorAsync<TActivated>( | ||
value: Promise<TActivated>, | ||
activationsIterator: Iterator<BindingActivation<TActivated>>, | ||
): Promise<Awaited<TActivated>> { | ||
let activatedValue: Awaited<TActivated> = await value; | ||
|
||
let activationIteratorResult: IteratorResult<BindingActivation<TActivated>> = | ||
activationsIterator.next(); | ||
|
||
while (activationIteratorResult.done !== true) { | ||
activatedValue = await activationIteratorResult.value(activatedValue); | ||
|
||
activationIteratorResult = activationsIterator.next(); | ||
} | ||
|
||
return activatedValue; | ||
} |
Oops, something went wrong.