refactor(ivy): minor cleanup in the listener instruction (#34255)

PR Close #34255
This commit is contained in:
Pawel Kozlowski 2019-12-05 16:32:28 +01:00 committed by Andrew Kushnir
parent f98aeca146
commit d3069dbec6
1 changed files with 8 additions and 5 deletions

View File

@ -119,6 +119,11 @@ function listenerInternal(
const firstCreatePass = tView.firstCreatePass; const firstCreatePass = tView.firstCreatePass;
const tCleanup: false|any[] = firstCreatePass && (tView.cleanup || (tView.cleanup = [])); const tCleanup: false|any[] = firstCreatePass && (tView.cleanup || (tView.cleanup = []));
// When the ɵɵlistener instruction was generated and is executed we know that there is either a
// native listener or a directive output on this element. As such we we know that we will have to
// register a listener and store its cleanup function on LView.
const lCleanup = getCleanup(lView);
ngDevMode && assertNodeOfPossibleTypes( ngDevMode && assertNodeOfPossibleTypes(
tNode, TNodeType.Element, TNodeType.Container, TNodeType.ElementContainer); tNode, TNodeType.Element, TNodeType.Container, TNodeType.ElementContainer);
@ -129,7 +134,6 @@ function listenerInternal(
const native = getNativeByTNode(tNode, lView) as RElement; const native = getNativeByTNode(tNode, lView) as RElement;
const resolved = eventTargetResolver ? eventTargetResolver(native) : EMPTY_OBJ as any; const resolved = eventTargetResolver ? eventTargetResolver(native) : EMPTY_OBJ as any;
const target = resolved.target || native; const target = resolved.target || native;
const lCleanup = getCleanup(lView);
const lCleanupIndex = lCleanup.length; const lCleanupIndex = lCleanup.length;
const idxOrTargetGetter = eventTargetResolver ? const idxOrTargetGetter = eventTargetResolver ?
(_lView: LView) => eventTargetResolver(unwrapRNode(_lView[tNode.index])).target : (_lView: LView) => eventTargetResolver(unwrapRNode(_lView[tNode.index])).target :
@ -195,7 +199,6 @@ function listenerInternal(
if (processOutputs && outputs !== null && (props = outputs[eventName])) { if (processOutputs && outputs !== null && (props = outputs[eventName])) {
const propsLength = props.length; const propsLength = props.length;
if (propsLength) { if (propsLength) {
const lCleanup = getCleanup(lView);
for (let i = 0; i < propsLength; i += 2) { for (let i = 0; i < propsLength; i += 2) {
const index = props[i] as number; const index = props[i] as number;
ngDevMode && assertDataInRange(lView, index); ngDevMode && assertDataInRange(lView, index);
@ -218,7 +221,7 @@ function listenerInternal(
} }
function executeListenerWithErrorHandling( function executeListenerWithErrorHandling(
lView: LView, tNode: TNode, listenerFn: (e?: any) => any, e: any): boolean { lView: LView, listenerFn: (e?: any) => any, e: any): boolean {
try { try {
// Only explicitly returning false from a listener should preventDefault // Only explicitly returning false from a listener should preventDefault
return listenerFn(e) !== false; return listenerFn(e) !== false;
@ -261,13 +264,13 @@ function wrapListener(
markViewDirty(startView); markViewDirty(startView);
} }
let result = executeListenerWithErrorHandling(lView, tNode, listenerFn, e); let result = executeListenerWithErrorHandling(lView, listenerFn, e);
// A just-invoked listener function might have coalesced listeners so we need to check for // A just-invoked listener function might have coalesced listeners so we need to check for
// their presence and invoke as needed. // their presence and invoke as needed.
let nextListenerFn = (<any>wrapListenerIn_markDirtyAndPreventDefault).__ngNextListenerFn__; let nextListenerFn = (<any>wrapListenerIn_markDirtyAndPreventDefault).__ngNextListenerFn__;
while (nextListenerFn) { while (nextListenerFn) {
// We should prevent default if any of the listeners explicitly return false // We should prevent default if any of the listeners explicitly return false
result = executeListenerWithErrorHandling(lView, tNode, nextListenerFn, e) && result; result = executeListenerWithErrorHandling(lView, nextListenerFn, e) && result;
nextListenerFn = (<any>nextListenerFn).__ngNextListenerFn__; nextListenerFn = (<any>nextListenerFn).__ngNextListenerFn__;
} }