docs(ivy): clarify injector API docs (#25166)

PR Close #25166
This commit is contained in:
Victor Berchet 2018-07-27 09:47:34 -07:00 committed by Miško Hevery
parent ce98634dfd
commit d3c0915598

View File

@ -37,7 +37,7 @@ import {ViewRef} from './view_ref';
/** /**
* If a directive is diPublic, bloomAdd sets a property on the instance with this constant as * If a directive is diPublic, bloomAdd sets a property on the type with this constant as
* the key and the directive's unique ID as the value. This allows us to map directives to their * the key and the directive's unique ID as the value. This allows us to map directives to their
* bloom filter bit for DI. * bloom filter bit for DI.
*/ */
@ -157,11 +157,10 @@ export function diPublic(def: DirectiveDefInternal<any>): void {
} }
/** /**
* Searches for an instance of the given type up the injector tree and returns * Returns the value associated to the given token from the injectors.
* that instance if found.
* *
* If not found, it will propagate up to the next parent injector until the token * `directiveInject` is intended to be used for directive, component and pipe factories.
* is found or the top is reached. * All other injection use `inject` which does not walk the node injector tree.
* *
* Usage example (in factory function): * Usage example (in factory function):
* *
@ -174,12 +173,9 @@ export function diPublic(def: DirectiveDefInternal<any>): void {
* }); * });
* } * }
* *
* NOTE: use `directiveInject` with `@Directive`, `@Component`, and `@Pipe`. For * @param token the type or token to inject
* all other injection use `inject` which does not walk the DOM render tree. * @param flags Injection flags
* * @returns the value from the injector or `null` when not found
* @param token The directive type to search for
* @param flags Injection flags (e.g. CheckParent)
* @returns The instance found
*/ */
export function directiveInject<T>(token: Type<T>): T; export function directiveInject<T>(token: Type<T>): T;
export function directiveInject<T>(token: Type<T>, flags: InjectFlags.Optional): T|null; export function directiveInject<T>(token: Type<T>, flags: InjectFlags.Optional): T|null;
@ -332,21 +328,15 @@ function getClosestComponentAncestor(node: LViewNode | LElementNode): LElementNo
} }
/** /**
* Searches for an instance of the given directive type up the injector tree and returns * Returns the value associated to the given token from the injectors.
* that instance if found.
* *
* Specifically, it gets the bloom filter bit associated with the directive (see bloomHashBit), * Look for the injector providing the token by walking up the node injector tree and then
* checks that bit against the bloom filter structure to identify an injector that might have * the module injector tree.
* the directive (see bloomFindPossibleInjector), then searches the directives on that injector
* for a match.
* *
* If not found, it will propagate up to the next parent injector until the token * @param nodeInjector Node injector where the search should start
* is found or the top is reached. * @param token The token to look for
* * @param flags Injection flags
* @param di Node injector where the search should start * @returns the value from the injector or `null` when not found
* @param token The directive type to search for
* @param flags Injection flags (e.g. CheckParent)
* @returns The instance found
*/ */
export function getOrCreateInjectable<T>( export function getOrCreateInjectable<T>(
di: LInjector, token: Type<T>, flags: InjectFlags = InjectFlags.Default): T|null { di: LInjector, token: Type<T>, flags: InjectFlags = InjectFlags.Default): T|null {
@ -433,16 +423,15 @@ function searchMatchesQueuedForCreation<T>(node: LNode, token: any): T|null {
} }
/** /**
* Given a directive type, this function returns the bit in an injector's bloom filter * Returns the bit in an injector's bloom filter that should be used to determine whether or not
* that should be used to determine whether or not the directive is present. * the directive might be provided by the injector.
* *
* When the directive was added to the bloom filter, it was given a unique ID that can be * When a directive is public, it is added to the bloom filter and given a unique ID that can be
* retrieved on the class. Since there are only BLOOM_SIZE slots per bloom filter, the directive's * retrieved on the Type. When the directive isn't public or the token is not a directive `null`
* ID must be modulo-ed by BLOOM_SIZE to get the correct bloom bit (directives share slots after * is returned as the node injector can not possibly provide that token.
* BLOOM_SIZE is reached).
* *
* @param type The directive type * @param token the injection token
* @returns The bloom bit to check for the directive * @returns the matching bit to check in the bloom filter or `null` if the token is not known.
*/ */
function bloomHashBit(type: Type<any>): number|null { function bloomHashBit(type: Type<any>): number|null {
let id: number|undefined = (type as any)[NG_ELEMENT_ID]; let id: number|undefined = (type as any)[NG_ELEMENT_ID];