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

Add async iterable flavors #1135

Closed
wants to merge 3 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
8 changes: 8 additions & 0 deletions baselines/audioworklet.asynciterable.generated.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/////////////////////////////
/// AudioWorklet Async Iterable APIs
/////////////////////////////

interface ReadableStream<R = any> {
[Symbol.asyncIterator](options?: ReadableStreamIteratorOptions): AsyncIterableIterator<R>;
values(options?: ReadableStreamIteratorOptions): AsyncIterableIterator<R>;
}
11 changes: 11 additions & 0 deletions baselines/audioworklet.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ interface ReadableStreamDefaultReadValueResult<T> {
value: T;
}

interface ReadableStreamIteratorOptions {
/**
* Asynchronously iterates over the chunks in the stream's internal queue.
*
* Asynchronously iterating over the stream will lock it, preventing any other consumer from acquiring a reader. The lock will be released if the async iterator's return() method is called, e.g. by breaking out of the loop.
*
* By default, calling the async iterator's return() method will also cancel the stream. To prevent this, use the stream's values() method, passing true for the preventCancel option.
*/
preventCancel?: boolean;
}

interface ReadableWritablePair<R = any, W = any> {
readable: ReadableStream<R>;
/**
Expand Down
8 changes: 8 additions & 0 deletions baselines/dom.asynciterable.generated.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/////////////////////////////
/// Window Async Iterable APIs
/////////////////////////////

interface ReadableStream<R = any> {
[Symbol.asyncIterator](options?: ReadableStreamIteratorOptions): AsyncIterableIterator<R>;
values(options?: ReadableStreamIteratorOptions): AsyncIterableIterator<R>;
}
11 changes: 11 additions & 0 deletions baselines/dom.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,17 @@ interface ReadableStreamDefaultReadValueResult<T> {
value: T;
}

interface ReadableStreamIteratorOptions {
/**
* Asynchronously iterates over the chunks in the stream's internal queue.
*
* Asynchronously iterating over the stream will lock it, preventing any other consumer from acquiring a reader. The lock will be released if the async iterator's return() method is called, e.g. by breaking out of the loop.
*
* By default, calling the async iterator's return() method will also cancel the stream. To prevent this, use the stream's values() method, passing true for the preventCancel option.
*/
preventCancel?: boolean;
}

interface ReadableWritablePair<R = any, W = any> {
readable: ReadableStream<R>;
/**
Expand Down
8 changes: 8 additions & 0 deletions baselines/serviceworker.asynciterable.generated.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/////////////////////////////
/// ServiceWorker Async Iterable APIs
/////////////////////////////

interface ReadableStream<R = any> {
[Symbol.asyncIterator](options?: ReadableStreamIteratorOptions): AsyncIterableIterator<R>;
values(options?: ReadableStreamIteratorOptions): AsyncIterableIterator<R>;
}
11 changes: 11 additions & 0 deletions baselines/serviceworker.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,17 @@ interface ReadableStreamDefaultReadValueResult<T> {
value: T;
}

interface ReadableStreamIteratorOptions {
/**
* Asynchronously iterates over the chunks in the stream's internal queue.
*
* Asynchronously iterating over the stream will lock it, preventing any other consumer from acquiring a reader. The lock will be released if the async iterator's return() method is called, e.g. by breaking out of the loop.
*
* By default, calling the async iterator's return() method will also cancel the stream. To prevent this, use the stream's values() method, passing true for the preventCancel option.
*/
preventCancel?: boolean;
}

interface ReadableWritablePair<R = any, W = any> {
readable: ReadableStream<R>;
/**
Expand Down
8 changes: 8 additions & 0 deletions baselines/sharedworker.asynciterable.generated.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/////////////////////////////
/// SharedWorker Async Iterable APIs
/////////////////////////////

interface ReadableStream<R = any> {
[Symbol.asyncIterator](options?: ReadableStreamIteratorOptions): AsyncIterableIterator<R>;
values(options?: ReadableStreamIteratorOptions): AsyncIterableIterator<R>;
}
11 changes: 11 additions & 0 deletions baselines/sharedworker.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,17 @@ interface ReadableStreamDefaultReadValueResult<T> {
value: T;
}

interface ReadableStreamIteratorOptions {
/**
* Asynchronously iterates over the chunks in the stream's internal queue.
*
* Asynchronously iterating over the stream will lock it, preventing any other consumer from acquiring a reader. The lock will be released if the async iterator's return() method is called, e.g. by breaking out of the loop.
*
* By default, calling the async iterator's return() method will also cancel the stream. To prevent this, use the stream's values() method, passing true for the preventCancel option.
*/
preventCancel?: boolean;
}

