Commit caab9c3
fix(runtime): don't overwrite native Event/EventTarget on web (#122)
## Summary
`@react-native-harness/runtime`'s `initialize.ts` unconditionally
assigns `event-target-shim`'s classes to `globalThis.Event` and
`globalThis.EventTarget`. That polyfill is required on RN's
JavaScriptCore runtime (which has no DOM), but on web (RN Web running in
a real browser) it clobbers the browser's native ctors.
Safari's native `EventTarget.dispatchEvent()` does an internal brand
check on its argument and throws
> `TypeError: Argument 1 ('event') to EventTarget.dispatchEvent must be
an instance of Event`
when a polyfill instance is dispatched onto a native `EventTarget` —
even though the polyfill behaves mostly like `Event`. That's enough to
break any DOM-event-driven flow in the page.
## Fix
Only install the polyfill when a native ctor is not already present:
```ts
if (typeof globalThis.Event !== 'function') {
globalThis.Event = Shim.Event;
}
if (typeof globalThis.EventTarget !== 'function') {
globalThis.EventTarget = Shim.EventTarget;
}
```
- On RN/JSC: `typeof globalThis.Event !== 'function'`, polyfill installs
— unchanged behaviour.
- On RN Web / any browser: `Event` is a native ctor, polyfill is
skipped, native dispatch keeps working.
## Test plan
- [x] `pnpm nx run-many -t build typecheck lint -p
@react-native-harness/runtime` — green.
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: Szymon Chmal <szymon@chmal.it>1 parent 64a74d7 commit caab9c3
2 files changed
Lines changed: 17 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
7 | 12 | | |
8 | | - | |
9 | | - | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
10 | 19 | | |
11 | 20 | | |
12 | 21 | | |
| |||
0 commit comments