-
Notifications
You must be signed in to change notification settings - Fork 0
/
issue-comment-stream.js
79 lines (57 loc) · 1.54 KB
/
issue-comment-stream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
module.exports = IssueCommentStream;
var assert = require('assert');
var inherits = require('util').inherits;
var Readable = require('stream').Readable;
function IssueCommentStream(client, issue) {
Readable.call(this, { objectMode: true });
this._client = client;
this._issue = issue;
this._queue = [];
this._fetching = false;
this._ended = false;
this._page = 1;
this._perPage = 64;
this._retryDelay = 60 * 1000;
}
inherits(IssueCommentStream, Readable);
IssueCommentStream.prototype._read = function() {
var next = this._queue.shift();
if (next)
return this.push(next);
if (this._ended)
return this.push(null);
if (this._fetching)
return;
this._fetch();
};
IssueCommentStream.prototype._fetch = function() {
assert(!this._fetching);
this._fetching = true;
var issue = this._issue;
var repo = issue.repository;
var query = {
user: repo.owner.login,
repo: repo.name,
number: issue.number,
page: this._page,
per_page: this._perPage,
sort: 'created',
direction: 'asc'
};
this._client.issues.getComments(query, this._onComments.bind(this));
};
IssueCommentStream.prototype._onComments = function(err, comments) {
if (err)
return setTimeout(this._fetch.bind(this), this._retryDelay);
assert(this._fetching);
this._fetching = false;
assert(!this._queue.length);
assert(!this._ended);
this._queue = comments;
if (comments.length < this._perPage)
this._ended = true;
else
this._page++;
var next = comments.shift() || null;
this.push(next);
};