interface ReadableWritablePair<R = any, W = any> {
readable: ReadableStream<R>;
/**
Expand Down
8 changes: 8 additions & 0 deletions baselines/webworker.asynciterable.generated.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/////////////////////////////
/// Worker Async Iterable APIs
/////////////////////////////

interface ReadableStream<R = any> {
[Symbol.asyncIterator](options?: ReadableStreamIteratorOptions): AsyncIterableIterator<R>;
values(options?: ReadableStreamIteratorOptions): AsyncIterableIterator<R>;
}
11 changes: 11 additions & 0 deletions baselines/webworker.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,17 @@ interface ReadableStreamDefaultReadValueResult<T> {
value: T;
}

interface ReadableStreamIteratorOptions {
/**
* Asynchronously iterates over the chunks in the stream's internal queue.
*
* Asynchronously iterating over the stream will lock it, preventing any other consumer from acquiring a reader. The lock will be released if the async iterator's return() method is called, e.g. by breaking out of the loop.
*
* By default, calling the async iterator's return() method will also cancel the stream. To prevent this, use the stream's values() method, passing true for the preventCancel option.
*/
preventCancel?: boolean;
}

interface ReadableWritablePair<R = any, W = any> {
readable: ReadableStream<R>;
/**
Expand Down
7 changes: 7 additions & 0 deletions inputfiles/overridingTypes.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -2332,6 +2332,13 @@
}
}
}
},
"iterator": {
"type": {
"0": {
"overrideType": "R"
}
}
}
},
"ReadableStreamDefaultReader": {
Expand Down
13 changes: 11 additions & 2 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,26 @@ async function emitFlavor(
const exposed = getExposedTypes(webidl, options.global, forceKnownTypes);
mergeNamesakes(exposed);

const result = emitWebIdl(exposed, options.global[0], false);
const result = emitWebIdl(exposed, options.global[0], "");
await fs.writeFile(
new URL(`${options.name}.generated.d.ts`, options.outputFolder),
result
);

const iterators = emitWebIdl(exposed, options.global[0], true);
const iterators = emitWebIdl(exposed, options.global[0], "sync");
await fs.writeFile(
new URL(`${options.name}.iterable.generated.d.ts`, options.outputFolder),
iterators
);

const asyncIterators = emitWebIdl(exposed, options.global[0], "async");
await fs.writeFile(
new URL(
`${options.name}.asynciterable.generated.d.ts`,
options.outputFolder
),
asyncIterators
);
}

async function emitDom() {
Expand Down
119 changes: 107 additions & 12 deletions src/build/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function isEventHandler(p: Browser.Property) {
export function emitWebIdl(
webidl: Browser.WebIdl,
global: string,
iterator: boolean
iterator: "" | "sync" | "async"
): string {
// Global print target
const printer = createTextWriter("\n");
Expand Down Expand Up @@ -225,7 +225,14 @@ export function emitWebIdl(
getParentsWithConstant
);

return iterator ? emitES6DomIterators() : emit();
switch (iterator) {
case "sync":
return emitES6DomIterators();
case "async":
return emitES2018DomAsyncIterators();
default:
return emit();
}

function getTagNameToElementNameMap() {
const htmlResult: Record<string, string> = {};
Expand Down Expand Up @@ -400,7 +407,7 @@ export function emitWebIdl(
}

function convertDomTypeToTsTypeSimple(objDomType: string): string {
if (objDomType === "sequence" && iterator) {
if (objDomType === "sequence" && iterator !== "") {
return "Iterable";
}
if (baseTypeConversionMap.has(objDomType)) {
Expand Down Expand Up @@ -996,7 +1003,7 @@ export function emitWebIdl(

// Emit forEach for iterators
function emitIteratorForEach(i: Browser.Interface) {
if (!i.iterator) {
if (!i.iterator || i.iterator.async) {
return;
}
const subtype = i.iterator.type.map(convertDomTypeToTsType);
Expand Down Expand Up @@ -1498,6 +1505,13 @@ export function emitWebIdl(
return printer.getResult();
}

function stringifySingleOrTupleTypes(types: string[]) {
if (types.length === 1) {
return types[0];
}
return `[${types.join(", ")}]`;
}

function emitIterator(i: Browser.Interface) {
// https://heycam.github.io/webidl/#dfn-indexed-property-getter
const isIndexedPropertyGetter = (m: Browser.AnonymousMethod) =>
Expand All @@ -1518,7 +1532,7 @@ export function emitWebIdl(
}

function getIteratorSubtypes() {
if (i.iterator) {
if (i.iterator && !i.iterator.async) {
if (i.iterator.type.length === 1) {
return [convertDomTypeToTsType(i.iterator.type[0])];
}
Expand All @@ -1536,13 +1550,6 @@ export function emitWebIdl(
}
}

function stringifySingleOrTupleTypes(types: string[]) {
if (types.length === 1) {
return types[0];
}
return `[${types.join(", ")}]`;
}

function emitIterableDeclarationMethods(
i: Browser.Interface,
subtypes: string[]
Expand Down Expand Up @@ -1682,6 +1689,83 @@ export function emitWebIdl(
}
}

function emitAsyncIterator(i: Browser.Interface) {
function getAsyncIteratorSubtypes() {
if (i.iterator && i.iterator.kind === "iterable" && i.iterator.async) {
if (i.iterator.type.length === 1) {
return [convertDomTypeToTsType(i.iterator.type[0])];
}
return i.iterator.type.map(convertDomTypeToTsType);
}
}

function emitAsyncIterableDeclarationMethods(
i: Browser.Interface,
subtypes: string[],
paramsString: string
) {
let methods;
if (subtypes.length === 1) {
// https://heycam.github.io/webidl/#value-asynchronously-iterable-declaration
const [valueType] = subtypes;
methods = [
{
name: "values",
definition: `AsyncIterableIterator<${valueType}>`,
},
];
} else {
// https://heycam.github.io/webidl/#pair-asynchronously-iterable-declaration
const [keyType, valueType] = subtypes;
methods = [
{
name: "entries",
definition: `AsyncIterableIterator<[${keyType}, ${valueType}]>`,
},
{
name: "keys",
definition: `AsyncIterableIterator<${keyType}>`,
},
{
name: "values",
definition: `AsyncIterableIterator<${valueType}>`,
},
];
}

const comments = i.iterator!.comments?.comment;

methods.forEach((m) => {
emitComments({ comment: comments?.[m.name] }, printer.printLine);
printer.printLine(`${m.name}(${paramsString}): ${m.definition};`);
});
}

const subtypes = getAsyncIteratorSubtypes();
if (subtypes) {
const name = getNameWithTypeParameter(
i.typeParameters,
extendConflictsBaseTypes[i.name] ? `${i.name}Base` : i.name
);
const paramsString = i.iterator!.param
? paramsToString(i.iterator!.param)
: "";
printer.printLine("");
printer.printLine(`interface ${name} {`);
printer.increaseIndent();

printer.printLine(
`[Symbol.asyncIterator](${paramsString}): AsyncIterableIterator<${stringifySingleOrTupleTypes(
subtypes
)}>;`
);
emitAsyncIterableDeclarationMethods(i, subtypes, paramsString);

printer.decreaseIndent();
printer.printLine("}");
}
}

function emitES6DomIterators() {
printer.reset();
printer.printLine("/////////////////////////////");
Expand All @@ -1692,4 +1776,15 @@ export function emitWebIdl(

return printer.getResult();
}

function emitES2018DomAsyncIterators() {
printer.reset();
printer.printLine("/////////////////////////////");
printer.printLine(`/// ${global} Async Iterable APIs`);
printer.printLine("/////////////////////////////");

allInterfaces.sort(compareName).forEach(emitAsyncIterator);

return printer.getResult();
}
}
2 changes: 2 additions & 0 deletions src/build/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ export interface Interface {
export interface Iterator {
kind: "iterable" | "setlike" | "maplike";
readonly: boolean;
async: boolean;
type: Typed[];
param?: Param[];
comments?: {
comment: Record<string, string>;
};
Expand Down
4 changes: 3 additions & 1 deletion src/build/widlprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,15 @@ function convertInterfaceCommon(
addComments(method[member.name], commentMap, i.name, member.name);
}
} else if (
(member.type === "iterable" && !member.async) ||
member.type === "iterable" ||
member.type === "maplike" ||
member.type === "setlike"
) {
result.iterator = {
kind: member.type,
readonly: member.readonly,
async: member.async,
param: member.arguments.map(convertArgument),
type: member.idlType.map(convertIdlType),
};
}
Expand Down
Loading