-
Notifications
You must be signed in to change notification settings - Fork 1
/
activity-stream.js
38 lines (34 loc) · 993 Bytes
/
activity-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
var Activity = require('./activity');
var Readable = require('stream').Readable;
var util = require('util');
/**
* Readable stream that emits event objects from the github events api for a
* given user
*/
function ActivityStream() {
this.eventsPromise = Activity.get();
this.eventIndex = 0;
Readable.call(this, {objectMode:true});
}
util.inherits(ActivityStream, Readable);
ActivityStream.prototype._read = function () {
pushNextEvent(this);
};
function pushNextEvent(stream) {
stream.eventsPromise.done(function(events) {
if (!events) {
return stream.push(null);
}
// If we're at the end of the current page of events,
// load some more.
if (!events[stream.eventIndex]) {
stream.eventsPromise = events.nextPage();
stream.eventIndex = 0;
pushNextEvent(stream);
} else {
stream.push(events[stream.eventIndex]);
stream.eventIndex++;
}
});
}
module.exports = ActivityStream;