(target: any)
| 40 | * @param target Component, Directive or DOM Node. |
| 41 | */ |
| 42 | export 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 | } |
no test coverage detected
searching dependent graphs…