MCPcopy
hub / github.com/angular/angular / getLContext

Function getLContext

packages/core/src/render3/context_discovery.ts:42–130  ·  view source on GitHub ↗
(target: any)

Source from the content-addressed store, hash-verified

40 * @param target Component, Directive or DOM Node.
41 */
42export function getLContext(target: any): LContext | null {
43 let mpValue = readPatchedData(target);
44 if (mpValue) {
45 // only when it's an array is it considered an LView instance
46 // ... otherwise it's an already constructed LContext instance
47 if (isLView(mpValue)) {
48 const lView: LView = mpValue!;
49 let nodeIndex: number;
50 let component: any = undefined;
51 let directives: any[] | null | undefined = undefined;
52
53 if (isComponentInstance(target)) {
54 nodeIndex = findViaComponent(lView, target);
55 if (nodeIndex == -1) {
56 throw new Error('The provided component was not found in the application');
57 }
58 component = target;
59 } else if (isDirectiveInstance(target)) {
60 nodeIndex = findViaDirective(lView, target);
61 if (nodeIndex == -1) {
62 throw new Error('The provided directive was not found in the application');
63 }
64 directives = getDirectivesAtNodeIndex(nodeIndex, lView);
65 } else {
66 nodeIndex = findViaNativeElement(lView, target as RElement);
67 if (nodeIndex == -1) {
68 return null;
69 }
70 }
71
72 // the goal is not to fill the entire context full of data because the lookups
73 // are expensive. Instead, only the target data (the element, component, container, ICU
74 // expression or directive details) are filled into the context. If called multiple times
75 // with different target values then the missing target data will be filled in.
76 const native = unwrapRNode(lView[nodeIndex]);
77 const existingCtx = readPatchedData(native);
78 const context: LContext =
79 existingCtx && !Array.isArray(existingCtx)
80 ? existingCtx
81 : createLContext(lView, nodeIndex, native);
82
83 // only when the component has been discovered then update the monkey-patch
84 if (component && context.component === undefined) {
85 context.component = component;
86 attachPatchData(context.component, context);
87 }
88
89 // only when the directives have been discovered then update the monkey-patch
90 if (directives && context.directives === undefined) {
91 context.directives = directives;
92 for (let i = 0; i < directives.length; i++) {
93 attachPatchData(directives[i], context);
94 }
95 }
96
97 attachPatchData(context.native, context);
98 mpValue = context;
99 }

Callers 15

query_spec.tsFile · 0.90
nameMethod · 0.90
propertiesMethod · 0.90
attributesMethod · 0.90
_queryAllFunction · 0.90
discoverDiGraphFunction · 0.90
getControlFlowBlocksFunction · 0.90
getComponentFunction · 0.90
getContextFunction · 0.90

Calls 13

isLViewFunction · 0.90
unwrapRNodeFunction · 0.90
assertDomNodeFunction · 0.90
readPatchedDataFunction · 0.85
isComponentInstanceFunction · 0.85
findViaComponentFunction · 0.85
isDirectiveInstanceFunction · 0.85
findViaDirectiveFunction · 0.85
getDirectivesAtNodeIndexFunction · 0.85
findViaNativeElementFunction · 0.85
createLContextFunction · 0.85
attachPatchDataFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…