Skip to content

Replace Promises with union types in callbacks #872

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions baselines/dom.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19104,7 +19104,7 @@ declare namespace WebAssembly {
function compileStreaming(source: Response | Promise<Response>): Promise<Module>;
function instantiate(bytes: BufferSource, importObject?: Imports): Promise<WebAssemblyInstantiatedSource>;
function instantiate(moduleObject: Module, importObject?: Imports): Promise<Instance>;
function instantiateStreaming(response: Response | PromiseLike<Response>, importObject?: Imports): Promise<WebAssemblyInstantiatedSource>;
function instantiateStreaming(response: Response | Promise<Response>, importObject?: Imports): Promise<WebAssemblyInstantiatedSource>;
function validate(bytes: BufferSource): boolean;
}

Expand Down Expand Up @@ -19201,43 +19201,43 @@ interface RTCStatsCallback {
}

interface ReadableByteStreamControllerCallback {
(controller: ReadableByteStreamController): void | PromiseLike<void>;
(controller: ReadableByteStreamController): void | Promise<void>;
}

interface ReadableStreamDefaultControllerCallback<R> {
(controller: ReadableStreamDefaultController<R>): void | PromiseLike<void>;
(controller: ReadableStreamDefaultController<R>): void | Promise<void>;
}

interface ReadableStreamErrorCallback {
(reason: any): void | PromiseLike<void>;
(reason: any): void | Promise<void>;
}

interface TransformStreamDefaultControllerCallback<O> {
(controller: TransformStreamDefaultController<O>): void | PromiseLike<void>;
(controller: TransformStreamDefaultController<O>): void | Promise<void>;
}

interface TransformStreamDefaultControllerTransformCallback<I, O> {
(chunk: I, controller: TransformStreamDefaultController<O>): void | PromiseLike<void>;
(chunk: I, controller: TransformStreamDefaultController<O>): void | Promise<void>;
}

interface VoidFunction {
(): void;
}

interface WritableStreamDefaultControllerCloseCallback {
(): void | PromiseLike<void>;
(): void | Promise<void>;
}

interface WritableStreamDefaultControllerStartCallback {
(controller: WritableStreamDefaultController): void | PromiseLike<void>;
(controller: WritableStreamDefaultController): void | Promise<void>;
}

interface WritableStreamDefaultControllerWriteCallback<W> {
(chunk: W, controller: WritableStreamDefaultController): void | PromiseLike<void>;
(chunk: W, controller: WritableStreamDefaultController): void | Promise<void>;
}

interface WritableStreamErrorCallback {
(reason: any): void | PromiseLike<void>;
(reason: any): void | Promise<void>;
}

