-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathsearch_response.js
57 lines (43 loc) · 1.19 KB
/
search_response.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
'use strict';
let _ = require('underscore');
let SearchResponseStream = require('./search_response_stream').SearchResponseStream;
class SearchResponse {
constructor(pagingFunction, results) {
if (pagingFunction != null) {
this.setPagingFunction(pagingFunction);
}
if (results != null) {
this.setResponse(results);
}
this.stream = new SearchResponseStream(this);
this.success = true;
}
each(callback) {
return _.each(_.range(0, this.ids.length, this.pageSize), offset => {
return this.pagingFunction(this.ids.slice(offset, offset + this.pageSize), callback);
});
}
first(callback) {
if (this.ids.length === 0) {
return callback(null, null);
}
return this.pagingFunction([this.ids[0]], callback);
}
length() {
return this.ids.length;
}
ready() {
return this.stream.ready();
}
setFatalError(error) {
this.fatalError = error;
}
setResponse(results) {
this.ids = results.searchResults.ids;
this.pageSize = parseInt(results.searchResults.pageSize, 10);
}
setPagingFunction(pagingFunction) {
this.pagingFunction = pagingFunction;
}
}
module.exports = {SearchResponse: SearchResponse};