Skip to content

Commit

Permalink
DomEventSwitchOptionsHandler[Single|Multiple] explicit typing & ext…
Browse files Browse the repository at this point in the history
…raction
  • Loading branch information
Stassi committed Nov 22, 2024
1 parent a7df4fa commit 0d69ae5
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/dom/event/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ type DomEventSwitchOptionsBase = {
context: unknown
}>

export type DomEventSwitchOptionsUsesHandler =
| (DomEventSwitchOptionsBase & {
event: string
handler: EventHandlerAsync
})
| (DomEventSwitchOptionsBase & {
handlers: EventHandlersAsync
})
type DomEventSwitchOptionsHandlerSingle = {
event: string
handler: EventHandlerAsync
}

type DomEventSwitchOptionsHandlerMultiple = {
handlers: EventHandlersAsync
}

export type DomEventSwitchOptionsUsesHandler = DomEventSwitchOptionsBase &
(DomEventSwitchOptionsHandlerSingle | DomEventSwitchOptionsHandlerMultiple)

type SwitchableStates = 'on' | 'off'
export type DomEventSwitchOptions<Switchable extends SwitchableStates> =
Expand All @@ -46,15 +49,15 @@ function domEventSwitch<Switchable extends SwitchableStates>(
>options

if ('event' in options && 'handler' in options) {
const { handler, event } = options
const { handler, event }: DomEventSwitchOptionsHandlerSingle = options
return DomEvent[switchable](
element,
event,
<EventHandlerSync>handler,
context,
)
} else if ('handlers' in options) {
const { handlers } = options
const { handlers }: DomEventSwitchOptionsHandlerMultiple = options
return DomEvent[switchable](element, <EventHandlersSync>handlers, context)
} else if (switchable === 'off') return DomEvent.off(element)

Expand Down

0 comments on commit 0d69ae5

Please sign in to comment.