main
js 69 lines 1.64 KB
Raw
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 * @flow
8 */
9
10 /* globals Event$Init */
11
12 /**
13 * A bridge event class that extends the W3C Event interface and carries
14 * the native event payload. This is used as a compatibility layer during
15 * the migration from the legacy SyntheticEvent system to EventTarget-based
16 * dispatching.
17 */
18 export default class LegacySyntheticEvent extends Event {
19 _nativeEvent: {[string]: mixed};
20 _propagationStopped: boolean;
21
22 constructor(
23 type: string,
24 options: Event$Init,
25 nativeEvent: {[string]: mixed},
26 ) {
27 super(type, options);
28 this._nativeEvent = nativeEvent;
29 this._propagationStopped = false;
30 }
31
32 get nativeEvent(): {[string]: mixed} {
33 return this._nativeEvent;
34 }
35
36 stopPropagation(): void {
37 super.stopPropagation();
38 this._propagationStopped = true;
39 }
40
41 stopImmediatePropagation(): void {
42 super.stopImmediatePropagation();
43 this._propagationStopped = true;
44 }
45
46 /**
47 * No-op for backward compatibility. The legacy SyntheticEvent system
48 * used pooling which required calling persist() to keep the event.
49 * With EventTarget-based dispatching, events are never pooled.
50 */
51 persist(): void {
52 // No-op
53 }
54
55 /**
56 * Backward-compatible wrapper for `defaultPrevented`.
57 */
58 isDefaultPrevented(): boolean {
59 return this.defaultPrevented;
60 }
61
62 /**
63 * Backward-compatible wrapper. Returns true if stopPropagation()
64 * has been called.
65 */
66 isPropagationStopped(): boolean {
67 return this._propagationStopped;
68 }
69 }