@@ -12,21 +12,21 @@ export const states = {
1212} ;
1313
1414/**
15- * @typedef {Object } StreamAsyncToIterator ~Options
15+ * @typedef {Object } StreamToAsyncIterator ~Options
1616 * @property {number } [size] - the size of each read from the stream for each iteration
1717 */
18- type StreamAsyncToIteratorOptions = {
18+ type StreamToAsyncIteratorOptions = {
1919 size ?: number ;
2020}
2121
2222/**
23- * @typedef {Object } StreamAsyncToIterator ~Iteration
23+ * @typedef {Object } StreamToAsyncIterator ~Iteration
2424 * @property {boolean } done
25- * @property {* } value
25+ * @property {* } [ value]
2626 */
27- type Iteration = {
27+ type Iteration < TVal > = {
2828 done : boolean ;
29- value: any ;
29+ value ? : TVal ;
3030}
3131
3232type Reject = ( err : any ) => void ;
@@ -38,12 +38,12 @@ type Reject = (err: any) => void;
3838 * iteration. A size can be supplied to set an explicit call to `stream.read([size])` in
3939 * the options for each iteration.
4040 */
41- export default class StreamAsyncToIterator {
41+ export default class StreamToAsyncIterator < TVal > {
4242 /**
4343 * @param {Readable } stream
44- * @param {StreamAsyncToIterator ~Options } [options]
44+ * @param {StreamToAsyncIterator ~Options } [options]
4545 */
46- constructor ( stream : Readable , options : StreamAsyncToIteratorOptions = { } ) {
46+ constructor ( stream : Readable , options : StreamToAsyncIteratorOptions = { } ) {
4747 /**
4848 * The underlying readable stream
4949 * @private
@@ -100,23 +100,28 @@ export default class StreamAsyncToIterator {
100100 _size: ?number;
101101 _rejections: Set< Reject > ;
102102
103+ //todo: flow is now working with this method in place
104+ // [Symbol.asyncIterator]() {
105+ // return this;
106+ // }
107+
103108 /**
104109 * Returns the next iteration of data. Rejects if the stream errored out.
105- * @returns {Promise<StreamAsyncToIterator ~Iteration> }
110+ * @returns {Promise<StreamToAsyncIterator ~Iteration> }
106111 */
107- async next ( ) : Promise < Iteration > {
112+ async next ( ) : Promise < Iteration < TVal > > {
108113 if ( this . _state === states . notReadable ) {
109114 //need to wait until the stream is readable or ended
110115 await Promise . race ( [ this . _untilReadable ( ) , this . _untilEnd ( ) ] ) ;
111116 return this . next ( ) ;
112117 } else if ( this . _state === states . ended ) {
113- return { done : true , value : null } ;
118+ return { done : true } ;
114119 } else if (this._state === states.errored) {
115120 throw this . _error ;
116121 } else /* readable */ {
117122 //stream.read returns null if not readable or when stream has ended
118123
119- const data = this . _size ? this . _stream . read ( this . _size ) : this . _stream . read ( ) ;
124+ const data : TVal = this . _size ? ( this . _stream . read ( this . _size ) : any ) : ( this . _stream . read ( ) : any ) ;
120125
121126 if ( data !== null ) {
122127 return { done : false , value : data } ;
@@ -164,7 +169,8 @@ export default class StreamAsyncToIterator {
164169 }
165170}
166171
167- Object . defineProperty ( StreamAsyncToIterator . prototype , ( Symbol : any ) . asyncIterator , {
172+ //hack: gets around flows inability to handle symbol properties
173+ Object . defineProperty ( StreamToAsyncIterator . prototype , ( Symbol : any ) . asyncIterator , {
168174 configurable : true ,
169175 value : function ( ) { return this ; }
170176} );
0 commit comments