qiscus-sdk-core 2.12.0 → 2.12.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/lib/Room.js CHANGED
@@ -7,6 +7,8 @@ Object.defineProperty(exports, "__esModule", {
7
7
  });
8
8
  exports["default"] = exports.Room = void 0;
9
9
 
10
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
11
+
10
12
  var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
11
13
 
12
14
  var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
@@ -105,7 +107,7 @@ var Room = /*#__PURE__*/function () {
105
107
  commentToFind.time = comment.time;
106
108
  commentToFind.unix_timestamp = comment.unix_timestamp;
107
109
  } else {
108
- this.comments.push(comment); // this.comments.sort((a, b) => a.date - b.date)
110
+ this.comments = [].concat((0, _toConsumableArray2["default"])(this.comments), [comment]);
109
111
  }
110
112
  }
111
113
  }, {
@@ -0,0 +1,218 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.ExpiredTokenAdapter = void 0;
9
+
10
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
11
+
12
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
13
+
14
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
15
+
16
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
17
+
18
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
19
+
20
+ var _util = require("../util");
21
+
22
+ // @ts-check
23
+ var ExpiredTokenAdapter = /*#__PURE__*/function () {
24
+ /** @type {boolean} */
25
+
26
+ /** @type {string | null} */
27
+
28
+ /** @type {Date | null} */
29
+
30
+ /** @type {import('./http').default} */
31
+
32
+ /** @type {(token: string, refreshToken: string, expiredAt: Date) => void | undefined} */
33
+
34
+ /** @type {any} */
35
+
36
+ /** @type {() => boolean} */
37
+
38
+ /**
39
+ * @constructor
40
+ *
41
+ * @param {{
42
+ * httpAdapter: import('./http').default,
43
+ * userId: string,
44
+ * refreshToken: string | null,
45
+ * expiredAt: string | null,
46
+ * onTokenRefreshed: (token: string, refreshToken: string, expiredAt: Date) => void
47
+ * getAuthenticationStatus: () => boolean,
48
+ * }} param
49
+ */
50
+ function ExpiredTokenAdapter(_ref) {
51
+ var httpAdapter = _ref.httpAdapter,
52
+ refreshToken = _ref.refreshToken,
53
+ expiredAt = _ref.expiredAt,
54
+ userId = _ref.userId,
55
+ onTokenRefreshed = _ref.onTokenRefreshed,
56
+ getAuthenticationStatus = _ref.getAuthenticationStatus;
57
+ (0, _classCallCheck2["default"])(this, ExpiredTokenAdapter);
58
+ (0, _defineProperty2["default"])(this, "_isExpiredTokenEnabled", false);
59
+ (0, _defineProperty2["default"])(this, "_refreshToken", null);
60
+ (0, _defineProperty2["default"])(this, "_expiredAt", null);
61
+ (0, _defineProperty2["default"])(this, "_http", void 0);
62
+ (0, _defineProperty2["default"])(this, "_onTokenRefreshed", void 0);
63
+ (0, _defineProperty2["default"])(this, "_timerId", void 0);
64
+ (0, _defineProperty2["default"])(this, "_getAuthenticationStatus", void 0);
65
+ this._http = httpAdapter;
66
+ this._refreshToken = refreshToken; // this._expiredAt = expiredAt == null ? null : new Date(expiredAt)
67
+
68
+ this._userId = userId;
69
+ this._onTokenRefreshed = onTokenRefreshed;
70
+ this._getAuthenticationStatus = getAuthenticationStatus;
71
+
72
+ if (this._refreshToken != null && this._refreshToken === '') {
73
+ this._refreshToken = null;
74
+ }
75
+
76
+ if (expiredAt != null && expiredAt !== '') {
77
+ this._expiredAt = new Date(expiredAt);
78
+ }
79
+
80
+ this._isExpiredTokenEnabled = this._refreshToken != null && this._expiredAt != null; // this._timerId = setInterval(this._checkToken, 1000)
81
+
82
+ this._checkToken();
83
+ }
84
+
85
+ (0, _createClass2["default"])(ExpiredTokenAdapter, [{
86
+ key: "_checkToken",
87
+ value: function () {
88
+ var _checkToken2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
89
+ var timeToSleep, now, diff;
90
+ return _regenerator["default"].wrap(function _callee$(_context) {
91
+ while (1) {
92
+ switch (_context.prev = _context.next) {
93
+ case 0:
94
+ timeToSleep = 5000; // 5 seconds
95
+
96
+ if (!(this._getAuthenticationStatus() == false || this._refreshToken == null)) {
97
+ _context.next = 3;
98
+ break;
99
+ }
100
+
101
+ return _context.abrupt("return");
102
+
103
+ case 3:
104
+ if (!(this._expiredAt != null && this._isExpiredTokenEnabled)) {
105
+ _context.next = 9;
106
+ break;
107
+ }
108
+
109
+ now = Date.now(); // @ts-ignore
110
+
111
+ diff = Math.floor((this._expiredAt - now) / 1000); // console.log('diff', diff)
112
+
113
+ if (!(diff < timeToSleep / 1000)) {
114
+ _context.next = 9;
115
+ break;
116
+ }
117
+
118
+ _context.next = 9;
119
+ return this.refreshAuthToken();
120
+
121
+ case 9:
122
+ _context.next = 11;
123
+ return (0, _util.sleep)(timeToSleep);
124
+
125
+ case 11:
126
+ this._checkToken();
127
+
128
+ case 12:
129
+ case "end":
130
+ return _context.stop();
131
+ }
132
+ }
133
+ }, _callee, this);
134
+ }));
135
+
136
+ function _checkToken() {
137
+ return _checkToken2.apply(this, arguments);
138
+ }
139
+
140
+ return _checkToken;
141
+ }()
142
+ }, {
143
+ key: "refreshAuthToken",
144
+ value: function () {
145
+ var _refreshAuthToken = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
146
+ var _this = this;
147
+
148
+ return _regenerator["default"].wrap(function _callee2$(_context2) {
149
+ while (1) {
150
+ switch (_context2.prev = _context2.next) {
151
+ case 0:
152
+ return _context2.abrupt("return", this._http.post('api/v2/sdk/refresh_user_token', {
153
+ user_id: this._userId,
154
+ refresh_token: this._refreshToken
155
+ }).then(function (r) {
156
+ var _this$_onTokenRefresh;
157
+
158
+ var res = r.body.results;
159
+ var token = res.token;
160
+ _this._refreshToken = res.refresh_token;
161
+
162
+ _this._http.setToken(res.token);
163
+
164
+ if (res.token_expires_at != null) {
165
+ _this._expiredAt = new Date(res.token_expires_at);
166
+ } // @ts-ignore
167
+
168
+
169
+ (_this$_onTokenRefresh = _this._onTokenRefreshed) === null || _this$_onTokenRefresh === void 0 ? void 0 : _this$_onTokenRefresh.call(_this, token, _this._refreshToken, _this._expiredAt);
170
+ return res;
171
+ }));
172
+
173
+ case 1:
174
+ case "end":
175
+ return _context2.stop();
176
+ }
177
+ }
178
+ }, _callee2, this);
179
+ }));
180
+
181
+ function refreshAuthToken() {
182
+ return _refreshAuthToken.apply(this, arguments);
183
+ }
184
+
185
+ return refreshAuthToken;
186
+ }()
187
+ }, {
188
+ key: "logout",
189
+ value: function () {
190
+ var _logout = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3() {
191
+ return _regenerator["default"].wrap(function _callee3$(_context3) {
192
+ while (1) {
193
+ switch (_context3.prev = _context3.next) {
194
+ case 0:
195
+ return _context3.abrupt("return", this._http.post('api/v2/sdk/logout', {
196
+ user_id: this._userId,
197
+ token: this._http.token
198
+ }));
199
+
200
+ case 1:
201
+ case "end":
202
+ return _context3.stop();
203
+ }
204
+ }
205
+ }, _callee3, this);
206
+ }));
207
+
208
+ function logout() {
209
+ return _logout.apply(this, arguments);
210
+ }
211
+
212
+ return logout;
213
+ }()
214
+ }]);
215
+ return ExpiredTokenAdapter;
216
+ }();
217
+
218
+ exports.ExpiredTokenAdapter = ExpiredTokenAdapter;
@@ -189,8 +189,6 @@ var HttpAdapter = /*#__PURE__*/function () {
189
189
  req.set("QISCUS-SDK-USER-ID", "".concat(this.userId));
190
190
  }
