-
Notifications
You must be signed in to change notification settings - Fork 0
/
BatchImpl.js
executable file
·65 lines (59 loc) · 1.9 KB
/
BatchImpl.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
/**
* Created by Administrator on 2015/5/13.
*/
var GtReq = require('./getui/GtReq');
var GtConfig = require('./GtConfig');
var utils = require('./getui/utils');
class BatchImpl {
constructor(appKey, push) {
this.batchId = utils.uuid();
this.appKey = appKey;
this.push = push;
this.seqId = 0;
this.innerMsgList = new Array();
this.lastPostData = null;
}
getBatchId() {
return this.batchId;
};
add(message, target) {
if (this.seqId >= 5000) {
throw new Error("Can not add over 5000 message once! Please call submit() first.");
} else {
var json = utils.createPostParams(message, target, null, this.appKey);
var item = new GtReq.SingleBatchItem({
'seqId': this.seqId,
'data': JSON.stringify(json)
});
this.innerMsgList.push(item);
this.seqId += 1;
}
return this.seqId;
};
submit(callback) {
var requestId = utils.uuid();
var data = {
'requestId': requestId,
'appkey': this.appKey,
'action': 'pushMessageToSingleBatchAction',
'serialize': 'pb',
'async': GtConfig.isPushSingleBatchAsync()
};
try {
var request = new GtReq.SingleBatchRequest({
'batchId': this.batchId,
'batchItem': this.innerMsgList
});
data.singleDatas = request.toBase64();
this.push.httpPostJson(this.push._host, data, true, callback);
} finally {
this.seqId = 0;
this.innerMsgList = [];
this.lastPostData = data;
}
};
retry(callback) {
this.push.httpPostJson(this.push._host, this.lastPostData, true, callback);
};
}
module.exports = BatchImpl;