refactor(core): view engine - move handleEvent function from view to element
Some versions of TypeScript are super slow to compile functions that contain a lot of `if` conditions in them. Splitting the handle event expressions per element is similar to what we did in the old codegen.
This commit is contained in:
@ -9,12 +9,18 @@
|
||||
import {isDevMode} from '../application_ref';
|
||||
import {SecurityContext} from '../security';
|
||||
|
||||
import {BindingDef, BindingType, DebugContext, DisposableFn, ElementData, ElementOutputDef, NodeData, NodeDef, NodeFlags, NodeType, QueryValueType, Services, ViewData, ViewDefinition, ViewDefinitionFactory, ViewFlags, asElementData} from './types';
|
||||
import {BindingDef, BindingType, DebugContext, DisposableFn, ElementData, ElementHandleEventFn, ElementOutputDef, NodeData, NodeDef, NodeFlags, NodeType, QueryValueType, Services, ViewData, ViewDefinition, ViewDefinitionFactory, ViewFlags, asElementData} from './types';
|
||||
import {checkAndUpdateBinding, dispatchEvent, elementEventFullName, filterQueryId, getParentRenderElement, resolveViewDefinition, sliceErrorStack, splitMatchedQueriesDsl, splitNamespace} from './util';
|
||||
|
||||
const NOOP: any = () => {};
|
||||
|
||||
export function anchorDef(
|
||||
flags: NodeFlags, matchedQueriesDsl: [string | number, QueryValueType][],
|
||||
ngContentIndex: number, childCount: number, templateFactory?: ViewDefinitionFactory): NodeDef {
|
||||
ngContentIndex: number, childCount: number, handleEvent?: ElementHandleEventFn,
|
||||
templateFactory?: ViewDefinitionFactory): NodeDef {
|
||||
if (!handleEvent) {
|
||||
handleEvent = NOOP;
|
||||
}
|
||||
const {matchedQueries, references, matchedQueryIds} = splitMatchedQueriesDsl(matchedQueriesDsl);
|
||||
// skip the call to sliceErrorStack itself + the call to this function.
|
||||
const source = isDevMode() ? sliceErrorStack(2, 3) : '';
|
||||
@ -43,7 +49,7 @@ export function anchorDef(
|
||||
// will bet set by the view definition
|
||||
component: undefined,
|
||||
publicProviders: undefined,
|
||||
allProviders: undefined,
|
||||
allProviders: undefined, handleEvent
|
||||
},
|
||||
provider: undefined,
|
||||
text: undefined,
|
||||
@ -60,7 +66,10 @@ export function elementDef(
|
||||
bindings?:
|
||||
([BindingType.ElementClass, string] | [BindingType.ElementStyle, string, string] |
|
||||
[BindingType.ElementAttribute | BindingType.ElementProperty, string, SecurityContext])[],
|
||||
outputs?: (string | [string, string])[]): NodeDef {
|
||||
outputs?: (string | [string, string])[], handleEvent?: ElementHandleEventFn): NodeDef {
|
||||
if (!handleEvent) {
|
||||
handleEvent = NOOP;
|
||||
}
|
||||
// skip the call to sliceErrorStack itself + the call to this function.
|
||||
const source = isDevMode() ? sliceErrorStack(2, 3) : '';
|
||||
const {matchedQueries, references, matchedQueryIds} = splitMatchedQueriesDsl(matchedQueriesDsl);
|
||||
@ -131,7 +140,7 @@ export function elementDef(
|
||||
// will bet set by the view definition
|
||||
component: undefined,
|
||||
publicProviders: undefined,
|
||||
allProviders: undefined,
|
||||
allProviders: undefined, handleEvent,
|
||||
},
|
||||
provider: undefined,
|
||||
text: undefined,
|
||||
|
@ -205,6 +205,7 @@ export interface ElementDef {
|
||||
*/
|
||||
allProviders: {[tokenKey: string]: NodeDef};
|
||||
source: string;
|
||||
handleEvent: ElementHandleEventFn;
|
||||
}
|
||||
|
||||
export interface ElementOutputDef {
|
||||
@ -212,6 +213,8 @@ export interface ElementOutputDef {
|
||||
eventName: string;
|
||||
}
|
||||
|
||||
export type ElementHandleEventFn = (view: ViewData, eventName: string, event: any) => boolean;
|
||||
|
||||
export interface ProviderDef {
|
||||
type: ProviderType;
|
||||
token: any;
|
||||
|
@ -23,7 +23,7 @@ const NOOP = (): any => undefined;
|
||||
|
||||
export function viewDef(
|
||||
flags: ViewFlags, nodes: NodeDef[], updateDirectives?: ViewUpdateFn,
|
||||
updateRenderer?: ViewUpdateFn, handleEvent?: ViewHandleEventFn): ViewDefinition {
|
||||
updateRenderer?: ViewUpdateFn): ViewDefinition {
|
||||
// clone nodes and set auto calculated values
|
||||
if (nodes.length === 0) {
|
||||
throw new Error(`Illegal State: Views without nodes are not allowed!`);
|
||||
@ -131,6 +131,8 @@ export function viewDef(
|
||||
}
|
||||
currentParent = newParent;
|
||||
}
|
||||
const handleEvent: ViewHandleEventFn = (view, nodeIndex, eventName, event) =>
|
||||
nodes[nodeIndex].element.handleEvent(view, eventName, event);
|
||||
return {
|
||||
nodeFlags: viewNodeFlags,
|
||||
nodeMatchedQueries: viewMatchedQueries, flags,
|
||||
@ -143,6 +145,8 @@ export function viewDef(
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
function calculateReverseChildIndex(
|
||||
currentParent: NodeDef, i: number, childCount: number, nodeCount: number) {
|
||||
// Notes about reverse child order:
|
||||
|
Reference in New Issue
Block a user