191
191
 
192
- console.log('this.token:', this.token);
193
-
194
192
  if (this.token != null) {
195
193
  req.set("QISCUS-SDK-TOKEN", "".concat(this.token));
196
194
  }
@@ -75,7 +75,7 @@ function synchronizeFactory(getHttp, getInterval, getSync, getId, logger) {
75
75
 
76
76
  accumulatedInterval += interval;
77
77
 
78
- if (!(accumulatedInterval >= getInterval() && shouldSync)) {
78
+ if (!(accumulatedInterval >= getInterval() && shouldSync())) {
79
79
  _context.next = 9;
80
80
  break;
81
81
  }
@@ -285,7 +285,7 @@ function synchronizeEventFactory(getHttp, getInterval, getSync, getId, logger) {
285
285
 
286
286
  accumulatedInterval += interval;
287
287
 
288
- if (!(accumulatedInterval >= getInterval() && shouldSync)) {
288
+ if (!(accumulatedInterval >= getInterval() && shouldSync())) {
289
289
  _context3.next = 9;
290
290
  break;
291
291
  }
@@ -441,7 +441,9 @@ function SyncAdapter(getHttpAdapter, _ref) {
441
441
  getShouldSync = _ref.getShouldSync,
442
442
  syncOnConnect = _ref.syncOnConnect,
443
443
  lastCommentId = _ref.lastCommentId,
444
- statusLogin = _ref.statusLogin;
444
+ statusLogin = _ref.statusLogin,
445
+ enableSync = _ref.enableSync,
446
+ enableSyncEvent = _ref.enableSyncEvent;
445
447
  var emitter = (0, _mitt["default"])();
446
448
 
447
449
  var logger = function logger() {
@@ -452,9 +454,9 @@ function SyncAdapter(getHttpAdapter, _ref) {
452
454
  }
453
455
 
454
456
  return isDebug ? (_console = console).log.apply(_console, ['QSync:'].concat(args)) : {};
455
- }; // let lastMessageId = 0
456
-
457
+ };
457
458
 
459
+ var lastMessageId = 0;
458
460
  var lastEventId = 0;
459
461
 
460
462
  var getInterval = function getInterval() {
@@ -463,10 +465,14 @@ function SyncAdapter(getHttpAdapter, _ref) {
463
465
  return syncOnConnect();
464
466
  }
465
467
 
466
- return;
468
+ return 0;
469
+ };
470
+
471
+ var _getShouldSync = function _getShouldSync() {
472
+ return getShouldSync() && enableSync();
467
473
  };
468
474
 
469
- var syncFactory = synchronizeFactory(getHttpAdapter, getInterval, getShouldSync, lastCommentId, logger);
475
+ var syncFactory = synchronizeFactory(getHttpAdapter, getInterval, _getShouldSync, lastCommentId, logger);
470
476
  syncFactory.on('last-message-id.new', function (id) {
471
477
  return lastMessageId = id;
472
478
  });
@@ -476,7 +482,12 @@ function SyncAdapter(getHttpAdapter, _ref) {
476
482
  syncFactory.run()["catch"](function (err) {
477
483
  return logger('got error when sync', err);
478
484
  });
479
- var syncEventFactory = synchronizeEventFactory(getHttpAdapter, getInterval, getShouldSync, function () {
485
+
486
+ var _getShouldSyncEvent = function _getShouldSyncEvent() {
487
+ return getShouldSync() && enableSyncEvent();
488
+ };
489
+
490
+ var syncEventFactory = synchronizeEventFactory(getHttpAdapter, getInterval, _getShouldSyncEvent, function () {
480
491
  return lastEventId;
481
492
  }, logger);
482
493
  syncEventFactory.on('last-event-id.new', function (id) {
package/lib/lib/util.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.wrapP = exports.tryCatch = void 0;
6
+ exports.sleep = exports.wrapP = exports.tryCatch = void 0;
7
7
 
8
8
  var tryCatch = function tryCatch(fn, default_, onError, onSuccess) {
9
9
  try {
@@ -26,4 +26,13 @@ var wrapP = function wrapP(promise) {
26
26
  });
27
27
  };
28
28
 
29
- exports.wrapP = wrapP;
29
+ exports.wrapP = wrapP;
30
+
31
+ var sleep = function sleep() {
32
+ var time = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1000;
33
+ return new Promise(function (r) {
34
+ return setTimeout(r, time);
35
+ });
36
+ };
37
+
38
+ exports.sleep = sleep;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qiscus-sdk-core",
3
- "version": "2.12.0",
3
+ "version": "2.12.2",
4
4
  "description": "Qiscus Web SDK Core",
5
5
  "license": "MIT",
6
6
  "main": "lib/index.js",
@@ -25,7 +25,9 @@
25
25
  "clean": "rimraf dist lib",
26
26
  "preversion": "npm-run-all clean build",
27
27
  "prepublishOnly": "npm-run-all clean build",
28
- "serve": "http-server -p 1234 -c-1 dist"
28
+ "serve": "http-server -p 1234 -c-1 dist",
29
+ "dev:serve": "run-p serve watch",
30
+ "release:patch": "np patch --any-branch --yolo"
29
31
  },
30
32
  "devDependencies": {
31
33
  "@babel/cli": "^7.10.5",
@@ -52,6 +54,7 @@
52
54
  "eslint-plugin-standard": "^4.0.1",
53
55
  "http-server": "^0.12.3",
54
56
  "mocha": "^5.2.0",
57
+ "np": "^7.6.2",
55
58
  "npm-run-all": "^4.1.5",
56
59
  "rimraf": "^2.7.1",
57
60
  "serve": "^12.0.0",
@@ -60,6 +63,7 @@
60
63
  },
61
64
  "dependencies": {
62
65
  "@babel/runtime": "^7.11.2",
66
+ "cuid": "^2.1.8",
63
67
  "date-fns": "^1.30.1",
64
68
  "is_js": "^0.9.0",
65
69
  "lodash.debounce": "^4.0.8",