qiscus-sdk-core 2.13.2 → 2.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -60,83 +60,68 @@ var ExpiredTokenAdapter = exports.ExpiredTokenAdapter = /*#__PURE__*/function ()
60
60
  this._expiredAt = new Date(expiredAt);
61
61
  }
62
62
  this._isExpiredTokenEnabled = this._refreshToken != null && this._expiredAt != null;
63
-
64
- // this._timerId = setInterval(this._checkToken, 1000)
65
- this._checkToken();
63
+ this._setTimer(this._expiredAt);
66
64
  }
65
+
66
+ /**
67
+ * @param {Date | null} expiredAt
68
+ *
69
+ * Sets a timer to refresh the authentication token when it expires.
70
+ * If the token is already expired, it will immediately trigger the refresh.
71
+ */
67
72
  return (0, _createClass2["default"])(ExpiredTokenAdapter, [{
68
- key: "_checkToken",
73
+ key: "_setTimer",
74
+ value: function _setTimer(expiredAt) {
75
+ var _expiredAt$getTime,
76
+ _this = this;
77
+ if (this._timerId != null) {
78
+ clearTimeout(this._timerId);
79
+ this._timerId = null;
80
+ }
81
+ var delay = Math.floor(((_expiredAt$getTime = expiredAt === null || expiredAt === void 0 ? void 0 : expiredAt.getTime()) !== null && _expiredAt$getTime !== void 0 ? _expiredAt$getTime : NaN) - Date.now());
82
+ if (!isNaN(delay) && delay > 0) {
83
+ this._timerId = setTimeout(function () {
84
+ _this.refreshAuthToken();
85
+ }, delay);
86
+ }
87
+ }
88
+ }, {
89
+ key: "refreshAuthToken",
69
90
  value: function () {
70
- var _checkToken2 = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee() {
71
- var timeToSleep, now, diff;
91
+ var _refreshAuthToken = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee() {
92
+ var _this2 = this;
72
93
  return _regenerator["default"].wrap(function _callee$(_context) {
73
94
  while (1) switch (_context.prev = _context.next) {
74
95
  case 0:
75
- timeToSleep = 5000; // 5 seconds
76
96
  if (!(this._getAuthenticationStatus() == false || this._refreshToken == null)) {
77
- _context.next = 3;
97
+ _context.next = 2;
78
98
  break;
79
99
  }
80
100
  return _context.abrupt("return");
81
- case 3:
82
- if (!(this._expiredAt != null && this._isExpiredTokenEnabled)) {
83
- _context.next = 9;
84
- break;
85
- }
86
- now = Date.now(); // @ts-ignore
87
- diff = Math.floor((this._expiredAt - now) / 1000); // console.log('diff', diff)
88
- if (!(diff < timeToSleep / 1000)) {
89
- _context.next = 9;
90
- break;
91
- }
92
- _context.next = 9;
93
- return this.refreshAuthToken();
94
- case 9:
95
- _context.next = 11;
96
- return (0, _util.sleep)(timeToSleep);
97
- case 11:
98
- this._checkToken();
99
- case 12:
100
- case "end":
101
- return _context.stop();
102
- }
103
- }, _callee, this);
104
- }));
105
- function _checkToken() {
106
- return _checkToken2.apply(this, arguments);
107
- }
108
- return _checkToken;
109
- }()
110
- }, {
111
- key: "refreshAuthToken",
112
- value: function () {
113
- var _refreshAuthToken = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2() {
114
- var _this = this;
115
- return _regenerator["default"].wrap(function _callee2$(_context2) {
116
- while (1) switch (_context2.prev = _context2.next) {
117
- case 0:
118
- return _context2.abrupt("return", this._http.post('api/v2/sdk/refresh_user_token', {
101
+ case 2:
102
+ return _context.abrupt("return", this._http.post('api/v2/sdk/refresh_user_token', {
119
103
  user_id: this._userId,
120
104
  refresh_token: this._refreshToken
121
105
  }).then(function (r) {
122
- var _this$_onTokenRefresh;
106
+ var _this2$_onTokenRefres;
123
107
  var res = r.body.results;
124
108
  var token = res.token;
125
- _this._refreshToken = res.refresh_token;
126
- _this._http.setToken(res.token);
109
+ _this2._refreshToken = res.refresh_token;
110
+ _this2._http.setToken(res.token);
127
111
  if (res.token_expires_at != null) {
128
- _this._expiredAt = new Date(res.token_expires_at);
112
+ _this2._expiredAt = new Date(res.token_expires_at);
129
113
  }
130
114
 
131
115
  // @ts-ignore
132
- (_this$_onTokenRefresh = _this._onTokenRefreshed) === null || _this$_onTokenRefresh === void 0 || _this$_onTokenRefresh.call(_this, token, _this._refreshToken, _this._expiredAt);
116
+ (_this2$_onTokenRefres = _this2._onTokenRefreshed) === null || _this2$_onTokenRefres === void 0 || _this2$_onTokenRefres.call(_this2, token, _this2._refreshToken, _this2._expiredAt);
117
+ _this2._setTimer(_this2._expiredAt);
133
118
  return res;
134
119
  }));
135
- case 1:
120
+ case 3:
136
121
  case "end":
137
- return _context2.stop();
122
+ return _context.stop();
138
123
  }
139
- }, _callee2, this);
124
+ }, _callee, this);
140
125
  }));
141
126
  function refreshAuthToken() {
142
127
  return _refreshAuthToken.apply(this, arguments);
@@ -146,19 +131,19 @@ var ExpiredTokenAdapter = exports.ExpiredTokenAdapter = /*#__PURE__*/function ()
146
131
  }, {
147
132
  key: "logout",
148
133
  value: function () {
149
- var _logout = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee3() {
150
- return _regenerator["default"].wrap(function _callee3$(_context3) {
151
- while (1) switch (_context3.prev = _context3.next) {
134
+ var _logout = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2() {
135
+ return _regenerator["default"].wrap(function _callee2$(_context2) {
136
+ while (1) switch (_context2.prev = _context2.next) {
152
137
  case 0:
153
- return _context3.abrupt("return", this._http.post('api/v2/sdk/logout', {
138
+ return _context2.abrupt("return", this._http.post('api/v2/sdk/logout', {
154
139
  user_id: this._userId,
155
140
  token: this._http.token
156
141
  }));
157
142
  case 1:
158
143
  case "end":
159
- return _context3.stop();
144
+ return _context2.stop();
160
145
  }
161
- }, _callee3, this);
146
+ }, _callee2, this);
162
147
  }));
163
148
  function logout() {
164
149
  return _logout.apply(this, arguments);
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
7
  exports["default"] = void 0;
8
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
8
10
  var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
9
11
  var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
10
12
  var _superagent = _interopRequireDefault(require("superagent"));
@@ -14,7 +16,8 @@ var HttpAdapter = exports["default"] = /*#__PURE__*/function () {
14
16
  AppId = _ref.AppId,
15
17
  userId = _ref.userId,
16
18
  version = _ref.version,
17
- getCustomHeader = _ref.getCustomHeader;
19
+ getCustomHeader = _ref.getCustomHeader,
20
+ expiredTokenAdapterGetter = _ref.expiredTokenAdapterGetter;
18
21
  (0, _classCallCheck2["default"])(this, HttpAdapter);
19
22
  this.baseURL = baseURL;
20
23
  this.token = null;
@@ -22,6 +25,8 @@ var HttpAdapter = exports["default"] = /*#__PURE__*/function () {
22
25
  this.AppId = AppId;
23
26
  this.version = version;
24
27
  this.getCustomHeader = getCustomHeader;
28
+ /** @type {() => import('./expired-token.js').ExpiredTokenAdapter} */
29
+ this._expiredTokenAdapterGetter = expiredTokenAdapterGetter;
25
30
  }
26
31
  return (0, _createClass2["default"])(HttpAdapter, [{
27
32
  key: "setToken",
@@ -34,135 +39,167 @@ var HttpAdapter = exports["default"] = /*#__PURE__*/function () {
34
39
  var _this = this;
35
40
  var headers = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
36
41
  var options = arguments.length > 2 ? arguments[2] : undefined;
37
- return new Promise(function (resolve, reject) {
38
- var req = _superagent["default"].get("".concat(_this.baseURL, "/").concat(path));
39
- if (options && options.baseURL) req = _superagent["default"].get("".concat(options.baseURL, "/").concat(path));
40
- req = _this.setupHeaders(req, headers);
41
- req.end(function (err, res) {
42
- if (err) {
43
- _this._rethrowIfExpiredToken(err);
44
- return reject(err);
42
+ return this._retryHelper(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee() {
43
+ var req;
44
+ return _regenerator["default"].wrap(function _callee$(_context) {
45
+ while (1) switch (_context.prev = _context.next) {
46
+ case 0:
47
+ req = _superagent["default"].get("".concat(_this.baseURL, "/").concat(path));
48
+ if (options && options.baseURL) req = _superagent["default"].get("".concat(options.baseURL, "/").concat(path));
49
+ req = _this.setupHeaders(req, headers);
50
+ return _context.abrupt("return", req);
51
+ case 4:
52
+ case "end":
53
+ return _context.stop();
45
54
  }
46
- return resolve(res);
47
- });
48
- });
55
+ }, _callee);
56
+ })));
49
57
  }
50
58
  // eslint-disable-next-line
51
59
  }, {
52
60
  key: "get_request",
53
61
  value: function get_request(path) {
54
- var req = _superagent["default"].get("".concat(this.baseURL, "/").concat(path));
55
- req = this.setupHeaders(req, {});
56
- return req;
62
+ var _this2 = this;
63
+ return this._retryHelper(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2() {
64
+ var req;
65
+ return _regenerator["default"].wrap(function _callee2$(_context2) {
66
+ while (1) switch (_context2.prev = _context2.next) {
67
+ case 0:
68
+ req = _superagent["default"].get("".concat(_this2.baseURL, "/").concat(path));
69
+ req = _this2.setupHeaders(req, {});
70
+ return _context2.abrupt("return", req);
71
+ case 3:
72
+ case "end":
73
+ return _context2.stop();
74
+ }
75
+ }, _callee2);
76
+ })));
57
77
  }
58
78
  }, {
59
79
  key: "post",
60
80
  value: function post(path) {
61
- var _this2 = this;
81
+ var _this3 = this;
62
82
  var body = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
63
83
  var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
64
- return new Promise(function (resolve, reject) {
65
- var req = _superagent["default"].post("".concat(_this2.baseURL, "/").concat(path));
66
- req = _this2.setupHeaders(req, headers);
67
- req.send(body).set("Content-Type", "application/x-www-form-urlencoded").end(function (err, res) {
68
- if (err) {
69
- _this2._rethrowIfExpiredToken(err);
70
- return reject(err);
84
+ return this._retryHelper(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee3() {
85
+ var req;
86
+ return _regenerator["default"].wrap(function _callee3$(_context3) {
87
+ while (1) switch (_context3.prev = _context3.next) {
88
+ case 0:
89
+ req = _superagent["default"].post("".concat(_this3.baseURL, "/").concat(path));
90
+ req = _this3.setupHeaders(req, headers);
91
+ return _context3.abrupt("return", req.send(body).set('Content-Type', 'application/x-www-form-urlencoded'));
92
+ case 3:
93
+ case "end":
94
+ return _context3.stop();
71
95
  }
72
- return resolve(res);
73
- });
74
- });
96
+ }, _callee3);
97
+ })));
75
98
  }
76
99
 
77
100
  // eslint-disable-next-line
78
101
  }, {
79
102
  key: "post_json",
80
103
  value: function post_json(path) {
81
- var _this3 = this;
104
+ var _this4 = this;
82
105
  var body = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
83
106
  var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
84
- return new Promise(function (resolve, reject) {
85
- var req = _superagent["default"].post("".concat(_this3.baseURL, "/").concat(path));
86
- req = _this3.setupHeaders(req, headers);
87
- req.send(body).set("Content-Type", "application/json").end(function (err, res) {
88
- if (err) {
89
- _this3._rethrowIfExpiredToken(err);
90
- return reject(err);
107
+ return this._retryHelper(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee4() {
108
+ var req;
109
+ return _regenerator["default"].wrap(function _callee4$(_context4) {
110
+ while (1) switch (_context4.prev = _context4.next) {
111
+ case 0:
112
+ req = _superagent["default"].post("".concat(_this4.baseURL, "/").concat(path));
113
+ req = _this4.setupHeaders(req, headers);
114
+ req.send(body);
115
+ return _context4.abrupt("return", req);
116
+ case 4:
117
+ case "end":
118
+ return _context4.stop();
91
119
  }
92
- return resolve(res);
93
- });
94
- });
120
+ }, _callee4);
121
+ })));
95
122
  }
96
123
  }, {
97
124
  key: "put",
98
125
  value: function put(path) {
99
- var _this4 = this;
126
+ var _this5 = this;
100
127
  var body = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
101
128
  var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
102
- return new Promise(function (resolve, reject) {
103
- var req = _superagent["default"].put("".concat(_this4.baseURL, "/").concat(path));
104
- req = _this4.setupHeaders(req, headers);
105
- req.send(body).set("Content-Type", "application/x-www-form-urlencoded").end(function (err, res) {
106
- if (err) {
107
- _this4._rethrowIfExpiredToken(err);
108
- return reject(err);
129
+ return this._retryHelper(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee5() {
130
+ var req;
131
+ return _regenerator["default"].wrap(function _callee5$(_context5) {
132
+ while (1) switch (_context5.prev = _context5.next) {
133
+ case 0:
134
+ req = _superagent["default"].put("".concat(_this5.baseURL, "/").concat(path));
135
+ req = _this5.setupHeaders(req, headers);
136
+ return _context5.abrupt("return", req.send(body).set('Content-Type', 'application/x-www-form-urlencoded'));
137
+ case 3:
138
+ case "end":
139
+ return _context5.stop();
109
140
  }
110
- return resolve(res);
111
- });
112
- });
141
+ }, _callee5);
142
+ })));
113
143
  }
114
144
  }, {
115
145
  key: "patch",
116
146
  value: function patch(path) {
117
- var _this5 = this;
147
+ var _this6 = this;
118
148
  var body = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
119
149
  var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
120
- return new Promise(function (resolve, reject) {
121
- var req = _superagent["default"].patch("".concat(_this5.baseURL, "/").concat(path));
122
- req = _this5.setupHeaders(req, headers);
123
- req.send(body).set("Content-Type", "application/x-www-form-urlencoded").end(function (err, res) {
124
- if (err) {
125
- _this5._rethrowIfExpiredToken(err);
126
- return reject(err);
150
+ return this._retryHelper(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee6() {
151
+ var req;
152
+ return _regenerator["default"].wrap(function _callee6$(_context6) {
153
+ while (1) switch (_context6.prev = _context6.next) {
154
+ case 0:
155
+ req = _superagent["default"].patch("".concat(_this6.baseURL, "/").concat(path));
156
+ req = _this6.setupHeaders(req, headers);
157
+ return _context6.abrupt("return", req.send(body).set('Content-Type', 'application/x-www-form-urlencoded'));
158
+ case 3:
159
+ case "end":
160
+ return _context6.stop();
127
161
  }
128
- return resolve(res);
129
- });
130
- });
162
+ }, _callee6);
163
+ })));
131
164
  }
132
165
  }, {
133
166
  key: "del",
134
167
  value: function del(path) {
135
- var _this6 = this;
168
+ var _this7 = this;
136
169
  var body = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
137
170
  var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
138
- return new Promise(function (resolve, reject) {
139
- var req = _superagent["default"].del("".concat(_this6.baseURL, "/").concat(path));
140
- req = _this6.setupHeaders(req, headers);
141
- req.send(body).set("Content-Type", "application/json").end(function (err, res) {
142
- if (err) {
143
- _this6._rethrowIfExpiredToken(err);
144
- return reject(err);
171
+ return this._retryHelper(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee7() {
172
+ var req;
173
+ return _regenerator["default"].wrap(function _callee7$(_context7) {
174
+ while (1) switch (_context7.prev = _context7.next) {
175
+ case 0:
176
+ req = _superagent["default"].del("".concat(_this7.baseURL, "/").concat(path));
177
+ req = _this7.setupHeaders(req, headers);
178
+ return _context7.abrupt("return", req.send(body).set('Content-Type', 'application/json'));
179
+ case 3:
180
+ case "end":
181
+ return _context7.stop();
145
182
  }
146
- return resolve(res);
147
- });
148
- });
183
+ }, _callee7);
184
+ })));
149
185
  }
150
186
  }, {
151
187
  key: "setupHeaders",
152
188
  value: function setupHeaders(req) {
153
189
  var headers = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
190
+ req.set('QISCUS-SDK-PLATFORM', 'javascript');
154
191
  // let's give this default Authorization Header
155
192
  if (this.AppId != null) {
156
- req.set("QISCUS-SDK-APP-ID", "".concat(this.AppId));
193
+ req.set('QISCUS-SDK-APP-ID', "".concat(this.AppId));
157
194
  }
158
195
  if (this.userId != null) {
159
- req.set("QISCUS-SDK-USER-ID", "".concat(this.userId));
196
+ req.set('QISCUS-SDK-USER-ID', "".concat(this.userId));
160
197
  }
161
198
  if (this.token != null) {
162
- req.set("QISCUS-SDK-TOKEN", "".concat(this.token));
199
+ req.set('QISCUS-SDK-TOKEN', "".concat(this.token));
163
200
  }
164
201
  if (this.version != null) {
165
- req.set("QISCUS-SDK-VERSION", "".concat(this.version));
202
+ req.set('QISCUS-SDK-VERSION', "".concat(this.version));
166
203
  }
167
204
  if (this.getCustomHeader != null) {
168
205
  var customHeaders = this.getCustomHeader();
@@ -180,16 +217,53 @@ var HttpAdapter = exports["default"] = /*#__PURE__*/function () {
180
217
  }
181
218
  return req;
182
219
  }
220
+
221
+ /**
222
+ * @param {() => request.Request} fn
223
+ * @returns {Promise<unknown>}
224
+ */
183
225
  }, {
184
- key: "_rethrowIfExpiredToken",
185
- value: function _rethrowIfExpiredToken(err) {
186
- var _err$response, _err$response2, _body$error;
187
- var status = (_err$response = err.response) === null || _err$response === void 0 ? void 0 : _err$response.status;
188
- var body = (_err$response2 = err.response) === null || _err$response2 === void 0 ? void 0 : _err$response2.body;
189
- if (status === 403 && ((_body$error = body.error) === null || _body$error === void 0 || (_body$error = _body$error.message) === null || _body$error === void 0 ? void 0 : _body$error.toLowerCase()) === 'unauthorized. token is expired') {
190
- throw new Error('Token expired');
226
+ key: "_retryHelper",
227
+ value: (function () {
228
+ var _retryHelper2 = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee8(fn) {
229
+ var _err$response, _err$response2, _body$error, status, body;
230
+ return _regenerator["default"].wrap(function _callee8$(_context8) {
231
+ while (1) switch (_context8.prev = _context8.next) {
232
+ case 0:
233
+ _context8.prev = 0;
234
+ _context8.next = 3;
235
+ return fn();
236
+ case 3:
237
+ return _context8.abrupt("return", _context8.sent);
238
+ case 6:
239
+ _context8.prev = 6;
240
+ _context8.t0 = _context8["catch"](0);
241
+ status = _context8.t0 === null || _context8.t0 === void 0 || (_err$response = _context8.t0.response) === null || _err$response === void 0 ? void 0 : _err$response.status;
242
+ body = _context8.t0 === null || _context8.t0 === void 0 || (_err$response2 = _context8.t0.response) === null || _err$response2 === void 0 ? void 0 : _err$response2.body;
243
+ if (!(status === 403 && ((_body$error = body.error) === null || _body$error === void 0 || (_body$error = _body$error.message) === null || _body$error === void 0 ? void 0 : _body$error.toLowerCase()) === 'unauthorized. token is expired')) {
244
+ _context8.next = 18;
245
+ break;
246
+ }
247
+ _context8.next = 13;
248
+ return this._expiredTokenAdapterGetter().refreshAuthToken();
249
+ case 13:
250
+ _context8.next = 15;
251
+ return fn();
252
+ case 15:
253
+ return _context8.abrupt("return", _context8.sent);
254
+ case 18:
255
+ throw _context8.t0;
256
+ case 19:
257
+ case "end":
258
+ return _context8.stop();
259
+ }
260
+ }, _callee8, this, [[0, 6]]);
261
+ }));
262
+ function _retryHelper(_x) {
263
+ return _retryHelper2.apply(this, arguments);
191
264
  }
192
- }
265
+ return _retryHelper;
266
+ }())
193
267
  }]);
194
268
  }();
