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 all commits
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
4 changes: 2 additions & 2 deletions baselines/dom.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11414,7 +11414,7 @@ declare var PaymentRequest: {

/** This Payment Request API interface enables a web page to update the details of a PaymentRequest in response to a user action. */
interface PaymentRequestUpdateEvent extends Event {
updateWith(detailsPromise: PaymentDetailsUpdate | Promise<PaymentDetailsUpdate>): void;
updateWith(detailsPromise: PaymentDetailsUpdate | PromiseLike<PaymentDetailsUpdate>): void;
}

declare var PaymentRequestUpdateEvent: {
Expand Down Expand Up @@ -19148,7 +19148,7 @@ declare namespace WebAssembly {
type ModuleImports = Record<string, ImportValue>;
type Imports = Record<string, ModuleImports>;
function compile(bytes: BufferSource): Promise<Module>;
function compileStreaming(source: Response | Promise<Response>): Promise<Module>;
function compileStreaming(source: Response | PromiseLike<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>;
Expand Down
4 changes: 2 additions & 2 deletions baselines/webworker.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1521,7 +1521,7 @@ interface FetchEvent extends ExtendableEvent {
readonly replacesClientId: string;
readonly request: Request;
readonly resultingClientId: string;
respondWith(r: Response | Promise<Response>): void;
respondWith(r: Response | PromiseLike<Response>): void;
}

declare var FetchEvent: {
Expand Down Expand Up @@ -5837,7 +5837,7 @@ declare namespace WebAssembly {
type ModuleImports = Record<string, ImportValue>;
type Imports = Record<string, ModuleImports>;
function compile(bytes: BufferSource): Promise<Module>;
function compileStreaming(source: Response | Promise<Response>): Promise<Module>;
function compileStreaming(source: Response | PromiseLike<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>;
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 @@ -112,10 +112,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 @@ -150,8 +150,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
25 changes: 0 additions & 25 deletions inputfiles/overridingTypes.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,6 @@
"(chunk: T): number"
]
},
"ReadableByteStreamControllerCallback": {
"override-signatures": [
"(controller: ReadableByteStreamController): void | PromiseLike<void>"
]
},
"ReadableStreamDefaultControllerCallback": {
"type-parameters": [
{
Expand All @@ -240,16 +235,6 @@
"(controller: ReadableStreamDefaultController<R>): void | PromiseLike<void>"
]
},
"ReadableStreamErrorCallback": {
"override-signatures": [
"(reason: any): void | PromiseLike<void>"
]
},
"WritableStreamDefaultControllerStartCallback": {
"override-signatures": [
"(controller: WritableStreamDefaultController): void | PromiseLike<void>"
]
},
"WritableStreamDefaultControllerWriteCallback": {
"type-parameters": [
{
Expand All @@ -260,16 +245,6 @@
"(chunk: W, controller: WritableStreamDefaultController): void | PromiseLike<void>"
]
},
"WritableStreamDefaultControllerCloseCallback": {
"override-signatures": [
"(): void | PromiseLike<void>"
]
},
"WritableStreamErrorCallback": {
"override-signatures": [
"(reason: any): void | PromiseLike<void>"
]
},
"TransformStreamDefaultControllerCallback": {
"type-parameters": [
{
Expand Down
15 changes: 11 additions & 4 deletions src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,12 +511,18 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor, iterator: boo
}
}

function replacePromiseTypeWithUnion<T extends Browser.Typed>(type: T) {
if (type.type === "Promise" && !Array.isArray(type.subtype)) {
const promiseLike = { ...type, type: "PromiseLike" };
return { ...type, type: [type.subtype!, promiseLike], 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 @@ -551,7 +557,8 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor, iterator: boo
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
3 changes: 2 additions & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export const baseTypeConversionMap = new Map<string, string>([
["sequence", "Array"],
["record", "Record"],
["FrozenArray", "ReadonlyArray"],
["EventHandler", "EventHandler"]
["EventHandler", "EventHandler"],
["PromiseLike", "PromiseLike"]
]);

export function filter<T>(obj: T, fn: (o: any, n: string | undefined) => boolean): T {
Expand Down