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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
/*
Copyright 2017 Jeremy Scheff
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing
permissions and limitations under the License.
*/
import { InvalidStateError } from "./errors.js";
import FakeEvent, { EventType } from "./FakeEvent.js";
import {
EventTarget,
Event,
EventListenerOrEventListenerObject,
EventListener,
} from "../idbtypes";
type EventTypeProp =
| "onabort"
| "onblocked"
| "oncomplete"
| "onerror"
| "onsuccess"
| "onupgradeneeded"
| "onversionchange";
/** @public */
export interface Listener {
callback: EventListenerOrEventListenerObject;
capture: boolean;
type: EventType;
}
const stopped = (event: FakeEvent, listener: Listener) => {
return (
event.immediatePropagationStopped ||
(event.eventPhase === event.CAPTURING_PHASE &&
listener.capture === false) ||
(event.eventPhase === event.BUBBLING_PHASE && listener.capture === true)
);
};
// http://www.w3.org/TR/dom/#concept-event-listener-invoke
const invokeEventListeners = (event: FakeEvent, obj: FakeEventTarget) => {
event.currentTarget = obj;
// The callback might cause obj.listeners to mutate as we traverse it.
// Take a copy of the array so that nothing sneaks in and we don't lose
// our place.
for (const listener of obj.listeners.slice()) {
if (event.type !== listener.type || stopped(event, listener)) {
continue;
}
// @ts-ignore
listener.callback.call(event.currentTarget, event);
}
const typeToProp: { [key in EventType]: EventTypeProp } = {
abort: "onabort",
blocked: "onblocked",
complete: "oncomplete",
error: "onerror",
success: "onsuccess",
upgradeneeded: "onupgradeneeded",
versionchange: "onversionchange",
};
const prop = typeToProp[event.type];
if (prop === undefined) {
throw new Error(`Unknown event type: "${event.type}"`);
}
const callback = event.currentTarget[prop];
if (callback) {
const listener = {
callback,
capture: false,
type: event.type,
};
if (!stopped(event, listener)) {
// @ts-ignore
listener.callback.call(event.currentTarget, event);
}
}
};
/** @public */
abstract class FakeEventTarget implements EventTarget {
public readonly listeners: Listener[] = [];
// These will be overridden in individual subclasses and made not readonly
public readonly onabort: EventListener | null = null;
public readonly onblocked: EventListener | null = null;
public readonly oncomplete: EventListener | null = null;
public readonly onerror: EventListener | null = null;
public readonly onsuccess: EventListener | null = null;
public readonly onclose: EventListener | null = null;
public readonly onupgradeneeded: EventListener | null = null;
public readonly onversionchange: EventListener | null = null;
static enableTracing: boolean = false;
public addEventListener(
type: EventType,
listener: EventListenerOrEventListenerObject | null,
capture = false,
) {
if (typeof listener === "function") {
this.listeners.push({
callback: listener,
capture,
type,
});
} else if (typeof listener === "object" && listener != null) {
this.listeners.push({
callback: (e: Event) => listener.handleEvent(e),
capture,
type,
});
}
}
public removeEventListener(
type: EventType,
callback: EventListenerOrEventListenerObject,
capture = false,
) {
const i = this.listeners.findIndex((listener) => {
return (
listener.type === type &&
listener.callback === callback &&
listener.capture === capture
);
});
this.listeners.splice(i, 1);
}
// http://www.w3.org/TR/dom/#dispatching-events
public dispatchEvent(event: Event): boolean {
if (!(event instanceof FakeEvent)) {
throw Error("dispatchEvent only works with FakeEvent");
}
const fe = event as FakeEvent;
if (event.dispatched || !event.initialized) {
throw new InvalidStateError("The object is in an invalid state.");
}
fe.isTrusted = false;
fe.dispatched = true;
fe.target = this;
// NOT SURE WHEN THIS SHOULD BE SET event.eventPath = [];
fe.eventPhase = event.CAPTURING_PHASE;
if (FakeEventTarget.enableTracing) {
console.log(
`dispatching '${event.type}' event along path with ${event.eventPath.length} elements`,
);
}
for (const obj of event.eventPath) {
if (!event.propagationStopped) {
invokeEventListeners(event, obj);
}
}
fe.eventPhase = event.AT_TARGET;
if (!event.propagationStopped) {
invokeEventListeners(event, fe.target);
}
if (event.bubbles) {
fe.eventPath.reverse();
fe.eventPhase = event.BUBBLING_PHASE;
if (fe.eventPath.length === 0 && event.type === "error") {
console.error("Unhandled error event: ", event.target);
}
for (const obj of event.eventPath) {
if (!event.propagationStopped) {
invokeEventListeners(event, obj);
}
}
}
fe.dispatched = false;
fe.eventPhase = event.NONE;
fe.currentTarget = null;
if (event.canceled) {
return false;
}
return true;
}
}
export default FakeEventTarget;
|