Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): improve worker types #10965

Merged
merged 2 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 0 additions & 25 deletions cli/dts/lib.deno.shared_globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,19 +324,6 @@ interface VoidFunction {
*/
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
* 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
Expand All @@ -346,18 +333,6 @@ declare function addEventListener(
*/
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 {
/** Returns the number of strings in strings. */
readonly length: number;
Expand Down
25 changes: 25 additions & 0 deletions cli/dts/lib.deno.window.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,31 @@ declare function confirm(message?: string): boolean;
*/
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.
// 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
Expand Down
161 changes: 116 additions & 45 deletions cli/dts/lib.deno.worker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,45 @@
/// <reference lib="deno.webgpu" />
/// <reference lib="esnext" />

declare class WorkerGlobalScope {
new(): WorkerGlobalScope;
self: WorkerGlobalScope & typeof globalThis;
onmessage:
| ((
this: WorkerGlobalScope & typeof globalThis,
ev: MessageEvent,
) => any)
| null;
onmessageerror:
| ((
this: WorkerGlobalScope & typeof globalThis,
ev: MessageEvent,
) => any)
| null;
onerror:
| ((
this: WorkerGlobalScope & typeof globalThis,
ev: ErrorEvent,
) => any)
| null;
close: () => void;
postMessage: (message: any) => void;
interface WorkerGlobalScopeEventMap {
"error": ErrorEvent;
}

declare class WorkerGlobalScope extends EventTarget {
readonly location: WorkerLocation;
readonly navigator: WorkerNavigator;
onerror: ((this: WorkerGlobalScope, ev: ErrorEvent) => any) | null;

readonly self: WorkerGlobalScope & typeof globalThis;

addEventListener<K extends keyof WorkerGlobalScopeEventMap>(
type: K,
listener: (
this: WorkerGlobalScope,
ev: WorkerGlobalScopeEventMap[K],
) => any,
options?: boolean | AddEventListenerOptions,
): void;
addEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
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;
WorkerNavigator: typeof WorkerNavigator;
navigator: WorkerNavigator;
WorkerLocation: typeof WorkerLocation;
location: WorkerLocation;
}

declare class WorkerNavigator {
Expand All @@ -43,33 +54,93 @@ declare class WorkerNavigator {

declare var navigator: WorkerNavigator;

interface DedicatedWorkerGlobalScopeEventMap extends WorkerGlobalScope {
kitsonk marked this conversation as resolved.
Show resolved Hide resolved
"message": MessageEvent;
"messageerror": MessageEvent;
}

declare class DedicatedWorkerGlobalScope extends WorkerGlobalScope {
new(): DedicatedWorkerGlobalScope;
name: string;
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 self: WorkerGlobalScope & typeof globalThis;
declare var name: string;
declare var onmessage:
| ((
this: WorkerGlobalScope & typeof globalThis,
ev: MessageEvent,
) => any)
| ((this: DedicatedWorkerGlobalScope, ev: MessageEvent) => any)
| null;
declare var onmessageerror:
| ((
this: WorkerGlobalScope & typeof globalThis,
ev: MessageEvent,
) => any)
| ((this: DedicatedWorkerGlobalScope, ev: MessageEvent) => any)
| null;
declare function close(): void;
declare function postMessage(message: any): void;
declare var navigator: WorkerNavigator;
declare var onerror:
| ((
this: WorkerGlobalScope & typeof globalThis,
ev: ErrorEvent,
) => any)
| ((this: DedicatedWorkerGlobalScope, ev: ErrorEvent) => any)
| null;
declare var close: () => void;
declare var name: string;
declare var postMessage: (message: any) => void;
declare var self: WorkerGlobalScope & typeof globalThis;
declare function addEventListener<
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.
// The types there must first be split into window, worker and global types.
Expand Down