perf(ivy): guard listening to outputs with isDirectiveHost (#32495)

PR Close #32495
This commit is contained in:
Pawel Kozlowski 2019-09-05 15:53:49 +02:00 committed by Kara Erickson
parent f06505aafd
commit 527ce3b142

View File

@ -111,6 +111,7 @@ function listenerInternal(
listenerFn: (e?: any) => any, useCapture = false, listenerFn: (e?: any) => any, useCapture = false,
eventTargetResolver?: GlobalTargetResolver): void { eventTargetResolver?: GlobalTargetResolver): void {
const tView = lView[TVIEW]; const tView = lView[TVIEW];
const isTNodeDirectiveHost = isDirectiveHost(tNode);
const firstTemplatePass = tView.firstTemplatePass; const firstTemplatePass = tView.firstTemplatePass;
const tCleanup: false|any[] = firstTemplatePass && (tView.cleanup || (tView.cleanup = [])); const tCleanup: false|any[] = firstTemplatePass && (tView.cleanup || (tView.cleanup = []));
@ -150,7 +151,7 @@ function listenerInternal(
// Also, we don't have to search for existing listeners is there are no directives // Also, we don't have to search for existing listeners is there are no directives
// matching on a given node as we can't register multiple event handlers for the same event in // matching on a given node as we can't register multiple event handlers for the same event in
// a template (this would mean having duplicate attributes). // a template (this would mean having duplicate attributes).
if (!eventTargetResolver && isDirectiveHost(tNode)) { if (!eventTargetResolver && isTNodeDirectiveHost) {
existingListener = findExistingListener(lView, eventName, tNode.index); existingListener = findExistingListener(lView, eventName, tNode.index);
} }
if (existingListener !== null) { if (existingListener !== null) {
@ -185,34 +186,36 @@ function listenerInternal(
} }
// subscribe to directive outputs // subscribe to directive outputs
let outputs = tNode.outputs; if (isTNodeDirectiveHost && processOutputs) {
if (outputs === undefined) { let outputs = tNode.outputs;
// if we create TNode here, inputs must be undefined so we know they still need to be if (outputs === undefined) {
// checked // if we create TNode here, inputs must be undefined so we know they still need to be
outputs = tNode.outputs = generatePropertyAliases(tView, tNode, BindingDirection.Output); // checked
} outputs = tNode.outputs = generatePropertyAliases(tView, tNode, BindingDirection.Output);
}
let props: PropertyAliasValue|undefined; let props: PropertyAliasValue|undefined;
if (processOutputs && outputs !== null && (props = outputs[eventName])) { if (outputs !== null && (props = outputs[eventName])) {
const propsLength = props.length; const propsLength = props.length;
if (propsLength) { if (propsLength) {
const lCleanup = getCleanup(lView); const lCleanup = getCleanup(lView);
for (let i = 0; i < propsLength; i += 3) { for (let i = 0; i < propsLength; i += 3) {
const index = props[i] as number; const index = props[i] as number;
ngDevMode && assertDataInRange(lView, index); ngDevMode && assertDataInRange(lView, index);
const minifiedName = props[i + 2]; const minifiedName = props[i + 2];
const directiveInstance = lView[index]; const directiveInstance = lView[index];
const output = directiveInstance[minifiedName]; const output = directiveInstance[minifiedName];
if (ngDevMode && !isObservable(output)) { if (ngDevMode && !isObservable(output)) {
throw new Error( throw new Error(
`@Output ${minifiedName} not initialized in '${directiveInstance.constructor.name}'.`); `@Output ${minifiedName} not initialized in '${directiveInstance.constructor.name}'.`);
}
const subscription = output.subscribe(listenerFn);
const idx = lCleanup.length;
lCleanup.push(listenerFn, subscription);
tCleanup && tCleanup.push(eventName, tNode.index, idx, -(idx + 1));
} }
const subscription = output.subscribe(listenerFn);
const idx = lCleanup.length;
lCleanup.push(listenerFn, subscription);
tCleanup && tCleanup.push(eventName, tNode.index, idx, -(idx + 1));
} }
} }
} }