@@ -135,7 +135,7 @@ function isEventHandler(p: Browser.Property) {
135
135
export function emitWebIdl (
136
136
webidl : Browser . WebIdl ,
137
137
global : string ,
138
- iterator : boolean
138
+ iterator : "" | "sync" | "async"
139
139
) : string {
140
140
// Global print target
141
141
const printer = createTextWriter ( "\n" ) ;
@@ -225,7 +225,14 @@ export function emitWebIdl(
225
225
getParentsWithConstant
226
226
) ;
227
227
228
- return iterator ? emitES6DomIterators ( ) : emit ( ) ;
228
+ switch ( iterator ) {
229
+ case "sync" :
230
+ return emitES6DomIterators ( ) ;
231
+ case "async" :
232
+ return emitES2018DomAsyncIterators ( ) ;
233
+ default :
234
+ return emit ( ) ;
235
+ }
229
236
230
237
function getTagNameToElementNameMap ( ) {
231
238
const htmlResult : Record < string , string > = { } ;
@@ -402,7 +409,7 @@ export function emitWebIdl(
402
409
}
403
410
404
411
function convertDomTypeToTsTypeSimple ( objDomType : string ) : string {
405
- if ( objDomType === "sequence" && iterator ) {
412
+ if ( objDomType === "sequence" && iterator !== "" ) {
406
413
return "Iterable" ;
407
414
}
408
415
if ( baseTypeConversionMap . has ( objDomType ) ) {
@@ -1012,7 +1019,7 @@ export function emitWebIdl(
1012
1019
1013
1020
// Emit forEach for iterators
1014
1021
function emitIteratorForEach ( i : Browser . Interface ) {
1015
- if ( ! i . iterator ) {
1022
+ if ( ! i . iterator || i . iterator . async ) {
1016
1023
return ;
1017
1024
}
1018
1025
const subtype = i . iterator . type . map ( convertDomTypeToTsType ) ;
@@ -1521,8 +1528,15 @@ export function emitWebIdl(
1521
1528
return printer . getResult ( ) ;
1522
1529
}
1523
1530
1531
+ function stringifySingleOrTupleTypes ( types : string [ ] ) {
1532
+ if ( types . length === 1 ) {
1533
+ return types [ 0 ] ;
1534
+ }
1535
+ return `[${ types . join ( ", " ) } ]` ;
1536
+ }
1537
+
1524
1538
function emitIterator ( i : Browser . Interface ) {
1525
- // https://heycam.github.io/webidl /#dfn-indexed-property-getter
1539
+ // https://webidl.spec.whatwg.org /#dfn-indexed-property-getter
1526
1540
const isIndexedPropertyGetter = ( m : Browser . AnonymousMethod ) =>
1527
1541
m . getter &&
1528
1542
m . signature [ 0 ] ?. param ?. length === 1 &&
@@ -1541,7 +1555,7 @@ export function emitWebIdl(
1541
1555
}
1542
1556
1543
1557
function getIteratorSubtypes ( ) {
1544
- if ( i . iterator ) {
1558
+ if ( i . iterator && ! i . iterator . async ) {
1545
1559
if ( i . iterator . type . length === 1 ) {
1546
1560
return [ convertDomTypeToTsType ( i . iterator . type [ 0 ] ) ] ;
1547
1561
}
@@ -1559,13 +1573,6 @@ export function emitWebIdl(
1559
1573
}
1560
1574
}
1561
1575
1562
- function stringifySingleOrTupleTypes ( types : string [ ] ) {
1563
- if ( types . length === 1 ) {
1564
- return types [ 0 ] ;
1565
- }
1566
- return `[${ types . join ( ", " ) } ]` ;
1567
- }
1568
-
1569
1576
function emitIterableDeclarationMethods (
1570
1577
i : Browser . Interface ,
1571
1578
subtypes : string [ ]
@@ -1678,31 +1685,112 @@ export function emitWebIdl(
1678
1685
. filter ( ( m ) => m . signature . length )
1679
1686
. sort ( compareName ) ;
1680
1687
1681
- if ( subtypes || methodsWithSequence . length ) {
1682
- const iteratorExtends = getIteratorExtends ( i . iterator , subtypes ) ;
1683
- const name = getNameWithTypeParameter (
1684
- i . typeParameters ,
1685
- extendConflictsBaseTypes [ i . name ] ? `${ i . name } Base` : i . name
1686
- ) ;
1687
- printer . printLine ( "" ) ;
1688
- printer . printLine ( `interface ${ name } ${ iteratorExtends } {` ) ;
1689
- printer . increaseIndent ( ) ;
1688
+ if ( ! subtypes && ! methodsWithSequence . length ) {
1689
+ return ;
1690
+ }
1690
1691
1691
- methodsWithSequence . forEach ( ( m ) => emitMethod ( "" , m , new Set ( ) ) ) ;
1692
+ const iteratorExtends = getIteratorExtends ( i . iterator , subtypes ) ;
1693
+ const name = getNameWithTypeParameter (
1694
+ i . typeParameters ,
1695
+ extendConflictsBaseTypes [ i . name ] ? `${ i . name } Base` : i . name
1696
+ ) ;
1697
+ printer . printLine ( "" ) ;
1698
+ printer . printLine ( `interface ${ name } ${ iteratorExtends } {` ) ;
1699
+ printer . increaseIndent ( ) ;
1692
1700
1693
- if ( subtypes && ! iteratorExtends ) {
1694
- printer . printLine (
1695
- `[Symbol.iterator](): IterableIterator<${ stringifySingleOrTupleTypes (
1696
- subtypes
1697
- ) } >;`
1698
- ) ;
1701
+ methodsWithSequence . forEach ( ( m ) => emitMethod ( "" , m , new Set ( ) ) ) ;
1702
+
1703
+ if ( subtypes && ! iteratorExtends ) {
1704
+ printer . printLine (
1705
+ `[Symbol.iterator](): IterableIterator<${ stringifySingleOrTupleTypes (
1706
+ subtypes
1707
+ ) } >;`
1708
+ ) ;
1709
+ }
1710
+ if ( i . iterator ?. kind === "iterable" && subtypes ) {
1711
+ emitIterableDeclarationMethods ( i , subtypes ) ;
1712
+ }
1713
+ printer . decreaseIndent ( ) ;
1714
+ printer . printLine ( "}" ) ;
1715
+ }
1716
+
1717
+ function emitAsyncIterator ( i : Browser . Interface ) {
1718
+ function getAsyncIteratorSubtypes ( ) {
1719
+ if ( i . iterator && i . iterator . kind === "iterable" && i . iterator . async ) {
1720
+ if ( i . iterator . type . length === 1 ) {
1721
+ return [ convertDomTypeToTsType ( i . iterator . type [ 0 ] ) ] ;
1722
+ }
1723
+ return i . iterator . type . map ( convertDomTypeToTsType ) ;
1699
1724
}
1700
- if ( i . iterator ?. kind === "iterable" && subtypes ) {
1701
- emitIterableDeclarationMethods ( i , subtypes ) ;
1725
+ }
1726
+
1727
+ function emitAsyncIterableDeclarationMethods (
1728
+ i : Browser . Interface ,
1729
+ subtypes : string [ ] ,
1730
+ paramsString : string
1731
+ ) {
1732
+ let methods ;
1733
+ if ( subtypes . length === 1 ) {
1734
+ // https://webidl.spec.whatwg.org/#value-asynchronously-iterable-declaration
1735
+ const [ valueType ] = subtypes ;
1736
+ methods = [
1737
+ {
1738
+ name : "values" ,
1739
+ definition : `AsyncIterableIterator<${ valueType } >` ,
1740
+ } ,
1741
+ ] ;
1742
+ } else {
1743
+ // https://webidl.spec.whatwg.org/#pair-asynchronously-iterable-declaration
1744
+ const [ keyType , valueType ] = subtypes ;
1745
+ methods = [
1746
+ {
1747
+ name : "entries" ,
1748
+ definition : `AsyncIterableIterator<[${ keyType } , ${ valueType } ]>` ,
1749
+ } ,
1750
+ {
1751
+ name : "keys" ,
1752
+ definition : `AsyncIterableIterator<${ keyType } >` ,
1753
+ } ,
1754
+ {
1755
+ name : "values" ,
1756
+ definition : `AsyncIterableIterator<${ valueType } >` ,
1757
+ } ,
1758
+ ] ;
1702
1759
}
1703
- printer . decreaseIndent ( ) ;
1704
- printer . printLine ( "}" ) ;
1760
+
1761
+ const comments = i . iterator ! . comments ?. comment ;
1762
+
1763
+ methods . forEach ( ( m ) => {
1764
+ emitComments ( { comment : comments ?. [ m . name ] } , printer . printLine ) ;
1765
+ printer . printLine ( `${ m . name } (${ paramsString } ): ${ m . definition } ;` ) ;
1766
+ } ) ;
1767
+ }
1768
+
1769
+ const subtypes = getAsyncIteratorSubtypes ( ) ;
1770
+ if ( ! subtypes ) {
1771
+ return ;
1705
1772
}
1773
+
1774
+ const name = getNameWithTypeParameter (
1775
+ i . typeParameters ,
1776
+ extendConflictsBaseTypes [ i . name ] ? `${ i . name } Base` : i . name
1777
+ ) ;
1778
+ const paramsString = i . iterator ! . param
1779
+ ? paramsToString ( i . iterator ! . param )
1780
+ : "" ;
1781
+ printer . printLine ( "" ) ;
1782
+ printer . printLine ( `interface ${ name } {` ) ;
1783
+ printer . increaseIndent ( ) ;
1784
+
1785
+ printer . printLine (
1786
+ `[Symbol.asyncIterator](${ paramsString } ): AsyncIterableIterator<${ stringifySingleOrTupleTypes (
1787
+ subtypes
1788
+ ) } >;`
1789
+ ) ;
1790
+ emitAsyncIterableDeclarationMethods ( i , subtypes , paramsString ) ;
1791
+
1792
+ printer . decreaseIndent ( ) ;
1793
+ printer . printLine ( "}" ) ;
1706
1794
}
1707
1795
1708
1796
function emitES6DomIterators ( ) {
@@ -1715,4 +1803,15 @@ export function emitWebIdl(
1715
1803
1716
1804
return printer . getResult ( ) ;
1717
1805
}
1806
+
1807
+ function emitES2018DomAsyncIterators ( ) {
1808
+ printer . reset ( ) ;
1809
+ printer . printLine ( "/////////////////////////////" ) ;
1810
+ printer . printLine ( `/// ${ global } Async Iterable APIs` ) ;
1811
+ printer . printLine ( "/////////////////////////////" ) ;
1812
+
1813
+ allInterfaces . sort ( compareName ) . forEach ( emitAsyncIterator ) ;
1814
+
1815
+ return printer . getResult ( ) ;
1816
+ }
1718
1817
}
0 commit comments