deno.land / x / lume@v2.1.4 / core / events.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
type Listener<E extends Event> = [EventListener<E>, EventOptions | undefined];
/** * Class to manage the event listeners * and dispatch events */export default class Events<E extends Event> { listeners = new Map<string, Set<Listener<E>>>();
/** Assign a listener to an event */ addEventListener( type: string, listenerFn: EventListener<E>, options?: EventOptions, ) { const listeners = this.listeners.get(type) || new Set(); const listener: Listener<E> = [listenerFn, options];
listeners.add(listener); this.listeners.set(type, listeners);
// Remove on abort if (options?.signal) { options.signal.addEventListener("abort", () => { listeners.delete(listener); }); }
return this; }
/** Dispatch an event */ async dispatchEvent(event: E) { const { type } = event; const listeners = this.listeners.get(type);
if (listeners) { for (const listener of listeners) { const [listenerFn, listenerOptions] = listener;
// Remove the listener if it's a once listener if (listenerOptions?.once) { listeners.delete(listener); }
if (await listenerFn(event) === false) { return false; } } }
const customEvent = new CustomEvent(`lume:${type}`, { cancelable: true, detail: event, });
return dispatchEvent(customEvent); }}
/** An event object */export interface Event { /** The event type */ type: string;}
/** Event listener */export type EventListener<E extends Event> = (event: E) => unknown;
/** The available options for events */export interface EventOptions { /** * To indicate that the listener should be invoked at most once * after being added */ once?: boolean;
/** * The listener will be removed * when the given AbortSignal object's abort() method is called */ signal?: AbortSignal;}
lume

Version Info

Tagged at
7 months ago