Skip to content

Commit 7a7a863

Browse files
committed
Flatten the PromiseStream with methods
1 parent 54d8a51 commit 7a7a863

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

PromiseStream.js

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,47 @@ function PromiseStream (fn, args, size, maxConcurrency) {
1010
this._waiters = [];
1111
}
1212

13-
PromiseStream.prototype.next = function () {
14-
var self = this;
15-
function startRequest () {
16-
self._concurrency++;
17-
//console.log('start', self._concurrency);
18-
var arg;
19-
if (Array.isArray(self._args) && self._args.length) {
20-
arg = self._args.shift();
21-
} else {
22-
arg = self._args;
23-
self._args = undefined;
24-
}
25-
return self._fn(arg).then(handleResult);
13+
14+
PromiseStream.prototype._startRequest = function () {
15+
this._concurrency++;
16+
//console.log('start', self._concurrency);
17+
var arg;
18+
if (Array.isArray(this._args) && this._args.length) {
19+
arg = this._args.shift();
20+
} else {
21+
arg = this._args;
22+
this._args = undefined;
2623
}
24+
return this._fn(arg).then(this._handleResult.bind(this));
25+
};
2726

28-
function handleResult (res) {
29-
//console.log('end', self._concurrency);
30-
self._concurrency--;
31-
if (self._waiters.length) {
32-
self._waiters.shift().resolve(res);
33-
} else {
34-
self._buf.push(res);
35-
}
36-
if (!self._args) {
37-
self._args = res;
38-
}
39-
if (self._buf.length < self._size) {
40-
while (self._concurrency < self._maxConcurrency) {
41-
startRequest();
42-
}
27+
PromiseStream.prototype._handleResult = function (res) {
28+
//console.log('end', self._concurrency);
29+
this._concurrency--;
30+
if (this._waiters.length) {
31+
this._waiters.shift().resolve(res);
32+
} else {
33+
this._buf.push(res);
34+
}
35+
if (!this._args) {
36+
this._args = res;
37+
}
38+
if (this._buf.length < this._size) {
39+
while (this._concurrency < this._maxConcurrency) {
40+
this._startRequest();
4341
}
44-
4542
}
43+
};
44+
45+
PromiseStream.prototype.next = function () {
46+
var self = this;
4647

47-
while (self._concurrency < self._maxConcurrency) {
48-
startRequest();
48+
while (this._concurrency < this._maxConcurrency) {
49+
this._startRequest();
4950
}
5051

51-
if (self._buf.length) {
52-
return Promise.resolve(self._buf.shift());
52+
if (this._buf.length) {
53+
return Promise.resolve(this._buf.shift());
5354
} else {
5455
return new Promise(function(resolve, reject) {
5556
self._waiters.push({

0 commit comments

Comments
 (0)