-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathevent.ts
118 lines (106 loc) · 4.5 KB
/
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
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
/**
* Functions for working with events.
* @module
*/
import "./external/event-target-polyfill/index.ts";
/**
* Creates an `ErrorEvent` instance based on the given options. If the
* `ErrorEvent` constructor is not available, the generic `Event` constructor
* will be used instead, and the options will be attached to the event as its
* properties.
*/
export function createErrorEvent(type: "error", options?: ErrorEventInit): ErrorEvent;
export function createErrorEvent(type: string, options?: ErrorEventInit): ErrorEvent;
export function createErrorEvent(type: string, options: ErrorEventInit = {}): ErrorEvent {
if (typeof ErrorEvent === "function") {
return new ErrorEvent(type, options);
} else {
const event = new Event(type, {
bubbles: options?.bubbles ?? false,
cancelable: options?.cancelable ?? false,
composed: options?.composed ?? false,
});
Object.defineProperties(event, {
message: { configurable: true, value: options?.message ?? "" },
filename: { configurable: true, value: options?.filename ?? "" },
lineno: { configurable: true, value: options?.lineno ?? 0 },
colno: { configurable: true, value: options?.colno ?? 0 },
error: { configurable: true, value: options?.error ?? undefined },
});
return event as ErrorEvent;
}
}
/**
* Creates a `CloseEvent` instance based on the given options. If the
* `CloseEvent` constructor is not available, the generic `Event` constructor
* will be used instead, and the options will be attached to the event as its
* properties.
*/
export function createCloseEvent(type: "close", options?: CloseEventInit): CloseEvent;
export function createCloseEvent(type: string, options?: CloseEventInit): CloseEvent;
export function createCloseEvent(type: string, options: CloseEventInit = {}): CloseEvent {
if (typeof CloseEvent === "function") {
return new CloseEvent(type, options);
} else {
const event = new Event(type, {
bubbles: options?.bubbles ?? false,
cancelable: options?.cancelable ?? false,
composed: options?.composed ?? false,
});
Object.defineProperties(event, {
code: { configurable: true, value: options.code ?? 0 },
reason: { configurable: true, value: options.reason ?? "" },
wasClean: { configurable: true, value: options.wasClean ?? false },
});
return event as CloseEvent;
}
}
/**
* Creates a `ProgressEvent` instance based on the given options. If the
* `ProgressEvent` constructor is not available, the generic `Event` constructor
* will be used instead, and the options will be attached to the event as its
* properties.
*/
export function createProgressEvent(type: "progress", options?: ProgressEventInit): ProgressEvent;
export function createProgressEvent(type: string, options?: ProgressEventInit): ProgressEvent;
export function createProgressEvent(type: string, options: ProgressEventInit = {}): ProgressEvent {
if (typeof ProgressEvent === "function") {
return new ProgressEvent(type, options);
} else {
const event = new Event(type, {
bubbles: options?.bubbles ?? false,
cancelable: options?.cancelable ?? false,
composed: options?.composed ?? false,
});
Object.defineProperties(event, {
lengthComputable: { configurable: true, value: options?.lengthComputable ?? false },
loaded: { configurable: true, value: options?.loaded ?? 0 },
total: { configurable: true, value: options?.total ?? 0 },
});
return event as ProgressEvent;
}
}
/**
* Creates a `CustomEvent` instance based on the given options. If the
* `CustomEvent` constructor is not available, the generic `Event` constructor
* will be used instead, and the options will be attached to the event as its
* properties.
*/
export function createCustomEvent<T = any>(
type: string,
options: CustomEventInit<T> = {}
): CustomEvent<T> {
if (typeof CustomEvent === "function") {
return new CustomEvent(type, options);
} else {
const event = new Event(type, {
bubbles: options?.bubbles ?? false,
cancelable: options?.cancelable ?? false,
composed: options?.composed ?? false,
});
Object.defineProperties(event, {
detail: { configurable: true, value: options?.detail ?? null },
});
return event as CustomEvent<T>;
}
}