195
269
  module.exports = exports.default;
@@ -232,7 +232,7 @@ var MqttAdapter = exports["default"] = /*#__PURE__*/function () {
232
232
  while (1) switch (_context2.prev = _context2.next) {
233
233
  case 0:
234
234
  this.shouldConnect = true;
235
- this.__mqtt_conneck();
235
+ this.mqtt = this.__mqtt_conneck();
236
236
  case 2:
237
237
  case "end":
238
238
  return _context2.stop();
@@ -252,12 +252,18 @@ var MqttAdapter = exports["default"] = /*#__PURE__*/function () {
252
252
  key: "closeConnection",
253
253
  value: (function () {
254
254
  var _closeConnection = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee3() {
255
+ var _this2 = this;
255
256
  return _regenerator["default"].wrap(function _callee3$(_context3) {
256
257
  while (1) switch (_context3.prev = _context3.next) {
257
258
  case 0:
258
259
  this.shouldConnect = false;
259
- this.mqtt.end(true);
260
- case 2:
260
+ this.mqtt.end(true, function (err) {
261
+ if (err) {
262
+ _this2.logger('error when close connection', err.message);
263
+ }
264
+ });
265
+ this.mqtt = null;
266
+ case 3:
261
267
  case "end":
262
268
  return _context3.stop();
263
269
  }
@@ -308,12 +314,12 @@ var MqttAdapter = exports["default"] = /*#__PURE__*/function () {
308
314
  }
309
315
  this.logger('subscribe to', args);
310
316
  this.subscribtionBuffer.push(args);
311
- if (this.mqtt != null) {
312
- do {
317
+ while (this.mqtt != null && this.subscribtionBuffer.length > 0) {
318
+ var subs = this.subscribtionBuffer.shift();
319
+ if (subs != null) {
313
320
  var _this$mqtt;
314
- var subs = this.subscribtionBuffer.shift();
315
- if (subs != null) (_this$mqtt = this.mqtt).subscribe.apply(_this$mqtt, args);
316
- } while (this.subscribtionBuffer.length > 0);
321
+ (_this$mqtt = this.mqtt).subscribe.apply(_this$mqtt, (0, _toConsumableArray2["default"])(subs));
322
+ }
317
323
  }
318
324
  }
319
325
  }, {
@@ -322,16 +328,13 @@ var MqttAdapter = exports["default"] = /*#__PURE__*/function () {
322
328
  for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
323
329
  args[_key3] = arguments[_key3];
324
330
  }
325
- this.logger('unsubscribe from', args);
326
331
  this.unsubscribtionBuffer.push(args);
327
- if (this.mqtt != null) {
328
- do {
329
- var subs = this.unsubscribtionBuffer.shift();
330
- if (subs != null) {
331
- var _this$mqtt2;
332
- (_this$mqtt2 = this.mqtt).unsubscribe.apply(_this$mqtt2, (0, _toConsumableArray2["default"])(subs));
333
- }
334
- } while (this.unsubscribtionBuffer.length > 0);
332
+ while (this.mqtt != null && this.unsubscribtionBuffer.length > 0) {
333
+ var subs = this.unsubscribtionBuffer.shift();
334
+ if (subs != null) {
335
+ var _this$mqtt2;
336
+ (_this$mqtt2 = this.mqtt).unsubscribe.apply(_this$mqtt2, (0, _toConsumableArray2["default"])(subs));
337
+ }
335
338
  }
336
339
  }
337
340
  }, {
@@ -343,12 +346,12 @@ var MqttAdapter = exports["default"] = /*#__PURE__*/function () {
343
346
  payload: payload,
344
347
  options: options
345
348
  });
346
- do {
349
+ while (this.mqtt != null && this.publishBuffer.length > 0) {
347
350
  var data = this.publishBuffer.shift();
348
351
  if (data != null) {
349
- return this.mqtt.publish(data.topic, data.payload.toString(), data.options);
352
+ this.mqtt.publish(data.topic, data.payload.toString(), data.options);
350
353
  }
351
- } while (this.publishBuffer.length > 0);
354
+ }
352
355
  }
353
356
  }, {
354
357
  key: "emit",
@@ -440,13 +443,13 @@ var MqttAdapter = exports["default"] = /*#__PURE__*/function () {
440
443
  }, {
441
444
  key: "notificationHandler",
442
445
  value: function notificationHandler(topic, message) {
443
- var _this2 = this;
446
+ var _this3 = this;
444
447
  this.logger('on:notification', message);
445
448
  message = JSON.parse(message);
446
449
  var data = message.payload.data;
447
450
  if ('deleted_messages' in data) {
448
451
  data.deleted_messages.forEach(function (message) {
449
- _this2.emit('comment-deleted', {
452
+ _this3.emit('comment-deleted', {
450
453
  roomId: message.room_id,
451
454
  commentUniqueIds: message.message_unique_ids,
452
455
  isForEveryone: true,
@@ -456,7 +459,7 @@ var MqttAdapter = exports["default"] = /*#__PURE__*/function () {
456
459
  }
457
460
  if ('deleted_rooms' in data) {
458
461
  data.deleted_rooms.forEach(function (room) {
459
- _this2.emit('room-cleared', room);
462
+ _this3.emit('room-cleared', room);
460
463
  });
461
464
  }
462
465
  }
@@ -108,16 +108,20 @@ function synchronizeFactory(getHttp, getInterval, getSync, getId, logger) {
108
108
  }
109
109
  result = _step.value;
110
110
  try {
111
- messageId = result.lastMessageId;
112
- messages = result.messages;
113
- if (messageId > getId()) {
114
- messages.forEach(function (m) {
115
- return emitter.emit('message.new', m);
116
- });
117
- emitter.emit('last-message-id.new', messageId);
111
+ emitter.emit('synchronize', Date.now());
112
+ if ((result === null || result === void 0 ? void 0 : result.lastMessageId) != null && (result === null || result === void 0 ? void 0 : result.messages) != null) {
113
+ messageId = result.lastMessageId;
114
+ messages = result.messages;
115
+ if (messageId > getId()) {
116
+ messages.forEach(function (m) {
117
+ return emitter.emit('message.new', m);
118
+ });
119
+ emitter.emit('last-message-id.new', messageId);
120
+ }
118
121
  }
119
122
  } catch (e) {
120
123
  logger('error when sync', e.message);
124
+ console.log('error when sync', e);
121
125
  }
122
126
  case 9:
123
127
  _iteratorAbruptCompletion = false;
@@ -369,6 +373,9 @@ function SyncAdapter(getHttpAdapter, _ref) {
369
373
  syncFactory.on('message.new', function (m) {
370
374
  return emitter.emit('message.new', m);
371
375
  });
376
+ syncFactory.on('synchronize', function (m) {
377
+ return emitter.emit('synchronize', m);
378
+ });
372
379
  syncFactory.run()["catch"](function (err) {
373
380
  return logger('got error when sync', err);
374
381
  });
@@ -403,6 +410,12 @@ function SyncAdapter(getHttpAdapter, _ref) {
403
410
  get off() {
404
411
  return emitter.off;
405
412
  },
413
+ get interval() {
414
+ return getInterval();
415
+ },
416
+ get enabled() {
417
+ return enableSync();
418
+ },
406
419
  synchronize: function synchronize() {
407
420
  syncFactory.synchronize();
408
421
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qiscus-sdk-core",
3
- "version": "2.13.2",
3
+ "version": "2.14.0",
4
4
  "description": "Qiscus Web SDK Core",
5
5
  "license": "MIT",
6
6
  "main": "lib/index.js",