Skip to content

Commit 6124e74

Browse files
saschanazsandersn
andauthored
Add HTML Web workers types (#804)
* Support new constructor() syntax * Add HTML Web workers types * Fix onmessage event interface * restore Beacon Co-authored-by: Nathan Shively-Sanders <[email protected]>
1 parent 8d2979d commit 6124e74

9 files changed

+322
-51
lines changed

baselines/dom.generated.d.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10908,6 +10908,7 @@ interface Navigator extends MSFileSaver, MSNavigatorDoNotTrack, NavigatorAutomat
1090810908
getVRDisplays(): Promise<VRDisplay[]>;
1090910909
msLaunchUri(uri: string, successCallback?: MSLaunchUriCallback, noHandlerCallback?: MSLaunchUriCallback): void;
1091010910
requestMediaKeySystemAccess(keySystem: string, supportedConfigurations: MediaKeySystemConfiguration[]): Promise<MediaKeySystemAccess>;
10911+
sendBeacon(url: string, data?: BodyInit | null): boolean;
1091110912
vibrate(pattern: number | number[]): boolean;
1091210913
}
1091310914

@@ -15195,6 +15196,22 @@ declare var ShadowRoot: {
1519515196
new(): ShadowRoot;
1519615197
};
1519715198

15199+
interface SharedWorker extends EventTarget, AbstractWorker {
15200+
/**
15201+
* Returns sharedWorker's MessagePort object which can be used to communicate with the global environment.
15202+
*/
15203+
readonly port: MessagePort;
15204+
addEventListener<K extends keyof AbstractWorkerEventMap>(type: K, listener: (this: SharedWorker, ev: AbstractWorkerEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
15205+
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
15206+
removeEventListener<K extends keyof AbstractWorkerEventMap>(type: K, listener: (this: SharedWorker, ev: AbstractWorkerEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
15207+
removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
15208+
}
15209+
15210+
declare var SharedWorker: {
15211+
prototype: SharedWorker;
15212+
new(scriptURL: string, options?: string | WorkerOptions): SharedWorker;
15213+
};
15214+
1519815215
interface Slotable {
1519915216
readonly assignedSlot: HTMLSlotElement | null;
1520015217
}
@@ -18608,7 +18625,6 @@ interface Window extends EventTarget, AnimationFrameProvider, GlobalEventHandler
1860818625
readonly history: History;
1860918626
readonly innerHeight: number;
1861018627
readonly innerWidth: number;
18611-
readonly isSecureContext: boolean;
1861218628
readonly length: number;
1861318629
location: Location;
1861418630
readonly locationbar: BarProp;
@@ -18775,6 +18791,7 @@ interface WindowOrWorkerGlobalScope {
1877518791
readonly caches: CacheStorage;
1877618792
readonly crypto: Crypto;
1877718793
readonly indexedDB: IDBFactory;
18794+
readonly isSecureContext: boolean;
1877818795
readonly origin: string;
1877918796
readonly performance: Performance;
1878018797
atob(data: string): string;
@@ -18795,13 +18812,21 @@ interface WindowSessionStorage {
1879518812

1879618813
interface WorkerEventMap extends AbstractWorkerEventMap {
1879718814
"message": MessageEvent;
18815+
"messageerror": MessageEvent;
1879818816
}
1879918817

1880018818
/** This Web Workers API interface represents a background task that can be easily created and can send messages back to its creator. Creating a worker is as simple as calling the Worker() constructor and specifying a script to be run in the worker thread. */
1880118819
interface Worker extends EventTarget, AbstractWorker {
1880218820
onmessage: ((this: Worker, ev: MessageEvent) => any) | null;
18821+
onmessageerror: ((this: Worker, ev: MessageEvent) => any) | null;
18822+
/**
18823+
* Clones message and transmits it to worker's global environment. transfer can be passed as a list of objects that are to be transferred rather than cloned.
18824+
*/
1880318825
postMessage(message: any, transfer: Transferable[]): void;
1880418826
postMessage(message: any, options?: PostMessageOptions): void;
18827+
/**
18828+
* Aborts worker's associated global environment.
18829+
*/
1880518830
terminate(): void;
1880618831
addEventListener<K extends keyof WorkerEventMap>(type: K, listener: (this: Worker, ev: WorkerEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
1880718832
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
@@ -19598,7 +19623,6 @@ declare var frames: Window;
1959819623
declare var history: History;
1959919624
declare var innerHeight: number;
1960019625
declare var innerWidth: number;
19601-
declare var isSecureContext: boolean;
1960219626
declare var length: number;
1960319627
declare var location: Location;
1960419628
declare var locationbar: BarProp;
@@ -19977,6 +20001,7 @@ declare function requestAnimationFrame(callback: FrameRequestCallback): number;
1997720001
declare var caches: CacheStorage;
1997820002
declare var crypto: Crypto;
1997920003
declare var indexedDB: IDBFactory;
20004+
declare var isSecureContext: boolean;
1998020005
declare var origin: string;
1998120006
declare var performance: Performance;
1998220007
declare function atob(data: string): string;

baselines/webworker.generated.d.ts

Lines changed: 124 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,12 +1273,24 @@ declare var DOMStringList: {
12731273

12741274
interface DedicatedWorkerGlobalScopeEventMap extends WorkerGlobalScopeEventMap {
12751275
"message": MessageEvent;
1276+
"messageerror": MessageEvent;
12761277
}
12771278

12781279
/** (the Worker global scope) is accessible through the self keyword. Some additional global functions, namespaces objects, and constructors, not typically associated with the worker global scope, but available on it, are listed in the JavaScript Reference. See also: Functions available to workers. */
12791280
interface DedicatedWorkerGlobalScope extends WorkerGlobalScope, AnimationFrameProvider {
1281+
/**
1282+
* Returns dedicatedWorkerGlobal's name, i.e. the value given to the Worker constructor. Primarily useful for debugging.
1283+
*/
1284+
readonly name: string;
12801285
onmessage: ((this: DedicatedWorkerGlobalScope, ev: MessageEvent) => any) | null;
1286+
onmessageerror: ((this: DedicatedWorkerGlobalScope, ev: MessageEvent) => any) | null;
1287+
/**
1288+
* Aborts dedicatedWorkerGlobal.
1289+
*/
12811290
close(): void;
1291+
/**
1292+
* Clones message and transmits it to the Worker object associated with dedicatedWorkerGlobal. transfer can be passed as a list of objects that are to be transferred rather than cloned.
1293+
*/
12821294
postMessage(message: any, transfer: Transferable[]): void;
12831295
postMessage(message: any, options?: PostMessageOptions): void;
12841296
addEventListener<K extends keyof DedicatedWorkerGlobalScopeEventMap>(type: K, listener: (this: DedicatedWorkerGlobalScope, ev: DedicatedWorkerGlobalScopeEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
@@ -2318,10 +2330,6 @@ declare var NavigationPreloadManager: {
23182330
new(): NavigationPreloadManager;
23192331
};
23202332

2321-
interface NavigatorBeacon {
2322-
sendBeacon(url: string, data?: Blob | Int8Array | Int16Array | Int32Array | Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray | Float32Array | Float64Array | DataView | ArrayBuffer | FormData | string | null): boolean;
2323-
}
2324-
23252333
interface NavigatorConcurrentHardware {
23262334
readonly hardwareConcurrency: number;
23272335
}
@@ -2335,6 +2343,11 @@ interface NavigatorID {
23352343
readonly userAgent: string;
23362344
}
23372345

2346+
interface NavigatorLanguage {
2347+
readonly language: string;
2348+
readonly languages: ReadonlyArray<string>;
2349+
}
2350+
23382351
interface NavigatorOnLine {
23392352
readonly onLine: boolean;
23402353
}
@@ -3001,6 +3014,47 @@ declare var ServiceWorkerRegistration: {
30013014
new(): ServiceWorkerRegistration;
30023015
};
30033016

3017+
interface SharedWorker extends EventTarget, AbstractWorker {
3018+
/**
3019+
* Returns sharedWorker's MessagePort object which can be used to communicate with the global environment.
3020+
*/
3021+
readonly port: MessagePort;
3022+
addEventListener<K extends keyof AbstractWorkerEventMap>(type: K, listener: (this: SharedWorker, ev: AbstractWorkerEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
3023+
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
3024+
removeEventListener<K extends keyof AbstractWorkerEventMap>(type: K, listener: (this: SharedWorker, ev: AbstractWorkerEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
3025+
removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
3026+
}
3027+
3028+
declare var SharedWorker: {
3029+
prototype: SharedWorker;
3030+
new(scriptURL: string, options?: string | WorkerOptions): SharedWorker;
3031+
};
3032+
3033+
interface SharedWorkerGlobalScopeEventMap extends WorkerGlobalScopeEventMap {
3034+
"connect": MessageEvent;
3035+
}
3036+
3037+
interface SharedWorkerGlobalScope extends WorkerGlobalScope {
3038+
/**
3039+
* Returns sharedWorkerGlobal's name, i.e. the value given to the SharedWorker constructor. Multiple SharedWorker objects can correspond to the same shared worker (and SharedWorkerGlobalScope), by reusing the same name.
3040+
*/
3041+
readonly name: string;
3042+
onconnect: ((this: SharedWorkerGlobalScope, ev: MessageEvent) => any) | null;
3043+
/**
3044+
* Aborts sharedWorkerGlobal.
3045+
*/
3046+
close(): void;
3047+
addEventListener<K extends keyof SharedWorkerGlobalScopeEventMap>(type: K, listener: (this: SharedWorkerGlobalScope, ev: SharedWorkerGlobalScopeEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
3048+
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
3049+
removeEventListener<K extends keyof SharedWorkerGlobalScopeEventMap>(type: K, listener: (this: SharedWorkerGlobalScope, ev: SharedWorkerGlobalScopeEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
3050+
removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
3051+
}
3052+
3053+
declare var SharedWorkerGlobalScope: {
3054+
prototype: SharedWorkerGlobalScope;
3055+
new(): SharedWorkerGlobalScope;
3056+
};
3057+
30043058
interface StorageManager {
30053059
estimate(): Promise<StorageEstimate>;
30063060
persisted(): Promise<boolean>;
@@ -5342,11 +5396,6 @@ declare var WebSocket: {
53425396
readonly OPEN: number;
53435397
};
53445398

5345-
interface WindowBase64 {
5346-
atob(encodedString: string): string;
5347-
btoa(rawString: string): string;
5348-
}
5349-
53505399
/** This ServiceWorker API interface represents the scope of a service worker client that is a document in a browser context, controlled by an active worker. The service worker client independently selects and uses a service worker for its own loading and sub-resources. */
53515400
interface WindowClient extends Client {
53525401
readonly ancestorOrigins: ReadonlyArray<string>;
@@ -5369,6 +5418,7 @@ interface WindowOrWorkerGlobalScope {
53695418
readonly caches: CacheStorage;
53705419
readonly crypto: Crypto;
53715420
readonly indexedDB: IDBFactory;
5421+
readonly isSecureContext: boolean;
53725422
readonly origin: string;
53735423
readonly performance: Performance;
53745424
atob(data: string): string;
@@ -5385,13 +5435,21 @@ interface WindowOrWorkerGlobalScope {
53855435

53865436
interface WorkerEventMap extends AbstractWorkerEventMap {
53875437
"message": MessageEvent;
5438+
"messageerror": MessageEvent;
53885439
}
53895440

53905441
/** This Web Workers API interface represents a background task that can be easily created and can send messages back to its creator. Creating a worker is as simple as calling the Worker() constructor and specifying a script to be run in the worker thread. */
53915442
interface Worker extends EventTarget, AbstractWorker {
53925443
onmessage: ((this: Worker, ev: MessageEvent) => any) | null;
5444+
onmessageerror: ((this: Worker, ev: MessageEvent) => any) | null;
5445+
/**
5446+
* Clones message and transmits it to worker's global environment. transfer can be passed as a list of objects that are to be transferred rather than cloned.
5447+
*/
53935448
postMessage(message: any, transfer: Transferable[]): void;
53945449
postMessage(message: any, options?: PostMessageOptions): void;
5450+
/**
5451+
* Aborts worker's associated global environment.
5452+
*/
53955453
terminate(): void;
53965454
addEventListener<K extends keyof WorkerEventMap>(type: K, listener: (this: Worker, ev: WorkerEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
53975455
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
@@ -5406,17 +5464,34 @@ declare var Worker: {
54065464

54075465
interface WorkerGlobalScopeEventMap {
54085466
"error": ErrorEvent;
5467+
"languagechange": Event;
5468+
"offline": Event;
5469+
"online": Event;
5470+
"rejectionhandled": PromiseRejectionEvent;
5471+
"unhandledrejection": PromiseRejectionEvent;
54095472
}
54105473

54115474
/** This Web Workers API interface is an interface representing the scope of any worker. Workers have no browsing context; this scope contains the information usually conveyed by Window objects — in this case event handlers, the console or the associated WorkerNavigator object. Each WorkerGlobalScope has its own event loop. */
5412-
interface WorkerGlobalScope extends EventTarget, WindowConsole, WindowOrWorkerGlobalScope, WorkerUtils {
5413-
readonly caches: CacheStorage;
5414-
readonly isSecureContext: boolean;
5475+
interface WorkerGlobalScope extends EventTarget, WindowConsole, WindowOrWorkerGlobalScope {
5476+
/**
5477+
* Returns workerGlobal's WorkerLocation object.
5478+
*/
54155479
readonly location: WorkerLocation;
5480+
readonly navigator: WorkerNavigator;
54165481
onerror: ((this: WorkerGlobalScope, ev: ErrorEvent) => any) | null;
5417-
readonly performance: Performance;
5482+
onlanguagechange: ((this: WorkerGlobalScope, ev: Event) => any) | null;
5483+
onoffline: ((this: WorkerGlobalScope, ev: Event) => any) | null;
5484+
ononline: ((this: WorkerGlobalScope, ev: Event) => any) | null;
5485+
onrejectionhandled: ((this: WorkerGlobalScope, ev: PromiseRejectionEvent) => any) | null;
5486+
onunhandledrejection: ((this: WorkerGlobalScope, ev: PromiseRejectionEvent) => any) | null;
5487+
/**
5488+
* Returns workerGlobal.
5489+
*/
54185490
readonly self: WorkerGlobalScope & typeof globalThis;
5419-
msWriteProfilerMark(profilerMarkName: string): void;
5491+
/**
5492+
* Fetches each URL in urls, executes them one-by-one in the order they are passed, and then returns (or throws if something went amiss).
5493+
*/
5494+
importScripts(...urls: string[]): void;
54205495
addEventListener<K extends keyof WorkerGlobalScopeEventMap>(type: K, listener: (this: WorkerGlobalScope, ev: WorkerGlobalScopeEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
54215496
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
54225497
removeEventListener<K extends keyof WorkerGlobalScopeEventMap>(type: K, listener: (this: WorkerGlobalScope, ev: WorkerGlobalScopeEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
@@ -5448,7 +5523,7 @@ declare var WorkerLocation: {
54485523
};
54495524

54505525
/** A subset of the Navigator interface allowed to be accessed from a Worker. Such an object is initialized for each worker and is available via the WorkerGlobalScope.navigator property obtained by calling window.self.navigator. */
5451-
interface WorkerNavigator extends NavigatorBeacon, NavigatorConcurrentHardware, NavigatorID, NavigatorOnLine, NavigatorStorage {
5526+
interface WorkerNavigator extends NavigatorConcurrentHardware, NavigatorID, NavigatorLanguage, NavigatorOnLine, NavigatorStorage {
54525527
readonly permissions: Permissions;
54535528
readonly serviceWorker: ServiceWorkerContainer;
54545529
}
@@ -5458,13 +5533,6 @@ declare var WorkerNavigator: {
54585533
new(): WorkerNavigator;
54595534
};
54605535

5461-
interface WorkerUtils extends WindowBase64 {
5462-
readonly indexedDB: IDBFactory;
5463-
readonly msIndexedDB: IDBFactory;
5464-
readonly navigator: WorkerNavigator;
5465-
importScripts(...urls: string[]): void;
5466-
}
5467-
54685536
/** This Streams API interface provides a standard abstraction for writing streaming data to a destination, known as a sink. This object comes with built-in backpressure and queuing. */
54695537
interface WritableStream<W = any> {
54705538
readonly locked: boolean;
@@ -5747,14 +5815,14 @@ declare namespace WebAssembly {
57475815
function validate(bytes: BufferSource): boolean;
57485816
}
57495817

5750-
interface EventHandlerNonNull {
5751-
(event: Event): any;
5752-
}
5753-
57545818
interface FrameRequestCallback {
57555819
(time: number): void;
57565820
}
57575821

5822+
interface OnErrorEventHandlerNonNull {
5823+
(event: Event | string, source?: string, lineno?: number, colno?: number, error?: Error): any;
5824+
}
5825+
57585826
interface PerformanceObserverCallback {
57595827
(entries: PerformanceObserverEntryList, observer: PerformanceObserver): void;
57605828
}
@@ -5803,35 +5871,52 @@ interface WritableStreamErrorCallback {
58035871
(reason: any): void | PromiseLike<void>;
58045872
}
58055873

5874+
/**
5875+
* Returns dedicatedWorkerGlobal's name, i.e. the value given to the Worker constructor. Primarily useful for debugging.
5876+
*/
5877+
declare var name: string;
58065878
declare var onmessage: ((this: DedicatedWorkerGlobalScope, ev: MessageEvent) => any) | null;
5879+
declare var onmessageerror: ((this: DedicatedWorkerGlobalScope, ev: MessageEvent) => any) | null;
5880+
/**
5881+
* Aborts dedicatedWorkerGlobal.
5882+
*/
58075883
declare function close(): void;
5884+
/**
5885+
* Clones message and transmits it to the Worker object associated with dedicatedWorkerGlobal. transfer can be passed as a list of objects that are to be transferred rather than cloned.
5886+
*/
58085887
declare function postMessage(message: any, transfer: Transferable[]): void;
58095888
declare function postMessage(message: any, options?: PostMessageOptions): void;
58105889
/**
58115890
* 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.
58125891
*/
58135892
declare function dispatchEvent(event: Event): boolean;
5814-
declare var caches: CacheStorage;
5815-
declare var isSecureContext: boolean;
5893+
/**
5894+
* Returns workerGlobal's WorkerLocation object.
5895+
*/
58165896
declare var location: WorkerLocation;
5897+
declare var navigator: WorkerNavigator;
58175898
declare var onerror: ((this: DedicatedWorkerGlobalScope, ev: ErrorEvent) => any) | null;
5818-
declare var performance: Performance;
5899+
declare var onlanguagechange: ((this: DedicatedWorkerGlobalScope, ev: Event) => any) | null;
5900+
declare var onoffline: ((this: DedicatedWorkerGlobalScope, ev: Event) => any) | null;
5901+
declare var ononline: ((this: DedicatedWorkerGlobalScope, ev: Event) => any) | null;
5902+
declare var onrejectionhandled: ((this: DedicatedWorkerGlobalScope, ev: PromiseRejectionEvent) => any) | null;
5903+
declare var onunhandledrejection: ((this: DedicatedWorkerGlobalScope, ev: PromiseRejectionEvent) => any) | null;
5904+
/**
5905+
* Returns workerGlobal.
5906+
*/
58195907
declare var self: WorkerGlobalScope & typeof globalThis;
5820-
declare function msWriteProfilerMark(profilerMarkName: string): void;
5908+
/**
5909+
* Fetches each URL in urls, executes them one-by-one in the order they are passed, and then returns (or throws if something went amiss).
5910+
*/
5911+
declare function importScripts(...urls: string[]): void;
58215912
/**
58225913
* 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.
58235914
*/
58245915
declare function dispatchEvent(event: Event): boolean;
5825-
declare var indexedDB: IDBFactory;
5826-
declare var msIndexedDB: IDBFactory;
5827-
declare var navigator: WorkerNavigator;
5828-
declare function importScripts(...urls: string[]): void;
5829-
declare function atob(encodedString: string): string;
5830-
declare function btoa(rawString: string): string;
5831-
declare var console: Console;
58325916
declare var caches: CacheStorage;
58335917
declare var crypto: Crypto;
58345918
declare var indexedDB: IDBFactory;
5919+
declare var isSecureContext: boolean;
58355920
declare var origin: string;
58365921
declare var performance: Performance;
58375922
declare function atob(data: string): string;
@@ -5844,6 +5929,7 @@ declare function fetch(input: RequestInfo, init?: RequestInit): Promise<Response
58445929
declare function queueMicrotask(callback: VoidFunction): void;
58455930
declare function setInterval(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;
58465931
declare function setTimeout(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;
5932+
declare var console: Console;
58475933
declare function cancelAnimationFrame(handle: number): void;
58485934
declare function requestAnimationFrame(callback: FrameRequestCallback): number;
58495935
declare function addEventListener<K extends keyof DedicatedWorkerGlobalScopeEventMap>(type: K, listener: (this: DedicatedWorkerGlobalScope, ev: DedicatedWorkerGlobalScopeEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
@@ -5859,6 +5945,7 @@ type CanvasImageSource = ImageBitmap | OffscreenCanvas;
58595945
type OffscreenRenderingContext = OffscreenCanvasRenderingContext2D | ImageBitmapRenderingContext | WebGLRenderingContext | WebGL2RenderingContext;
58605946
type MessageEventSource = MessagePort | ServiceWorker;
58615947
type ImageBitmapSource = CanvasImageSource | Blob | ImageData;
5948+
type OnErrorEventHandler = OnErrorEventHandlerNonNull | null;
58625949
type TimerHandler = string | Function;
58635950
type PerformanceEntryList = PerformanceEntry[];
58645951
type PushMessageDataInit = BufferSource | string;

0 commit comments

Comments
 (0)