Skip to content

Commit 795633b

Browse files
dgrahamkeithamus
andcommitted
Promote handleEvent to top-level function
Co-authored-by: Keith Cirkel <keithamus@users.noreply.github.com>
1 parent 90a6b75 commit 795633b

1 file changed

Lines changed: 21 additions & 23 deletions

File tree

src/bind.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -75,28 +75,26 @@ function getActionMethodName(action: string): string {
7575
// this is far more performant; creating functions for each `addEventListener`
7676
// would be very costly for CPU performance (and memory), while registering a
7777
// single handler for every event keeps things relatively performant.
78-
const ControllerEventHandler = {
79-
handleEvent(event: Event) {
80-
const el = event.currentTarget
81-
if (!(el instanceof Element)) return
82-
for (const action of (el.getAttribute('data-action') || '').split(' ')) {
83-
// We want to dispatch this event, only to the subscribers; we filter by
84-
// event.type to find which actions should fire
85-
const eventType = getActionEventName(action)
86-
if (event.type !== eventType) continue
87-
// We need to find the closest controller to dispatch the event to.
88-
const tagName = getActionControllerName(action)
89-
// The controller should be "well known" in that `bind()` should have
90-
// been called on it.
91-
if (!controllers.has(tagName)) continue
92-
const controller = el.closest(tagName) as Element & Record<string, (ev: Event) => unknown>
93-
if (!controller) continue
94-
// Finally we need to get the right method to call on the controller.
95-
// The method also needs to exist!
96-
const method = getActionMethodName(action)
97-
if (typeof controller[method] === 'function') {
98-
controller[method](event)
99-
}
78+
function handleEvent(event: Event) {
79+
const el = event.currentTarget
80+
if (!(el instanceof Element)) return
81+
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.
87+
const tagName = getActionControllerName(action)
88+
// The controller should be "well known" in that `bind()` should have
89+
// been called on it.
90+
if (!controllers.has(tagName)) continue
91+
const controller = el.closest(tagName) as Element & Record<string, (ev: Event) => unknown>
92+
if (!controller) continue
93+
// Finally we need to get the right method to call on the controller.
94+
// The method also needs to exist!
95+
const method = getActionMethodName(action)
96+
if (typeof controller[method] === 'function') {
97+
controller[method](event)
10098
}
10199
}
102100
}
@@ -110,7 +108,7 @@ function bindActions(el: Element) {
110108
for (const action of (el.getAttribute('data-action') || '').split(' ')) {
111109
const event = getActionEventName(action)
112110
if (!elementBindings.has(event)) {
113-
el.addEventListener(event, ControllerEventHandler)
111+
el.addEventListener(event, handleEvent)
114112
}
115113
elementBindings.add(event)
116114
}

0 commit comments

Comments
 (0)