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
feat(ivy): support injection flags at runtime #23518
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,7 +178,8 @@ export function diPublic(def: DirectiveDef<any>): void { | |
* @returns The instance found | ||
*/ | ||
export function directiveInject<T>(token: Type<T>): T; | ||
export function directiveInject<T>(token: Type<T>, flags?: InjectFlags): T|null; | ||
export function directiveInject<T>(token: Type<T>, flags: InjectFlags.Optional): T|null; | ||
export function directiveInject<T>(token: Type<T>, flags: InjectFlags): T; | ||
export function directiveInject<T>(token: Type<T>, flags = InjectFlags.Default): T|null { | ||
return getOrCreateInjectable<T>(getOrCreateNodeInjector(), token, flags); | ||
} | ||
|
@@ -329,8 +330,8 @@ function getClosestComponentAncestor(node: LViewNode | LElementNode): LElementNo | |
* @param flags Injection flags (e.g. CheckParent) | ||
* @returns The instance found | ||
*/ | ||
export function getOrCreateInjectable<T>(di: LInjector, token: Type<T>, flags?: InjectFlags): T| | ||
null { | ||
export function getOrCreateInjectable<T>( | ||
di: LInjector, token: Type<T>, flags: InjectFlags = InjectFlags.Default): T|null { | ||
const bloomHash = bloomHashBit(token); | ||
|
||
// If the token has a bloom hash, then it is a directive that is public to the injection system | ||
|
@@ -349,7 +350,7 @@ export function getOrCreateInjectable<T>(di: LInjector, token: Type<T>, flags?: | |
while (injector) { | ||
// Get the closest potential matching injector (upwards in the injector tree) that | ||
// *potentially* has the token. | ||
injector = bloomFindPossibleInjector(injector, bloomHash); | ||
injector = bloomFindPossibleInjector(injector, bloomHash, flags); | ||
|
||
// If no injector is found, we *know* that there is no ancestor injector that contains the | ||
// token, so we abort. | ||
|
@@ -360,11 +361,11 @@ export function getOrCreateInjectable<T>(di: LInjector, token: Type<T>, flags?: | |
// At this point, we have an injector which *may* contain the token, so we step through the | ||
// directives associated with the injector's corresponding node to get the directive instance. | ||
const node = injector.node; | ||
const flags = node.tNode !.flags; | ||
const count = flags & TNodeFlags.DirectiveCountMask; | ||
const nodeFlags = node.tNode !.flags; | ||
const count = nodeFlags & TNodeFlags.DirectiveCountMask; | ||
|
||
if (count !== 0) { | ||
const start = flags >> TNodeFlags.DirectiveStartingIndexShift; | ||
const start = nodeFlags >> TNodeFlags.DirectiveStartingIndexShift; | ||
const end = start + count; | ||
const defs = node.view.tView.directives !; | ||
|
||
|
@@ -385,15 +386,19 @@ export function getOrCreateInjectable<T>(di: LInjector, token: Type<T>, flags?: | |
return instance; | ||
} | ||
|
||
// The def wasn't found anywhere on this node, so it might be a false positive. | ||
// Traverse up the tree and continue searching. | ||
injector = injector.parent; | ||
// The def wasn't found anywhere on this node, so it was a false positive. | ||
// If flags permit, traverse up the tree and continue searching. | ||
if (flags & InjectFlags.Self || flags & InjectFlags.Host && !sameHostView(injector)) { | ||
injector = null; | ||
} else { | ||
injector = injector.parent; | ||
} | ||
} | ||
} | ||
|
||
// No directive was found for the given token. | ||
// TODO: implement optional, check-self, and check-parent. | ||
throw new Error('Implement'); | ||
if (flags & InjectFlags.Optional) return null; | ||
throw new Error(`Injector: NOT_FOUND [${stringify(token)}]`); | ||
} | ||
|
||
function searchMatchesQueuedForCreation<T>(node: LNode, token: any): T|null { | ||
|
@@ -443,18 +448,20 @@ function bloomHashBit(type: Type<any>): number|null { | |
* | ||
* @param injector The starting node injector to check | ||
* @param bloomBit The bit to check in each injector's bloom filter | ||
* @param flags The injection flags for this injection site (e.g. Optional or SkipSelf) | ||
* @returns An injector that might have the directive | ||
*/ | ||
export function bloomFindPossibleInjector(startInjector: LInjector, bloomBit: number): LInjector| | ||
null { | ||
export function bloomFindPossibleInjector( | ||
startInjector: LInjector, bloomBit: number, flags: InjectFlags): LInjector|null { | ||
// Create a mask that targets the specific bit associated with the directive we're looking for. | ||
// JS bit operations are 32 bits, so this will be a number between 2^0 and 2^31, corresponding | ||
// to bit positions 0 - 31 in a 32 bit integer. | ||
const mask = 1 << bloomBit; | ||
|
||
// Traverse up the injector tree until we find a potential match or until we know there *isn't* a | ||
// match. | ||
let injector: LInjector|null = startInjector; | ||
let injector: LInjector|null = | ||
flags & InjectFlags.SkipSelf ? startInjector.parent ! : startInjector; | ||
while (injector) { | ||
// Our bloom filter size is 256 bits, which is eight 32-bit bloom filter buckets: | ||
// bf0 = [0 - 31], bf1 = [32 - 63], bf2 = [64 - 95], bf3 = [96 - 127], etc. | ||
|
@@ -472,6 +479,8 @@ export function bloomFindPossibleInjector(startInjector: LInjector, bloomBit: nu | |
// this injector is a potential match. | ||
if ((value & mask) === mask) { | ||
return injector; | ||
} else if (flags & InjectFlags.Self || flags & InjectFlags.Host && !sameHostView(injector)) { | ||
return null; | ||
} | ||
|
||
// If the current injector does not have the directive, check the bloom filters for the ancestor | ||
|
@@ -491,6 +500,16 @@ export function bloomFindPossibleInjector(startInjector: LInjector, bloomBit: nu | |
return null; | ||
} | ||
|
||
/** | ||
* Checks whether the current injector and its parent are in the same host view. | ||
* | ||
* This is necessary to support @Host() decorators. If @Host() is set, we should stop searching once | ||
* the injector and its parent view don't match because it means we'd cross the view boundary. | ||
*/ | ||
function sameHostView(injector: LInjector): boolean { | ||
return !!injector.parent && injector.parent.node.view === injector.node.view; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. impressive, there are a |
||
} | ||
|
||
export class ReadFromInjectorFn<T> { | ||
constructor(readonly read: (injector: LInjector, node: LNode, directiveIndex?: number) => T) {} | ||
} | ||
|
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.
pls update the API docs