1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

fix(cli): improve worker types (#10965)

This commit is contained in:
Kitson Kelly 2021-06-15 11:16:06 +10:00 committed by GitHub
parent 0acd0602bb
commit 1eac527adb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 142 additions and 71 deletions

View file

@ -324,19 +324,6 @@ interface VoidFunction {
*/ */
declare function queueMicrotask(func: VoidFunction): void; declare function queueMicrotask(func: VoidFunction): void;
/** Registers an event listener in the global scope, which will be called
* synchronously whenever the event `type` is dispatched.
*
* addEventListener('unload', () => { console.log('All finished!'); });
* ...
* dispatchEvent(new Event('unload'));
*/
declare function addEventListener(
type: string,
callback: EventListenerOrEventListenerObject | null,
options?: boolean | AddEventListenerOptions | undefined,
): void;
/** Dispatches an event in the global scope, synchronously invoking any /** Dispatches an event in the global scope, synchronously invoking any
* registered event listeners for this event in the appropriate order. Returns * registered event listeners for this event in the appropriate order. Returns
* false if event is cancelable and at least one of the event handlers which * false if event is cancelable and at least one of the event handlers which
@ -346,18 +333,6 @@ declare function addEventListener(
*/ */
declare function dispatchEvent(event: Event): boolean; declare function dispatchEvent(event: Event): boolean;
/** Remove a previously registered event listener from the global scope
*
* const lstnr = () => { console.log('hello'); };
* addEventListener('load', lstnr);
* removeEventListener('load', lstnr);
*/
declare function removeEventListener(
type: string,
callback: EventListenerOrEventListenerObject | null,
options?: boolean | EventListenerOptions | undefined,
): void;
interface DOMStringList { interface DOMStringList {
/** Returns the number of strings in strings. */ /** Returns the number of strings in strings. */
readonly length: number; readonly length: number;

View file

@ -67,6 +67,31 @@ declare function confirm(message?: string): boolean;
*/ */
declare function prompt(message?: string, defaultValue?: string): string | null; declare function prompt(message?: string, defaultValue?: string): string | null;
/** Registers an event listener in the global scope, which will be called
* synchronously whenever the event `type` is dispatched.
*
* addEventListener('unload', () => { console.log('All finished!'); });
* ...
* dispatchEvent(new Event('unload'));
*/
declare function addEventListener(
type: string,
callback: EventListenerOrEventListenerObject | null,
options?: boolean | AddEventListenerOptions | undefined,
): void;
/** Remove a previously registered event listener from the global scope
*
* const lstnr = () => { console.log('hello'); };
* addEventListener('load', lstnr);
* removeEventListener('load', lstnr);
*/
declare function removeEventListener(
type: string,
callback: EventListenerOrEventListenerObject | null,
options?: boolean | EventListenerOptions | undefined,
): void;
// TODO(nayeemrmn): Move this to `extensions/web` where its implementation is. // TODO(nayeemrmn): Move this to `extensions/web` where its implementation is.
// The types there must first be split into window, worker and global types. // The types there must first be split into window, worker and global types.
/** The location (URL) of the object it is linked to. Changes done on it are /** The location (URL) of the object it is linked to. Changes done on it are

View file

@ -6,34 +6,45 @@
/// <reference lib="deno.webgpu" /> /// <reference lib="deno.webgpu" />
/// <reference lib="esnext" /> /// <reference lib="esnext" />
declare class WorkerGlobalScope { interface WorkerGlobalScopeEventMap {
new(): WorkerGlobalScope; "error": ErrorEvent;
self: WorkerGlobalScope & typeof globalThis; }
onmessage:
| (( declare class WorkerGlobalScope extends EventTarget {
this: WorkerGlobalScope & typeof globalThis, readonly location: WorkerLocation;
ev: MessageEvent, readonly navigator: WorkerNavigator;
) => any) onerror: ((this: WorkerGlobalScope, ev: ErrorEvent) => any) | null;
| null;
onmessageerror: readonly self: WorkerGlobalScope & typeof globalThis;
| ((
this: WorkerGlobalScope & typeof globalThis, addEventListener<K extends keyof WorkerGlobalScopeEventMap>(
ev: MessageEvent, type: K,
) => any) listener: (
| null; this: WorkerGlobalScope,
onerror: ev: WorkerGlobalScopeEventMap[K],
| (( ) => any,
this: WorkerGlobalScope & typeof globalThis, options?: boolean | AddEventListenerOptions,
ev: ErrorEvent, ): void;
) => any) addEventListener(
| null; type: string,
close: () => void; listener: EventListenerOrEventListenerObject,
postMessage: (message: any) => void; options?: boolean | AddEventListenerOptions,
): void;
removeEventListener<K extends keyof WorkerGlobalScopeEventMap>(
type: K,
listener: (
this: WorkerGlobalScope,
ev: WorkerGlobalScopeEventMap[K],
) => any,
options?: boolean | EventListenerOptions,
): void;
removeEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | EventListenerOptions,
): void;
Deno: typeof Deno; Deno: typeof Deno;
WorkerNavigator: typeof WorkerNavigator;
navigator: WorkerNavigator;
WorkerLocation: typeof WorkerLocation;
location: WorkerLocation;
} }
declare class WorkerNavigator { declare class WorkerNavigator {
@ -43,33 +54,93 @@ declare class WorkerNavigator {
declare var navigator: WorkerNavigator; declare var navigator: WorkerNavigator;
declare class DedicatedWorkerGlobalScope extends WorkerGlobalScope { interface DedicatedWorkerGlobalScopeEventMap extends WorkerGlobalScopeEventMap {
new(): DedicatedWorkerGlobalScope; "message": MessageEvent;
name: string; "messageerror": MessageEvent;
} }
declare var self: WorkerGlobalScope & typeof globalThis; declare class DedicatedWorkerGlobalScope extends WorkerGlobalScope {
readonly name: string;
onmessage:
| ((this: DedicatedWorkerGlobalScope, ev: MessageEvent) => any)
| null;
onmessageerror:
| ((this: DedicatedWorkerGlobalScope, ev: MessageEvent) => any)
| null;
close(): void;
postMessage(message: any): void;
addEventListener<K extends keyof DedicatedWorkerGlobalScopeEventMap>(
type: K,
listener: (
this: DedicatedWorkerGlobalScope,
ev: DedicatedWorkerGlobalScopeEventMap[K],
) => any,
options?: boolean | AddEventListenerOptions,
): void;
addEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions,
): void;
removeEventListener<K extends keyof DedicatedWorkerGlobalScopeEventMap>(
type: K,
listener: (
this: DedicatedWorkerGlobalScope,
ev: DedicatedWorkerGlobalScopeEventMap[K],
) => any,
options?: boolean | EventListenerOptions,
): void;
removeEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | EventListenerOptions,
): void;
}
declare var name: string;
declare var onmessage: declare var onmessage:
| (( | ((this: DedicatedWorkerGlobalScope, ev: MessageEvent) => any)
this: WorkerGlobalScope & typeof globalThis,
ev: MessageEvent,
) => any)
| null; | null;
declare var onmessageerror: declare var onmessageerror:
| (( | ((this: DedicatedWorkerGlobalScope, ev: MessageEvent) => any)
this: WorkerGlobalScope & typeof globalThis,
ev: MessageEvent,
) => any)
| null; | null;
declare function close(): void;
declare function postMessage(message: any): void;
declare var navigator: WorkerNavigator;
declare var onerror: declare var onerror:
| (( | ((this: DedicatedWorkerGlobalScope, ev: ErrorEvent) => any)
this: WorkerGlobalScope & typeof globalThis,
ev: ErrorEvent,
) => any)
| null; | null;
declare var close: () => void; declare var self: WorkerGlobalScope & typeof globalThis;
declare var name: string; declare function addEventListener<
declare var postMessage: (message: any) => void; K extends keyof DedicatedWorkerGlobalScopeEventMap,
>(
type: K,
listener: (
this: DedicatedWorkerGlobalScope,
ev: DedicatedWorkerGlobalScopeEventMap[K],
) => any,
options?: boolean | AddEventListenerOptions,
): void;
declare function addEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions,
): void;
declare function removeEventListener<
K extends keyof DedicatedWorkerGlobalScopeEventMap,
>(
type: K,
listener: (
this: DedicatedWorkerGlobalScope,
ev: DedicatedWorkerGlobalScopeEventMap[K],
) => any,
options?: boolean | EventListenerOptions,
): void;
declare function removeEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | EventListenerOptions,
): void;
// TODO(nayeemrmn): Move this to `extensions/web` where its implementation is. // TODO(nayeemrmn): Move this to `extensions/web` where its implementation is.
// The types there must first be split into window, worker and global types. // The types there must first be split into window, worker and global types.