Skip to content

Commit

Permalink
Add type definitions for EventTarget interface on globalThis
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Oct 21, 2023
1 parent a3b7ab6 commit e0f2439
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-bears-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nxjs-runtime': patch
---

Add type definitions for `EventTarget` interface on `globalThis`
55 changes: 52 additions & 3 deletions packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ import {
} from './timers';
import { console } from './console';
import {
type Event,
ErrorEvent,
KeyboardEvent,
TouchEvent,
UIEvent,
PromiseRejectionEvent,
} from './polyfills/event';
import type {
AddEventListenerOptions,
EventListenerOptions,
} from './polyfills/event-target';

export type {
SwitchClass,
Expand All @@ -37,7 +42,7 @@ export type {
} from './canvas';
export type * from './canvas/image-data';
export type { Path2D } from './canvas/path2d';
export type { EventTarget } from './polyfills/event-target';
export type * from './polyfills/event-target';
export type { URL, URLSearchParams } from './polyfills/url';
export type * from './polyfills/streams';
export type * from './polyfills/event';
Expand Down Expand Up @@ -117,8 +122,6 @@ function touchIsEqual(a: Touch, b: Touch) {
);
}

const btnPlus = 1 << 10; ///< Plus button

// Make `globalThis` inherit from `EventTarget`
Object.setPrototypeOf(globalThis, EventTarget.prototype);
EventTarget.call(globalThis);
Expand All @@ -132,6 +135,50 @@ def(
);
def('dispatchEvent', EventTarget.prototype.dispatchEvent.bind(globalThis));

export declare function addEventListener(
type: 'error',
callback: (ev: ErrorEvent) => any,
options?: AddEventListenerOptions | boolean
): void;
export declare function addEventListener(
type: 'unhandledrejection',
callback: (ev: PromiseRejectionEvent) => any,
options?: AddEventListenerOptions | boolean
): void;
export declare function addEventListener(
type: string,
callback: EventListenerOrEventListenerObject | null,
options?: AddEventListenerOptions | boolean
): void;

/**
* Removes the event listener in target's event listener list with the same type, callback, and options.
*
* @see {@link https://developer.mozilla.org/docs/Web/API/EventTarget/removeEventListener | MDN Reference}
*/
export declare function removeEventListener(
type: 'error',
callback: (ev: ErrorEvent) => any,
options?: EventListenerOptions | boolean
): void;
export declare function removeEventListener(
type: 'unhandledrejection',
callback: (ev: PromiseRejectionEvent) => any,
options?: EventListenerOptions | boolean
): void;
export declare function removeEventListener(
type: string,
callback: EventListenerOrEventListenerObject | null,
options?: EventListenerOptions | boolean
): void;

/**
* Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.
*
* @see {@link https://developer.mozilla.org/docs/Web/API/EventTarget/dispatchEvent | MDN Reference}
*/
export declare function dispatchEvent(event: Event): boolean;

$.onError((e) => {
const ev = new ErrorEvent('error', {
error: e,
Expand All @@ -155,6 +202,8 @@ $.onUnhandledRejection((p, r) => {
return 1;
});

const btnPlus = 1 << 10; ///< Plus button

Switch.addEventListener('frame', (event) => {
const {
keyboardInitialized,
Expand Down

0 comments on commit e0f2439

Please sign in to comment.