Skip to content

Commit 182d0ef

Browse files
committed
Fix some module naming, fix some flow type errors
1 parent 43646bd commit 182d0ef

File tree

5 files changed

+41
-28
lines changed

5 files changed

+41
-28
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea/
22
build/
33
flow-typed/
4+
*.js.flow

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/stream-to-async-iterator.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3232
type 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
});
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// @flow
22

3-
declare type StreamAsyncIteratorOptions = {
3+
declare type StreamToAsyncIteratorOptions = {
44
size?: number;
55
}
66

7-
declare export default class StreamAsyncIterator<TData> {
8-
constructor(stream: stream$Readable, options?: StreamAsyncIteratorOptions): StreamAsyncIterator<TData>;
9-
@@asyncIterator(): $AsyncIterator<TData, void, void>;
10-
next(): Promise<IteratorResult<TData, void>>;
7+
declare export default class StreamToAsyncIterator<TVal> {
8+
constructor(stream: stream$Readable, options?: StreamToAsyncIteratorOptions): void;
9+
@@asyncIterator(): $AsyncIterator<TVal, void, void>;
10+
next(): Promise<IteratorResult<TVal, void>>;
1111
}

lib/test/stream-to-async-iterator-tests.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,36 @@
11
// @flow
2-
import {describe, it} from 'mocha';
32
import fs from 'fs';
43
import path from 'path';
5-
import {expect} from 'chai';
4+
5+
import chai from 'chai';
6+
import mocha from 'mocha';
7+
68
import S2A from '../stream-to-async-iterator';
79

10+
const {expect} = chai;
11+
const {describe, it} = mocha;
12+
813

9-
describe('StreamAsyncToIterator', () => {
14+
describe('StreamToAsyncIterator', function() {
1015
const filePath = path.join(__dirname, 'lorem-ipsum.txt');
1116

12-
it('should iterate on a node stream', async () => {
17+
it('should iterate on a node stream with string encoding', async function() {
1318
const fileStream = fs.createReadStream(filePath, {encoding: 'utf8'});
14-
const buff: Array<string> = [];
19+
const buff: string[] = [];
1520

16-
for await (const value of (new S2A(fileStream): S2A<string>)) {
21+
for await (const value of new S2A(fileStream)) {
1722
buff.push(value);
1823
}
1924

2025
const content = buff.join('');
2126
expect(content).to.have.lengthOf(1502);
2227
});
2328

24-
it('should iterate on a node stream with a size', async () => {
29+
it('should iterate on a node stream with a size with string encoding', async function() {
2530
const fileStream = fs.createReadStream(filePath, {encoding: 'utf8'});
26-
const buff: Array<string> = [];
31+
const buff: string[] = [];
2732

28-
for await (const value of (new S2A(fileStream, {size: 16}): S2A<string>)) {
33+
for await (const value of new S2A(fileStream, {size: 16})) {
2934
buff.push(value);
3035
}
3136

0 commit comments

Comments
 (0)