1 | (function (factory) {
|
2 | if (typeof module === "object" && typeof module.exports === "object") {
|
3 | var v = factory(require, exports);
|
4 | if (v !== undefined) module.exports = v;
|
5 | }
|
6 | else if (typeof define === "function" && define.amd) {
|
7 | define(["require", "exports", "tslib", "@theintern/common", "./Base", "../common/util"], factory);
|
8 | }
|
9 | })(function (require, exports) {
|
10 | "use strict";
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | var tslib_1 = require("tslib");
|
13 | var common_1 = require("@theintern/common");
|
14 | var Base_1 = tslib_1.__importDefault(require("./Base"));
|
15 | var util_1 = require("../common/util");
|
16 | var HttpChannel = (function (_super) {
|
17 | tslib_1.__extends(HttpChannel, _super);
|
18 | function HttpChannel(options) {
|
19 | var _this = _super.call(this, options) || this;
|
20 | _this._sequence = 1;
|
21 | _this._maxPostSize = options.maxPostSize || 100000;
|
22 | _this._messageBuffer = [];
|
23 | _this._lastRequest = common_1.Task.resolve();
|
24 | return _this;
|
25 | }
|
26 | HttpChannel.prototype._sendData = function (name, data) {
|
27 | var _this = this;
|
28 | var id = String(this._sequence++);
|
29 | var sessionId = this.sessionId;
|
30 | var message = { id: id, sessionId: sessionId, name: name, data: data };
|
31 | var task = new common_1.Task(function (resolve, reject) {
|
32 | _this._messageBuffer.push({
|
33 | message: util_1.stringify(message),
|
34 | resolve: resolve,
|
35 | reject: reject,
|
36 | });
|
37 | if (_this._activeRequest) {
|
38 | _this._activeRequest.then(function () { return _this._sendMessages(); });
|
39 | }
|
40 | else {
|
41 | _this._sendMessages();
|
42 | }
|
43 | }, function () {
|
44 | if (_this._activeRequest) {
|
45 | _this._activeRequest.cancel();
|
46 | }
|
47 | _this._messageBuffer = [];
|
48 | });
|
49 | return task;
|
50 | };
|
51 | HttpChannel.prototype._sendMessages = function () {
|
52 | var _this = this;
|
53 | var messages = this._messageBuffer;
|
54 | if (messages.length === 0) {
|
55 | return;
|
56 | }
|
57 | var block = [messages.shift()];
|
58 | var size = block[0].message.length;
|
59 | while (messages.length > 0 &&
|
60 | size + messages[0].message.length < this._maxPostSize) {
|
61 | size += messages[0].message.length;
|
62 | block.push(messages.shift());
|
63 | }
|
64 | this._activeRequest = common_1.request(this.url, {
|
65 | method: 'post',
|
66 | headers: { 'Content-Type': 'application/json' },
|
67 | data: block.map(function (entry) { return entry.message; }),
|
68 | })
|
69 | .then(function (response) {
|
70 | if (response.status === 200) {
|
71 | return response.json();
|
72 | }
|
73 | else if (response.status === 204) {
|
74 | return [];
|
75 | }
|
76 | else {
|
77 | throw new Error(response.statusText);
|
78 | }
|
79 | })
|
80 | .then(function (results) {
|
81 | block.forEach(function (entry, index) {
|
82 | entry.resolve(results[index]);
|
83 | });
|
84 | })
|
85 | .catch(function (error) {
|
86 | block.forEach(function (entry) {
|
87 | entry.reject(error);
|
88 | });
|
89 | })
|
90 | .finally(function () {
|
91 | _this._activeRequest = undefined;
|
92 | if (messages.length > 0) {
|
93 | return _this._sendMessages();
|
94 | }
|
95 | });
|
96 | return this._activeRequest;
|
97 | };
|
98 | return HttpChannel;
|
99 | }(Base_1.default));
|
100 | exports.default = HttpChannel;
|
101 | });
|
102 |
|
\ | No newline at end of file |