@@ -70,28 +70,17 @@ function getActionMethodName(action: string): string {
7070 return action . slice ( action . lastIndexOf ( '#' ) + 1 )
7171}
7272
73- // ControllerEventHandler is a global event handler that dispatches events to
74- // controllers. We use a global event handler over bindings functions because
75- // this is far more performant; creating functions for each `addEventListener`
76- // would be very costly for CPU performance (and memory), while registering a
77- // single handler for every event keeps things relatively performant.
73+ // Bind a single function to all events to avoid anonymous closure performance penalty.
7874function handleEvent ( event : Event ) {
7975 const el = event . currentTarget
8076 if ( ! ( el instanceof Element ) ) return
8177 for ( const action of ( el . getAttribute ( 'data-action' ) || '' ) . split ( ' ' ) ) {
82- // We want to dispatch this event, only to the subscribers; we filter by
83- // event.type to find which actions should fire
84- const eventType = getActionEventName ( action )
85- if ( event . type !== eventType ) continue
86- // We need to find the closest controller to dispatch the event to.
78+ if ( event . type !== getActionEventName ( action ) ) continue
8779 const tagName = getActionControllerName ( action )
88- // The controller should be "well known" in that `bind()` should have
89- // been called on it.
80+ // Dispatch only to Catalyst elements.
9081 if ( ! controllers . has ( tagName ) ) continue
9182 const controller = el . closest ( tagName ) as Element & Record < string , ( ev : Event ) => unknown >
9283 if ( ! controller ) continue
93- // Finally we need to get the right method to call on the controller.
94- // The method also needs to exist!
9584 const method = getActionMethodName ( action )
9685 if ( typeof controller [ method ] === 'function' ) {
9786 controller [ method ] ( event )
0 commit comments