forked from intercom/intercom-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscroll.js
More file actions
49 lines (46 loc) · 1.34 KB
/
Copy pathscroll.js
File metadata and controls
49 lines (46 loc) · 1.34 KB
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
import Bluebird from 'bluebird';
export default class Scroll {
constructor(client, dataType) {
this.client = client;
this.dataType = dataType;
}
each(params, f) {
var self = this;
this.scroll_param = undefined;
return new Bluebird(function (resolve, reject) {
self.eachInternal(params, f, { resolve, reject });
});
}
eachInternal(params, f, promise) {
var self = this;
this.client.get(this.scrollUrl(), params)
.then(function (response) {
var result = f(response);
if (response.body[`${self.dataType}s`].length > 0) {
self.scroll_param = response.body.scroll_param;
if (result && ('then' in result) && typeof result.then === 'function') {
result.then(function () {
self.eachInternal(params, f, promise);
}, function (error) {
promise.reject(error);
});
} else {
self.eachInternal(params, f, promise);
}
} else {
promise.resolve();
}
})
.catch(function (error) {
promise.reject(error);
});
}
scrollUrl() {
const dataType = this.dataType;
if (typeof this.scroll_param !== 'undefined') {
return `/${dataType}s/scroll?scroll_param=${this.scroll_param}`;
} else {
return `/${dataType}s/scroll`;
}
}
}