-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathEventTarget.ts
More file actions
171 lines (143 loc) · 4.74 KB
/
EventTarget.ts
File metadata and controls
171 lines (143 loc) · 4.74 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import {HOOKS, PATH, LISTENERS, OWNER_DOCUMENT} from './constants.ts';
import {fireEvent, EventPhase} from './Event.ts';
import {CAPTURE_MARKER, type Event} from './Event.ts';
import type {ChildNode} from './ChildNode.ts';
import type {Document} from './Document.ts';
const ONCE_LISTENERS = Symbol('onceListeners');
export class EventTarget {
[LISTENERS]:
| Map<string, Set<EventListenerOrEventListenerObject>>
| undefined = undefined;
[ONCE_LISTENERS]:
| WeakMap<EventListenerOrEventListenerObject, EventListener>
| undefined = undefined;
/**
* Property set by entities that extend this class that are part of the DOM tree.
* @internal
*/
[OWNER_DOCUMENT]: Document | undefined = undefined;
addEventListener(
type: string,
listener: EventListenerOrEventListenerObject | null,
options?: boolean | AddEventListenerOptions,
) {
if (listener == null) return;
const capture = options === true || (options && options.capture === true);
const once = typeof options === 'object' && options.once === true;
const signal = typeof options === 'object' ? options.signal : undefined;
const key = `${type}${capture ? CAPTURE_MARKER : ''}`;
let normalizedListener = listener;
if (once) {
normalizedListener = function normalizedListener(
this: EventTarget,
...args: Parameters<EventListener>
) {
this.removeEventListener(type, listener, options);
return typeof listener === 'object'
? listener.handleEvent(...args)
: listener.call(this, ...args);
};
let onceListeners = this[ONCE_LISTENERS];
if (!onceListeners) {
onceListeners = new WeakMap();
this[ONCE_LISTENERS] = onceListeners;
}
onceListeners.set(listener, normalizedListener);
}
let listeners = this[LISTENERS];
if (!listeners) {
listeners = new Map();
this[LISTENERS] = listeners;
}
let list = listeners.get(key);
if (!list) {
list = new Set();
listeners.set(key, list);
}
if (list.has(normalizedListener)) return;
signal?.addEventListener(
'abort',
() => {
removeEventListener.call(this, type, listener, options);
},
{once: true},
);
list.add(normalizedListener);
this[OWNER_DOCUMENT]?.defaultView[HOOKS].addEventListener?.(
this as any,
type,
listener,
options,
);
}
removeEventListener(
type: string,
listener: EventListenerOrEventListenerObject | null,
options?: boolean | EventListenerOptions,
) {
return removeEventListener.call(this, type, listener, options);
}
// function isChildNode(node: EventTarget): node is ChildNode {
// return PARENT in node;
// }
dispatchEvent(event: Event) {
const path: EventTarget[] = [];
// instanceof here is just to keep TypeScript happy
let target = this as unknown as ChildNode | null;
while (target != null) {
path.push(target);
target = target.parentNode;
}
// while (target instanceof Node && (target = target.parentNode)) {
// path.push(target);
// }
// Safely set target property only if it's writable (native DOM Events have read-only target)
try {
event.target = this;
} catch {
// target property is read-only on native DOM Events
// Event will still function but may have different target than expected
}
try {
event.srcElement = this;
} catch {
// srcElement property may also be read-only on some implementations
}
event[PATH] = path;
for (let i = path.length; i--; ) {
fireEvent(event, path[i]!, EventPhase.CAPTURING_PHASE);
if (event.cancelBubble) return event.defaultPrevented;
}
const bubblePath = event.bubbles ? path : path.slice(0, 1);
for (let i = 0; i < bubblePath.length; i++) {
fireEvent(event, bubblePath[i]!, EventPhase.BUBBLING_PHASE);
if (event.cancelBubble) return event.defaultPrevented;
}
return event.defaultPrevented;
}
}
function removeEventListener(
this: EventTarget,
type: string,
listener: EventListenerOrEventListenerObject | null,
options?: boolean | EventListenerOptions,
) {
if (listener == null) return;
const onceListeners = this[ONCE_LISTENERS];
const normalizedListener = onceListeners?.get(listener) ?? listener;
onceListeners?.delete(listener);
const capture = options === true || (options && options.capture === true);
const key = `${type}${capture ? CAPTURE_MARKER : ''}`;
const list = this[LISTENERS]?.get(key);
if (list) {
const deleted = list.delete(normalizedListener);
if (deleted) {
this[OWNER_DOCUMENT]?.defaultView[HOOKS].removeEventListener?.(
this as any,
type,
listener,
options,
);
}
}
}