interface HTMLElementTagNameMap {
Expand Down
20 changes: 10 additions & 10 deletions baselines/webworker.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5800,7 +5800,7 @@ declare namespace WebAssembly {
function compileStreaming(source: Response | Promise<Response>): Promise<Module>;
function instantiate(bytes: BufferSource, importObject?: Imports): Promise<WebAssemblyInstantiatedSource>;
function instantiate(moduleObject: Module, importObject?: Imports): Promise<Instance>;
function instantiateStreaming(response: Response | PromiseLike<Response>, importObject?: Imports): Promise<WebAssemblyInstantiatedSource>;
function instantiateStreaming(response: Response | Promise<Response>, importObject?: Imports): Promise<WebAssemblyInstantiatedSource>;
function validate(bytes: BufferSource): boolean;
}

Expand All @@ -5821,43 +5821,43 @@ interface QueuingStrategySizeCallback<T = any> {
}

interface ReadableByteStreamControllerCallback {
(controller: ReadableByteStreamController): void | PromiseLike<void>;
(controller: ReadableByteStreamController): void | Promise<void>;
}

interface ReadableStreamDefaultControllerCallback<R> {
(controller: ReadableStreamDefaultController<R>): void | PromiseLike<void>;
(controller: ReadableStreamDefaultController<R>): void | Promise<void>;
}

interface ReadableStreamErrorCallback {
(reason: any): void | PromiseLike<void>;
(reason: any): void | Promise<void>;
}

interface TransformStreamDefaultControllerCallback<O> {
(controller: TransformStreamDefaultController<O>): void | PromiseLike<void>;
(controller: TransformStreamDefaultController<O>): void | Promise<void>;
}

interface TransformStreamDefaultControllerTransformCallback<I, O> {
(chunk: I, controller: TransformStreamDefaultController<O>): void | PromiseLike<void>;
(chunk: I, controller: TransformStreamDefaultController<O>): void | Promise<void>;
}

interface VoidFunction {
(): void;
}

interface WritableStreamDefaultControllerCloseCallback {
(): void | PromiseLike<void>;
(): void | Promise<void>;
}

interface WritableStreamDefaultControllerStartCallback {
(controller: WritableStreamDefaultController): void | PromiseLike<void>;
(controller: WritableStreamDefaultController): void | Promise<void>;
}

interface WritableStreamDefaultControllerWriteCallback<W> {
(chunk: W, controller: WritableStreamDefaultController): void | PromiseLike<void>;
(chunk: W, controller: WritableStreamDefaultController): void | Promise<void>;
}

interface WritableStreamErrorCallback {
(reason: any): void | PromiseLike<void>;
(reason: any): void | Promise<void>;
}

/**
Expand Down
18 changes: 9 additions & 9 deletions inputfiles/idl/Streams.widl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ dictionary QueuingStrategy {
QueuingStrategySizeCallback? size;
};

callback ReadableByteStreamControllerCallback = any (ReadableByteStreamController controller);
callback ReadableStreamDefaultControllerCallback = any (ReadableStreamDefaultController controller);
callback ReadableStreamErrorCallback = any (any reason);
callback ReadableByteStreamControllerCallback = Promise<void> (ReadableByteStreamController controller);
callback ReadableStreamDefaultControllerCallback = Promise<void> (ReadableStreamDefaultController controller);
callback ReadableStreamErrorCallback = Promise<void> (any reason);

dictionary UnderlyingSource {
ReadableStreamDefaultControllerCallback? start;
Expand Down Expand Up @@ -117,10 +117,10 @@ interface WritableStream {
WritableStreamDefaultWriter getWriter();
};

callback WritableStreamDefaultControllerStartCallback = any (WritableStreamDefaultController controller);
callback WritableStreamDefaultControllerWriteCallback = any (any chunk, WritableStreamDefaultController controller);
callback WritableStreamDefaultControllerCloseCallback = any ();
callback WritableStreamErrorCallback = any (any reason);
callback WritableStreamDefaultControllerStartCallback = Promise<void> (WritableStreamDefaultController controller);
callback WritableStreamDefaultControllerWriteCallback = Promise<void> (any chunk, WritableStreamDefaultController controller);
callback WritableStreamDefaultControllerCloseCallback = Promise<void> ();
callback WritableStreamErrorCallback = Promise<void> (any reason);

dictionary UnderlyingSink {
WritableStreamDefaultControllerStartCallback? start;
Expand Down Expand Up @@ -157,8 +157,8 @@ interface TransformStream {
readonly attribute WritableStream writable;
};

callback TransformStreamDefaultControllerCallback = any (TransformStreamDefaultController controller);
callback TransformStreamDefaultControllerTransformCallback = any (any chunk, TransformStreamDefaultController controller);
callback TransformStreamDefaultControllerCallback = Promise<void> (TransformStreamDefaultController controller);
callback TransformStreamDefaultControllerTransformCallback = Promise<void> (any chunk, TransformStreamDefaultController controller);

dictionary Transformer {
TransformStreamDefaultControllerCallback? start;
Expand Down
35 changes: 5 additions & 30 deletions inputfiles/overridingTypes.json
Original file line number Diff line number Diff line change
Expand Up @@ -244,29 +244,14 @@
"(chunk: T): number"
]
},
"ReadableByteStreamControllerCallback": {
"override-signatures": [
"(controller: ReadableByteStreamController): void | PromiseLike<void>"
]
},
"ReadableStreamDefaultControllerCallback": {
"type-parameters": [
{
"name": "R"
}
],
"override-signatures": [
"(controller: ReadableStreamDefaultController<R>): void | PromiseLike<void>"
]
},
"ReadableStreamErrorCallback": {
"override-signatures": [
"(reason: any): void | PromiseLike<void>"
]
},
"WritableStreamDefaultControllerStartCallback": {
"override-signatures": [
"(controller: WritableStreamDefaultController): void | PromiseLike<void>"
"(controller: ReadableStreamDefaultController<R>): void | Promise<void>"
]
},
"WritableStreamDefaultControllerWriteCallback": {
Expand All @@ -276,17 +261,7 @@
}
],
"override-signatures": [
"(chunk: W, controller: WritableStreamDefaultController): void | PromiseLike<void>"
]
},
"WritableStreamDefaultControllerCloseCallback": {
"override-signatures": [
"(): void | PromiseLike<void>"
]
},
"WritableStreamErrorCallback": {
"override-signatures": [
"(reason: any): void | PromiseLike<void>"
"(chunk: W, controller: WritableStreamDefaultController): void | Promise<void>"
]
},
"TransformStreamDefaultControllerCallback": {
Expand All @@ -296,7 +271,7 @@
}
],
"override-signatures": [
"(controller: TransformStreamDefaultController<O>): void | PromiseLike<void>"
"(controller: TransformStreamDefaultController<O>): void | Promise<void>"
]
},
"TransformStreamDefaultControllerTransformCallback": {
Expand All @@ -309,7 +284,7 @@
}
],
"override-signatures": [
"(chunk: I, controller: TransformStreamDefaultController<O>): void | PromiseLike<void>"
"(chunk: I, controller: TransformStreamDefaultController<O>): void | Promise<void>"
]
},
"CustomElementConstructor": {
Expand Down Expand Up @@ -3224,7 +3199,7 @@
},
"instantiateStreaming": {
"override-signatures": [
"instantiateStreaming(response: Response | PromiseLike<Response>, importObject?: Imports): Promise<WebAssemblyInstantiatedSource>"
"instantiateStreaming(response: Response | Promise<Response>, importObject?: Imports): Promise<WebAssemblyInstantiatedSource>"
],
"force-references": [
{
Expand Down
14 changes: 10 additions & 4 deletions src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,12 +512,17 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) {
}
}

function replacePromiseTypeWithUnion<T extends Browser.Typed>(type: T) {
if (type.type === "Promise" && !Array.isArray(type.subtype)) {
return { ...type, type: [type.subtype!, type], subtype: undefined };
}
return type;
}

/// Generate the parameters string for function signatures
function paramsToString(ps: Browser.Param[]) {
function paramToString(p: Browser.Param) {
if (p.type === "Promise" && !Array.isArray(p.subtype)) {
p = { name: p.name, type: [p.subtype!, p] }
}
p = replacePromiseTypeWithUnion(p);
const isOptional = !p.variadic && p.optional;
const pType = convertDomTypeToTsType(p);
const variadicParams = p.variadic && pType.indexOf('|') !== -1;
Expand Down Expand Up @@ -552,7 +557,8 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) {
function emitCallBackFunction(cb: Browser.CallbackFunction) {
printer.printLine(`interface ${getNameWithTypeParameter(cb, cb.name)} {`);
printer.increaseIndent();
emitSignatures(cb, "", "", printer.printLine);
const signature = cb.signature.map(replacePromiseTypeWithUnion);
emitSignatures({ ...cb, signature }, "", "", printer.printLine);
printer.decreaseIndent();
printer.printLine("}");
printer.printLine("");
Expand Down