qiscus-sdk-core 2.14.1 → 2.14.2

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.
package/lib/index.js CHANGED
@@ -475,9 +475,12 @@ var QiscusSDK = /*#__PURE__*/function () {
475
475
  return _this2._retrySendComment(m);
476
476
  });
477
477
  });
478
+ this.syncAdapter.on('last-message-id.new', function (id) {
479
+ _this2.last_received_comment_id = id;
480
+ });
478
481
  this.customEventAdapter = (0, _customEvent["default"])(this.realtimeAdapter, this.user_id);
479
482
  this.setEventListeners();
480
- case 56:
483
+ case 57:
481
484
  case "end":
482
485
  return _context3.stop();
483
486
  }
@@ -661,6 +664,10 @@ var QiscusSDK = /*#__PURE__*/function () {
661
664
  });
662
665
  _this3.HTTPAdapter.setToken(_this3.userData.token);
663
666
  var user = response.user;
667
+ _this3._delayedSync = (0, _utils.delayed)(function () {
668
+ _this3.synchronize();
669
+ _this3.synchronizeEvent();
670
+ }, 500);
664
671
  _this3.expiredTokenAdapter = new _expiredToken.ExpiredTokenAdapter({
665
672
  httpAdapter: _this3.HTTPAdapter,
666
673
  refreshToken: user.refresh_token,
@@ -678,6 +685,7 @@ var QiscusSDK = /*#__PURE__*/function () {
678
685
  });
679
686
  _this3.realtimeAdapter.unsusbcribeUserChannelByToken(oldToken);
680
687
  _this3.realtimeAdapter.subscribeUserChannelByToken(token);
688
+ _this3._delayedSync();
681
689
  },
