Created
August 7, 2023 06:45
-
-
Save guiseek/d89918aeb5d77c596fcf8c7e322bce3c to your computer and use it in GitHub Desktop.
Observe Event
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type DOMEventMapDefinitions = [ | |
[HTMLElement, HTMLElementEventMap], | |
[Document, DocumentEventMap], | |
[Window, WindowEventMap], | |
[FileReader, FileReaderEventMap], | |
[Element, ElementEventMap], | |
[Animation, AnimationEventMap], | |
[EventSource, EventSourceEventMap], | |
[AbortSignal, AbortSignalEventMap], | |
[AbstractWorker, AbstractWorkerEventMap], | |
[MediaQueryList, MediaQueryListEventMap] | |
// ... | |
]; | |
export type { DOMEventMapDefinitions } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { DOMEventMapDefinitions } from './dom-event-map-definitions'; | |
type DOMEventSubscriber = DOMEventMapDefinitions[number][0]; | |
export type { DOMEventSubscriber } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { DOMEventMapDefinitions } from './dom-event-map-definitions'; | |
import type { DOMEventSubscriber } from './dom-event-subscriber'; | |
type GetDOMEventMaps<T extends DOMEventSubscriber> = MapDefinitionToEventMap< | |
DOMEventMapDefinitions, | |
T | |
>; | |
export type { GetDOMEventMaps } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type MapDefinitionToEventMap<D extends { [K: number]: any[] }, T> = { | |
[K in keyof D]: D[K] extends any[] | |
? T extends D[K][0] | |
? D[K][1] | |
: never | |
: never; | |
}; | |
export type { MapDefinitionToEventMap } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type MapEventMapsToEvent< | |
D extends { [K: number]: any }, | |
T extends PropertyKey | |
> = { | |
[K in keyof D]: D[K] extends never | |
? never | |
: T extends keyof D[K] | |
? D[K][T] | |
: never; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type MapEventMapsToKeys<D extends { [K: number]: any }> = { | |
[K in keyof D]: D[K] extends never ? never : keyof D[K]; | |
}; | |
export type { MapEventMapsToKeys } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { DOMEventSubscriber } from './dom-event-subscriber'; | |
import type { MapEventMapsToKeys } from './map-event-maps-to-keys'; | |
import type { GetDOMEventMaps } from './get-dom-event-maps'; | |
import type { MapEventMapsToEvent } from './map-event-maps-to-event'; | |
import { fromEvent, map } from 'rxjs'; | |
export function observeEvent< | |
T extends DOMEventSubscriber, | |
K extends MapEventMapsToKeys<GetDOMEventMaps<T>>[number] & string | |
>( | |
element: T, | |
type: K, | |
handler: ( | |
this: T, | |
ev: MapEventMapsToEvent<GetDOMEventMaps<T>, K>[number] | |
) => any | |
) { | |
return fromEvent<MapEventMapsToEvent<GetDOMEventMaps<T>, K>[number]>( | |
element, | |
type | |
).pipe(map(handler)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment