Skip to content

Commit ef0de0e

Browse files
coadofacebook-github-bot
authored andcommitted
Move event-target-shim types to react-native package (#50013)
Summary: Pull Request resolved: #50013 The diff incorporates event-target-shim types that will be referenced by generated TS types to make sure that the public API does not change unexpectedly. The generated tsconfig contains path property which tells Typescript to use copied types instead of the downloaded event-target-shims types. Changelog: [internal] - Moved event-target-shim types to react-native package. Reviewed By: huntie Differential Revision: D71044389 fbshipit-source-id: b2837cc880a2161f7ff716470fd6eb4644d514e2
1 parent 934c446 commit ef0de0e

File tree

3 files changed

+402
-0
lines changed

3 files changed

+402
-0
lines changed
Lines changed: 392 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,392 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
*/
9+
10+
export as namespace EventTargetShim;
11+
12+
/**
13+
* `Event` interface.
14+
* @see https://dom.spec.whatwg.org/#event
15+
*/
16+
export interface Event {
17+
/**
18+
* The type of this event.
19+
*/
20+
readonly type: string;
21+
22+
/**
23+
* The target of this event.
24+
*/
25+
readonly target: EventTarget<{}, {}, 'standard'> | null;
26+
27+
/**
28+
* The current target of this event.
29+
*/
30+
readonly currentTarget: EventTarget<{}, {}, 'standard'> | null;
31+
32+
/**
33+
* The target of this event.
34+
* @deprecated
35+
*/
36+
readonly srcElement: any | null;
37+
38+
/**
39+
* The composed path of this event.
40+
*/
41+
composedPath(): EventTarget<{}, {}, 'standard'>[];
42+
43+
/**
44+
* Constant of NONE.
45+
*/
46+
readonly NONE: number;
47+
48+
/**
49+
* Constant of CAPTURING_PHASE.
50+
*/
51+
readonly CAPTURING_PHASE: number;
52+
53+
/**
54+
* Constant of BUBBLING_PHASE.
55+
*/
56+
readonly BUBBLING_PHASE: number;
57+
58+
/**
59+
* Constant of AT_TARGET.
60+
*/
61+
readonly AT_TARGET: number;
62+
63+
/**
64+
* Indicates which phase of the event flow is currently being evaluated.
65+
*/
66+
readonly eventPhase: number;
67+
68+
/**
69+
* Stop event bubbling.
70+
*/
71+
stopPropagation(): void;
72+
73+
/**
74+
* Stop event bubbling.
75+
*/
76+
stopImmediatePropagation(): void;
77+
78+
/**
79+
* Initialize event.
80+
* @deprecated
81+
*/
82+
initEvent(type: string, bubbles?: boolean, cancelable?: boolean): void;
83+
84+
/**
85+
* The flag indicating bubbling.
86+
*/
87+
readonly bubbles: boolean;
88+
89+
/**
90+
* Stop event bubbling.
91+
* @deprecated
92+
*/
93+
cancelBubble: boolean;
94+
95+
/**
96+
* Set or get cancellation flag.
97+
* @deprecated
98+
*/
99+
returnValue: boolean;
100+
101+
/**
102+
* The flag indicating whether the event can be canceled.
103+
*/
104+
readonly cancelable: boolean;
105+
106+
/**
107+
* Cancel this event.
108+
*/
109+
preventDefault(): void;
110+
111+
/**
112+
* The flag to indicating whether the event was canceled.
113+
*/
114+
readonly defaultPrevented: boolean;
115+
116+
/**
117+
* The flag to indicating if event is composed.
118+
*/
119+
readonly composed: boolean;
120+
121+
/**
122+
* Indicates whether the event was dispatched by the user agent.
123+
*/
124+
readonly isTrusted: boolean;
125+
126+
/**
127+
* The unix time of this event.
128+
*/
129+
readonly timeStamp: number;
130+
}
131+
132+
/**
133+
* The constructor of `EventTarget` interface.
134+
*/
135+
export type EventTargetConstructor<
136+
TEvents extends EventTarget.EventDefinition = {},
137+
TEventAttributes extends EventTarget.EventDefinition = {},
138+
TMode extends EventTarget.Mode = 'loose',
139+
> = {
140+
prototype: EventTarget<TEvents, TEventAttributes, TMode>;
141+
new (): EventTarget<TEvents, TEventAttributes, TMode>;
142+
};
143+
144+
/**
145+
* `EventTarget` interface.
146+
* @see https://dom.spec.whatwg.org/#interface-eventtarget
147+
*/
148+
export type EventTarget<
149+
TEvents extends EventTarget.EventDefinition = {},
150+
TEventAttributes extends EventTarget.EventDefinition = {},
151+
TMode extends EventTarget.Mode = 'loose',
152+
> = EventTarget.EventAttributes<TEventAttributes> & {
153+
/**
154+
* Add a given listener to this event target.
155+
* @param eventName The event name to add.
156+
* @param listener The listener to add.
157+
* @param options The options for this listener.
158+
*/
159+
addEventListener<TEventType extends EventTarget.EventType<TEvents, TMode>>(
160+
type: TEventType,
161+
listener: EventTarget.Listener<
162+
EventTarget.PickEvent<TEvents, TEventType>
163+
> | null,
164+
options?: boolean | EventTarget.AddOptions,
165+
): void;
166+
167+
/**
168+
* Remove a given listener from this event target.
169+
* @param eventName The event name to remove.
170+
* @param listener The listener to remove.
171+
* @param options The options for this listener.
172+
*/
173+
removeEventListener<TEventType extends EventTarget.EventType<TEvents, TMode>>(
174+
type: TEventType,
175+
listener: EventTarget.Listener<
176+
EventTarget.PickEvent<TEvents, TEventType>
177+
> | null,
178+
options?: boolean | EventTarget.RemoveOptions,
179+
): void;
180+
181+
/**
182+
* Dispatch a given event.
183+
* @param event The event to dispatch.
184+
* @returns `false` if canceled.
185+
*/
186+
dispatchEvent<TEventType extends EventTarget.EventType<TEvents, TMode>>(
187+
event: EventTarget.EventData<TEvents, TEventType, TMode>,
188+
): boolean;
189+
};
190+
191+
export const EventTarget: EventTargetConstructor & {
192+
/**
193+
* Create an `EventTarget` instance with detailed event definition.
194+
*
195+
* The detailed event definition requires to use `defineEventAttribute()`
196+
* function later.
197+
*
198+
* Unfortunately, the second type parameter `TEventAttributes` was needed
199+
* because we cannot compute string literal types.
200+
*
201+
* @example
202+
* const signal = new EventTarget<{ abort: Event }, { onabort: Event }>()
203+
* defineEventAttribute(signal, "abort")
204+
*/
205+
new <
206+
TEvents extends EventTarget.EventDefinition,
207+
TEventAttributes extends EventTarget.EventDefinition,
208+
TMode extends EventTarget.Mode = 'loose',
209+
>(): EventTarget<TEvents, TEventAttributes, TMode>;
210+
211+
/**
212+
* Define an `EventTarget` constructor with attribute events and detailed event definition.
213+
*
214+
* Unfortunately, the second type parameter `TEventAttributes` was needed
215+
* because we cannot compute string literal types.
216+
*
217+
* @example
218+
* class AbortSignal extends EventTarget<{ abort: Event }, { onabort: Event }>("abort") {
219+
* abort(): void {}
220+
* }
221+
*
222+
* @param events Optional event attributes (e.g. passing in `"click"` adds `onclick` to prototype).
223+
*/
224+
<
225+
TEvents extends EventTarget.EventDefinition = {},
226+
TEventAttributes extends EventTarget.EventDefinition = {},
227+
TMode extends EventTarget.Mode = 'loose',
228+
>(
229+
events: string[],
230+
): EventTargetConstructor<TEvents, TEventAttributes, TMode>;
231+
232+
/**
233+
* Define an `EventTarget` constructor with attribute events and detailed event definition.
234+
*
235+
* Unfortunately, the second type parameter `TEventAttributes` was needed
236+
* because we cannot compute string literal types.
237+
*
238+
* @example
239+
* class AbortSignal extends EventTarget<{ abort: Event }, { onabort: Event }>("abort") {
240+
* abort(): void {}
241+
* }
242+
*
243+
* @param events Optional event attributes (e.g. passing in `"click"` adds `onclick` to prototype).
244+
*/
245+
<
246+
TEvents extends EventTarget.EventDefinition = {},
247+
TEventAttributes extends EventTarget.EventDefinition = {},
248+
TMode extends EventTarget.Mode = 'loose',
249+
>(
250+
event0: string,
251+
...events: string[]
252+
): EventTargetConstructor<TEvents, TEventAttributes, TMode>;
253+
};
254+
255+
export namespace EventTarget {
256+
/**
257+
* Options of `removeEventListener()` method.
258+
*/
259+
export interface RemoveOptions {
260+
/**
261+
* The flag to indicate that the listener is for the capturing phase.
262+
*/
263+
capture?: boolean | undefined;
264+
}
265+
266+
/**
267+
* Options of `addEventListener()` method.
268+
*/
269+
export interface AddOptions extends RemoveOptions {
270+
/**
271+
* The flag to indicate that the listener doesn't support
272+
* `event.preventDefault()` operation.
273+
*/
274+
passive?: boolean | undefined;
275+
/**
276+
* The flag to indicate that the listener will be removed on the first
277+
* event.
278+
*/
279+
once?: boolean | undefined;
280+
}
281+
282+
/**
283+
* The type of regular listeners.
284+
*/
285+
export interface FunctionListener<TEvent> {
286+
(event: TEvent): void;
287+
}
288+
289+
/**
290+
* The type of object listeners.
291+
*/
292+
export interface ObjectListener<TEvent> {
293+
handleEvent(event: TEvent): void;
294+
}
295+
296+
/**
297+
* The type of listeners.
298+
*/
299+
export type Listener<TEvent> =
300+
| FunctionListener<TEvent>
301+
| ObjectListener<TEvent>;
302+
303+
/**
304+
* Event definition.
305+
*/
306+
export type EventDefinition = {
307+
readonly [key: string]: Event;
308+
};
309+
310+
/**
311+
* Mapped type for event attributes.
312+
*/
313+
export type EventAttributes<TEventAttributes extends EventDefinition> = {
314+
[P in keyof TEventAttributes]: FunctionListener<TEventAttributes[P]> | null;
315+
};
316+
317+
/**
318+
* The type of event data for `dispatchEvent()` method.
319+
*/
320+
export type EventData<
321+
TEvents extends EventDefinition,
322+
TEventType extends keyof TEvents | string,
323+
TMode extends Mode,
324+
> = TEventType extends keyof TEvents
325+
? // Require properties which are not generated automatically.
326+
Pick<
327+
TEvents[TEventType],
328+
Exclude<keyof TEvents[TEventType], OmittableEventKeys>
329+
> &
330+
// Properties which are generated automatically are optional.
331+
Partial<Pick<Event, OmittableEventKeys>>
332+
: TMode extends 'standard'
333+
? Event
334+
: Event | NonStandardEvent;
335+
336+
/**
337+
* The string literal types of the properties which are generated
338+
* automatically in `dispatchEvent()` method.
339+
*/
340+
export type OmittableEventKeys = Exclude<keyof Event, 'type'>;
341+
342+
/**
343+
* The type of event data.
344+
*/
345+
export type NonStandardEvent = {
346+
[key: string]: any;
347+
type: string;
348+
};
349+
350+
/**
351+
* The type of listeners.
352+
*/
353+
export type PickEvent<
354+
TEvents extends EventDefinition,
355+
TEventType extends keyof TEvents | string,
356+
> = TEventType extends keyof TEvents ? TEvents[TEventType] : Event;
357+
358+
/**
359+
* Event type candidates.
360+
*/
361+
export type EventType<
362+
TEvents extends EventDefinition,
363+
TMode extends Mode,
364+
> = TMode extends 'strict' ? keyof TEvents : keyof TEvents | string;
365+
366+
/**
367+
* - `"strict"` ..... Methods don't accept unknown events.
368+
* `dispatchEvent()` accepts partial objects.
369+
* - `"loose"` ...... Methods accept unknown events.
370+
* `dispatchEvent()` accepts partial objects.
371+
* - `"standard"` ... Methods accept unknown events.
372+
* `dispatchEvent()` doesn't accept partial objects.
373+
*/
374+
export type Mode = 'strict' | 'standard' | 'loose';
375+
}
376+
377+
/**
378+
* Specialized `type` property.
379+
*/
380+
export type Type<T extends string> = {type: T};
381+
382+
/**
383+
* Define an event attribute (e.g. `eventTarget.onclick`).
384+
* @param prototype The event target prototype to define an event attribute.
385+
* @param eventName The event name to define.
386+
*/
387+
export function defineEventAttribute(
388+
prototype: EventTarget,
389+
eventName: string,
390+
): void;
391+
392+
export default EventTarget;

0 commit comments

Comments
 (0)