-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
one-event.ts
59 lines (52 loc) · 1.45 KB
/
one-event.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import type {ParseSelector} from 'typed-query-selector/parser.d.js';
import delegate, {
type DelegateEvent,
type DelegateOptions,
type EventType,
} from './delegate.js';
/**
* Delegates event to a selector and resolves after the first event
*/
async function oneEvent<
Selector extends string,
TElement extends Element = ParseSelector<Selector, HTMLElement>,
TEventType extends EventType = EventType,
>(
selector: Selector | Selector[],
type: TEventType,
options?: DelegateOptions
): Promise<DelegateEvent<GlobalEventHandlersEventMap[TEventType], TElement>>;
async function oneEvent<
TElement extends Element = HTMLElement,
TEventType extends EventType = EventType,
>(
selector: string | string[],
type: TEventType,
options?: DelegateOptions
): Promise<DelegateEvent<GlobalEventHandlersEventMap[TEventType], TElement>>;
// This type isn't exported as a declaration, so it needs to be duplicated above
async function oneEvent<
TElement extends Element,
TEventType extends EventType = EventType,
>(
selector: string | string[],
type: TEventType,
options: DelegateOptions = {},
): Promise<DelegateEvent<GlobalEventHandlersEventMap[TEventType], TElement> | undefined> {
return new Promise(resolve => {
options.once = true;
if (options.signal?.aborted) {
resolve(undefined);
}
options.signal?.addEventListener('abort', () => {
resolve(undefined);
});
delegate(
selector,
type,
resolve,
options,
);
});
}
export default oneEvent;