682
690
  getAuthenticationStatus: function getAuthenticationStatus() {
683
691
  return _this3.user_id != null && _this3.isLogin;
@@ -11,6 +11,9 @@ var _awaitAsyncGenerator2 = _interopRequireDefault(require("@babel/runtime/helpe
11
11
  var _wrapAsyncGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/wrapAsyncGenerator"));
12
12
  var _mitt = _interopRequireDefault(require("mitt"));
13
13
  var _urlBuilder = _interopRequireDefault(require("../url-builder"));
14
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
15
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
16
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
14
17
  function _asyncIterator(r) { var n, t, o, e = 2; for ("undefined" != typeof Symbol && (t = Symbol.asyncIterator, o = Symbol.iterator); e--;) { if (t && null != (n = r[t])) return n.call(r); if (o && null != (n = r[o])) return new AsyncFromSyncIterator(n.call(r)); t = "@@asyncIterator", o = "@@iterator"; } throw new TypeError("Object is not async iterable"); }
15
18
  function AsyncFromSyncIterator(r) { function AsyncFromSyncIteratorContinuation(r) { if (Object(r) !== r) return Promise.reject(new TypeError(r + " is not an object.")); var n = r.done; return Promise.resolve(r.value).then(function (r) { return { value: r, done: n }; }); } return AsyncFromSyncIterator = function AsyncFromSyncIterator(r) { this.s = r, this.n = r.next; }, AsyncFromSyncIterator.prototype = { s: null, n: null, next: function next() { return AsyncFromSyncIteratorContinuation(this.n.apply(this.s, arguments)); }, "return": function _return(r) { var n = this.s["return"]; return void 0 === n ? Promise.resolve({ value: r, done: !0 }) : AsyncFromSyncIteratorContinuation(n.apply(this.s, arguments)); }, "throw": function _throw(r) { var n = this.s["return"]; return void 0 === n ? Promise.reject(r) : AsyncFromSyncIteratorContinuation(n.apply(this.s, arguments)); } }, new AsyncFromSyncIterator(r); }
16
19
  var noop = function noop() {};
@@ -417,10 +420,76 @@ function SyncAdapter(getHttpAdapter, _ref) {
417
420
  return enableSync();
418
421
  },
419
422
  synchronize: function synchronize() {
420
- syncFactory.synchronize();
423
+ return (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee5() {
424
+ var _result$messages, _result$lastMessageId;
425
+ var id, result, messages, lastMessageId, _iterator3, _step3, message;
426
+ return _regenerator["default"].wrap(function _callee5$(_context5) {
427
+ while (1) switch (_context5.prev = _context5.next) {
428
+ case 0:
429
+ id = lastCommentId();
430
+ _context5.next = 3;
431
+ return syncFactory.synchronize(id);
432
+ case 3:
433
+ result = _context5.sent;
434
+ messages = (_result$messages = result === null || result === void 0 ? void 0 : result.messages) !== null && _result$messages !== void 0 ? _result$messages : [];
435
+ lastMessageId = (_result$lastMessageId = result === null || result === void 0 ? void 0 : result.lastMessageId) !== null && _result$lastMessageId !== void 0 ? _result$lastMessageId : -1;
436
+ _iterator3 = _createForOfIteratorHelper(messages);
437
+ try {
438
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
439
+ message = _step3.value;
440
+ emitter.emit('message.new', message);
441
+ }
442
+ } catch (err) {
443
+ _iterator3.e(err);
444
+ } finally {
445
+ _iterator3.f();
446
+ }
447
+ if (lastMessageId > 0) {
448
+ emitter.emit('last-message-id.new', lastMessageId);
449
+ }
450
+ case 9:
451
+ case "end":
452
+ return _context5.stop();
453
+ }
454
+ }, _callee5);
455
+ }))();
421
456
  },
422
457
  synchronizeEvent: function synchronizeEvent() {
423
- syncEventFactory.synchronize();
458
+ return (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee6() {
459
+ var result, eventId;
460
+ return _regenerator["default"].wrap(function _callee6$(_context6) {
461
+ while (1) switch (_context6.prev = _context6.next) {
462
+ case 0:
463
+ _context6.next = 2;
464
+ return syncEventFactory.synchronize(lastEventId);
465
+ case 2:
466
+ result = _context6.sent;
467
+ try {
468
+ eventId = result.lastId;
469
+ if (eventId > getId()) {
470
+ emitter.emit('last-event-id.new', eventId);
471
+ result.messageDelivered.forEach(function (it) {
472
+ return emitter.emit('message.delivered', it);
473
+ });
474
+ result.messageDeleted.forEach(function (it) {
475
+ return emitter.emit('message.deleted', it);
476
+ });
477
+ result.messageRead.forEach(function (it) {
478
+ return emitter.emit('message.read', it);
479
+ });
480
+ result.roomCleared.forEach(function (it) {
481
+ return emitter.emit('room.cleared', it);
482
+ });
483
+ }
484
+ } catch (e) {
485
+ logger('error when sync event', e.message);
486
+ }
487
+ case 4:
488
+ case "end":
489
+ return _context6.stop();
490
+ }
491
+ }, _callee6);
492
+ }))();
424
493
  }
425
494
  };
426
495
  }
package/lib/lib/utils.js CHANGED
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
7
  exports.GroupChatBuilder = void 0;
8
+ exports.delayed = delayed;
8
9
  exports.escapeHTML = escapeHTML;
9
10
  exports.scrollToBottom = scrollToBottom;
10
11
  exports.searchAndReplace = searchAndReplace;
@@ -106,4 +107,17 @@ function scrollToBottom(latestCommentId) {
106
107
  // on entering the room, wait for data processed then focus on comment form
107
108
  document.getElementsByClassName('qcw-comment-form').item(0).getElementsByTagName('textarea').item(0).focus();
108
109
  });
110
+ }
111
+ function delayed(cb, timeout) {
112
+ var timer = null;
113
+ return function () {
114
+ for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
115
+ args[_key2] = arguments[_key2];
116
+ }
117
+ if (timer != null) clearTimeout(timer);
118
+ timer = setTimeout(function () {
119
+ cb.apply(void 0, args);
120
+ timer = null;
121
+ }, timeout);
122
+ };
109
123
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qiscus-sdk-core",
3
- "version": "2.14.1",
3
+ "version": "2.14.2",
4
4
  "description": "Qiscus Web SDK Core",
5
5
  "license": "MIT",
6
6
  "main": "lib/index.js",