qiscus-sdk-core 2.16.0 → 2.16.1
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/dist/qiscus-sdk-core.js +24475 -34766
- package/dist/qiscus-sdk-core.js.map +1 -1
- package/dist/qiscus-sdk-core.min.js +24 -8
- package/dist/qiscus-sdk-core.min.js.map +1 -1
- package/lib/index.js +1847 -2705
- package/lib/lib/Comment.js +89 -142
- package/lib/lib/Room.js +52 -94
- package/lib/lib/adapters/auth.js +42 -69
- package/lib/lib/adapters/custom-event.js +26 -35
- package/lib/lib/adapters/expired-token.js +86 -113
- package/lib/lib/adapters/hook.js +15 -19
- package/lib/lib/adapters/http.js +129 -253
- package/lib/lib/adapters/mqtt.js +422 -648
- package/lib/lib/adapters/room.js +121 -147
- package/lib/lib/adapters/sync.js +233 -421
- package/lib/lib/adapters/user.js +175 -210
- package/lib/lib/is.js +13 -47
- package/lib/lib/match.js +49 -92
- package/lib/lib/url-builder.js +7 -18
- package/lib/lib/util.js +6 -20
- package/lib/lib/utils.js +52 -97
- package/package.json +21 -55
package/lib/lib/adapters/sync.js
CHANGED
|
@@ -1,85 +1,76 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
var
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
var
|
|
20
|
-
|
|
21
|
-
return new Promise(function (res) {
|
|
22
|
-
return setTimeout(res, time);
|
|
2
|
+
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
|
|
3
|
+
var __async = (__this, __arguments, generator) => {
|
|
4
|
+
return new Promise((resolve, reject) => {
|
|
5
|
+
var fulfilled = (value) => {
|
|
6
|
+
try {
|
|
7
|
+
step(generator.next(value));
|
|
8
|
+
} catch (e) {
|
|
9
|
+
reject(e);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var rejected = (value) => {
|
|
13
|
+
try {
|
|
14
|
+
step(generator.throw(value));
|
|
15
|
+
} catch (e) {
|
|
16
|
+
reject(e);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
20
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
23
21
|
});
|
|
24
22
|
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
var
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
23
|
+
var __await = function(promise, isYieldStar) {
|
|
24
|
+
this[0] = promise;
|
|
25
|
+
this[1] = isYieldStar;
|
|
26
|
+
};
|
|
27
|
+
var __asyncGenerator = (__this, __arguments, generator) => {
|
|
28
|
+
var resume = (k, v, yes, no) => {
|
|
29
|
+
try {
|
|
30
|
+
var x = generator[k](v), isAwait = (v = x.value) instanceof __await, done = x.done;
|
|
31
|
+
Promise.resolve(isAwait ? v[0] : v).then((y) => isAwait ? resume(k === "return" ? k : "next", v[1] ? { done: y.done, value: y.value } : y, yes, no) : yes({ value: y, done })).catch((e) => resume("throw", e, yes, no));
|
|
32
|
+
} catch (e) {
|
|
33
|
+
no(e);
|
|
34
|
+
}
|
|
35
|
+
}, method = (k) => it[k] = (x) => new Promise((yes, no) => resume(k, x, yes, no)), it = {};
|
|
36
|
+
return generator = generator.apply(__this, __arguments), it[__knownSymbol("asyncIterator")] = () => it, method("next"), method("throw"), method("return"), it;
|
|
37
|
+
};
|
|
38
|
+
var __forAwait = (obj, it, method) => (it = obj[__knownSymbol("asyncIterator")]) ? it.call(obj) : (obj = obj[__knownSymbol("iterator")](), it = {}, method = (key, fn) => (fn = obj[key]) && (it[key] = (arg) => new Promise((yes, no, done) => (arg = fn.call(obj, arg), done = arg.done, Promise.resolve(arg.value).then((value) => yes({ value, done }), no)))), method("next"), method("return"), it);
|
|
39
|
+
const mitt = require("mitt");
|
|
40
|
+
const urlBuilder = require("../url-builder.js");
|
|
41
|
+
const noop = () => {
|
|
42
|
+
};
|
|
43
|
+
const sleep = (time) => new Promise((res) => setTimeout(res, time));
|
|
44
|
+
function synchronizeFactory(getHttp, getInterval, getSync, getId2, logger) {
|
|
45
|
+
const emitter = mitt();
|
|
46
|
+
const synchronize = (messageId) => {
|
|
47
|
+
const url = urlBuilder("api/v2/sdk/sync").param("last_received_comment_id", messageId).build();
|
|
48
|
+
return getHttp().get(url).then((resp) => {
|
|
49
|
+
const results = resp.body.results;
|
|
50
|
+
const messages = results.comments;
|
|
51
|
+
const lastMessageId = results.meta.last_received_comment_id;
|
|
52
|
+
messages.sort((a, b) => a.id - b.id);
|
|
36
53
|
return Promise.resolve({
|
|
37
|
-
lastMessageId
|
|
38
|
-
messages
|
|
54
|
+
lastMessageId,
|
|
55
|
+
messages,
|
|
39
56
|
interval: getInterval()
|
|
40
57
|
});
|
|
41
|
-
})
|
|
58
|
+
}).catch(noop);
|
|
42
59
|
};
|
|
43
60
|
function generator() {
|
|
44
|
-
return
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
interval = 100;
|
|
54
|
-
shouldSync = function shouldSync() {
|
|
55
|
-
return getHttp() != null && getSync();
|
|
56
|
-
};
|
|
57
|
-
case 1:
|
|
58
|
-
if (!true) {
|
|
59
|
-
_context.next = 4;
|
|
60
|
-
break;
|
|
61
|
-
}
|
|
62
|
-
accumulatedInterval += interval;
|
|
63
|
-
if (!(accumulatedInterval >= getInterval() && shouldSync())) {
|
|
64
|
-
_context.next = 2;
|
|
65
|
-
break;
|
|
66
|
-
}
|
|
67
|
-
accumulatedInterval = 0;
|
|
68
|
-
_context.next = 2;
|
|
69
|
-
return synchronize(getId());
|
|
70
|
-
case 2:
|
|
71
|
-
_context.next = 3;
|
|
72
|
-
return (0, _awaitAsyncGenerator2["default"])(sleep(interval));
|
|
73
|
-
case 3:
|
|
74
|
-
_context.next = 1;
|
|
75
|
-
break;
|
|
76
|
-
case 4:
|
|
77
|
-
case "end":
|
|
78
|
-
return _context.stop();
|
|
61
|
+
return __asyncGenerator(this, null, function* () {
|
|
62
|
+
let accumulatedInterval = 0;
|
|
63
|
+
const interval = 100;
|
|
64
|
+
const shouldSync = () => getHttp() != null && getSync();
|
|
65
|
+
while (true) {
|
|
66
|
+
accumulatedInterval += interval;
|
|
67
|
+
if (accumulatedInterval >= getInterval() && shouldSync()) {
|
|
68
|
+
accumulatedInterval = 0;
|
|
69
|
+
yield synchronize(getId2());
|
|
79
70
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
71
|
+
yield new __await(sleep(interval));
|
|
72
|
+
}
|
|
73
|
+
});
|
|
83
74
|
}
|
|
84
75
|
return {
|
|
85
76
|
get synchronize() {
|
|
@@ -91,163 +82,75 @@ function synchronizeFactory(getHttp, getInterval, getSync, getId, logger) {
|
|
|
91
82
|
get off() {
|
|
92
83
|
return emitter.off;
|
|
93
84
|
},
|
|
94
|
-
run
|
|
95
|
-
return (
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
case 3:
|
|
108
|
-
if (!(_iteratorAbruptCompletion = !(_step = _context2.sent).done)) {
|
|
109
|
-
_context2.next = 5;
|
|
110
|
-
break;
|
|
111
|
-
}
|
|
112
|
-
result = _step.value;
|
|
113
|
-
try {
|
|
114
|
-
emitter.emit('synchronize', Date.now());
|
|
115
|
-
if ((result === null || result === void 0 ? void 0 : result.lastMessageId) != null && (result === null || result === void 0 ? void 0 : result.messages) != null) {
|
|
116
|
-
messageId = result.lastMessageId;
|
|
117
|
-
messages = result.messages;
|
|
118
|
-
if (messageId > getId()) {
|
|
119
|
-
messages.forEach(function (m) {
|
|
120
|
-
return emitter.emit('message.new', m);
|
|
121
|
-
});
|
|
122
|
-
emitter.emit('last-message-id.new', messageId);
|
|
123
|
-
}
|
|
85
|
+
run() {
|
|
86
|
+
return __async(this, null, function* () {
|
|
87
|
+
try {
|
|
88
|
+
for (var iter = __forAwait(generator()), more, temp, error; more = !(temp = yield iter.next()).done; more = false) {
|
|
89
|
+
let result = temp.value;
|
|
90
|
+
try {
|
|
91
|
+
emitter.emit("synchronize", Date.now());
|
|
92
|
+
if ((result == null ? void 0 : result.lastMessageId) != null && (result == null ? void 0 : result.messages) != null) {
|
|
93
|
+
const messageId = result.lastMessageId;
|
|
94
|
+
const messages = result.messages;
|
|
95
|
+
if (messageId > getId2()) {
|
|
96
|
+
messages.forEach((m) => emitter.emit("message.new", m));
|
|
97
|
+
emitter.emit("last-message-id.new", messageId);
|
|
124
98
|
}
|
|
125
|
-
} catch (e) {
|
|
126
|
-
logger('error when sync', e.message);
|
|
127
|
-
console.log('error when sync', e);
|
|
128
|
-
}
|
|
129
|
-
case 4:
|
|
130
|
-
_iteratorAbruptCompletion = false;
|
|
131
|
-
_context2.next = 2;
|
|
132
|
-
break;
|
|
133
|
-
case 5:
|
|
134
|
-
_context2.next = 7;
|
|
135
|
-
break;
|
|
136
|
-
case 6:
|
|
137
|
-
_context2.prev = 6;
|
|
138
|
-
_t = _context2["catch"](1);
|
|
139
|
-
_didIteratorError = true;
|
|
140
|
-
_iteratorError = _t;
|
|
141
|
-
case 7:
|
|
142
|
-
_context2.prev = 7;
|
|
143
|
-
_context2.prev = 8;
|
|
144
|
-
if (!(_iteratorAbruptCompletion && _iterator["return"] != null)) {
|
|
145
|
-
_context2.next = 9;
|
|
146
|
-
break;
|
|
147
|
-
}
|
|
148
|
-
_context2.next = 9;
|
|
149
|
-
return _iterator["return"]();
|
|
150
|
-
case 9:
|
|
151
|
-
_context2.prev = 9;
|
|
152
|
-
if (!_didIteratorError) {
|
|
153
|
-
_context2.next = 10;
|
|
154
|
-
break;
|
|
155
99
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
100
|
+
} catch (e) {
|
|
101
|
+
logger("error when sync", e.message);
|
|
102
|
+
console.log("error when sync", e);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
} catch (temp) {
|
|
106
|
+
error = [temp];
|
|
107
|
+
} finally {
|
|
108
|
+
try {
|
|
109
|
+
more && (temp = iter.return) && (yield temp.call(iter));
|
|
110
|
+
} finally {
|
|
111
|
+
if (error)
|
|
112
|
+
throw error[0];
|
|
164
113
|
}
|
|
165
|
-
}
|
|
166
|
-
})
|
|
114
|
+
}
|
|
115
|
+
});
|
|
167
116
|
}
|
|
168
117
|
};
|
|
169
118
|
}
|
|
170
|
-
function synchronizeEventFactory(getHttp, getInterval, getSync,
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
return getHttp().get(url).then(
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
var messageDelivered = events.filter(function (it) {
|
|
182
|
-
return it.action_topic === 'delivered';
|
|
183
|
-
}).map(function (it) {
|
|
184
|
-
return it.payload.data;
|
|
185
|
-
});
|
|
186
|
-
var messageRead = events.filter(function (it) {
|
|
187
|
-
return it.action_topic === 'read';
|
|
188
|
-
}).map(function (it) {
|
|
189
|
-
return it.payload.data;
|
|
190
|
-
});
|
|
191
|
-
var messageDeleted = events.filter(function (it) {
|
|
192
|
-
return it.action_topic === 'delete_message';
|
|
193
|
-
}).map(function (it) {
|
|
194
|
-
return it.payload.data;
|
|
195
|
-
});
|
|
196
|
-
var roomCleared = events.filter(function (it) {
|
|
197
|
-
return it.action_topic === 'clear_room';
|
|
198
|
-
}).map(function (it) {
|
|
199
|
-
return it.payload.data;
|
|
200
|
-
});
|
|
119
|
+
function synchronizeEventFactory(getHttp, getInterval, getSync, getId2, logger) {
|
|
120
|
+
const emitter = mitt();
|
|
121
|
+
const synchronize = (messageId) => {
|
|
122
|
+
const url = urlBuilder("api/v2/sdk/sync_event").param("start_event_id", messageId).build();
|
|
123
|
+
return getHttp().get(url).then((resp) => {
|
|
124
|
+
const events = resp.body.events;
|
|
125
|
+
const lastId = events.map((it) => it.id).sort((a, b) => a - b).pop();
|
|
126
|
+
const messageDelivered = events.filter((it) => it.action_topic === "delivered").map((it) => it.payload.data);
|
|
127
|
+
const messageRead = events.filter((it) => it.action_topic === "read").map((it) => it.payload.data);
|
|
128
|
+
const messageDeleted = events.filter((it) => it.action_topic === "delete_message").map((it) => it.payload.data);
|
|
129
|
+
const roomCleared = events.filter((it) => it.action_topic === "clear_room").map((it) => it.payload.data);
|
|
201
130
|
return Promise.resolve({
|
|
202
|
-
lastId
|
|
203
|
-
messageDelivered
|
|
204
|
-
messageRead
|
|
205
|
-
messageDeleted
|
|
206
|
-
roomCleared
|
|
131
|
+
lastId,
|
|
132
|
+
messageDelivered,
|
|
133
|
+
messageRead,
|
|
134
|
+
messageDeleted,
|
|
135
|
+
roomCleared,
|
|
207
136
|
interval: getInterval()
|
|
208
137
|
});
|
|
209
|
-
})
|
|
138
|
+
}).catch(noop);
|
|
210
139
|
};
|
|
211
140
|
function generator() {
|
|
212
|
-
return
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
interval = 100;
|
|
222
|
-
shouldSync = function shouldSync() {
|
|
223
|
-
return getHttp() != null && getSync();
|
|
224
|
-
};
|
|
225
|
-
case 1:
|
|
226
|
-
if (!true) {
|
|
227
|
-
_context3.next = 4;
|
|
228
|
-
break;
|
|
229
|
-
}
|
|
230
|
-
accumulatedInterval += interval;
|
|
231
|
-
if (!(accumulatedInterval >= getInterval() && shouldSync())) {
|
|
232
|
-
_context3.next = 2;
|
|
233
|
-
break;
|
|
234
|
-
}
|
|
235
|
-
accumulatedInterval = 0;
|
|
236
|
-
_context3.next = 2;
|
|
237
|
-
return synchronize(getId());
|
|
238
|
-
case 2:
|
|
239
|
-
_context3.next = 3;
|
|
240
|
-
return (0, _awaitAsyncGenerator2["default"])(sleep(interval));
|
|
241
|
-
case 3:
|
|
242
|
-
_context3.next = 1;
|
|
243
|
-
break;
|
|
244
|
-
case 4:
|
|
245
|
-
case "end":
|
|
246
|
-
return _context3.stop();
|
|
141
|
+
return __asyncGenerator(this, null, function* () {
|
|
142
|
+
let accumulatedInterval = 0;
|
|
143
|
+
const interval = 100;
|
|
144
|
+
const shouldSync = () => getHttp() != null && getSync();
|
|
145
|
+
while (true) {
|
|
146
|
+
accumulatedInterval += interval;
|
|
147
|
+
if (accumulatedInterval >= getInterval() && shouldSync()) {
|
|
148
|
+
accumulatedInterval = 0;
|
|
149
|
+
yield synchronize(getId2());
|
|
247
150
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
151
|
+
yield new __await(sleep(interval));
|
|
152
|
+
}
|
|
153
|
+
});
|
|
251
154
|
}
|
|
252
155
|
return {
|
|
253
156
|
get synchronize() {
|
|
@@ -259,153 +162,98 @@ function synchronizeEventFactory(getHttp, getInterval, getSync, getId, logger) {
|
|
|
259
162
|
get off() {
|
|
260
163
|
return emitter.off;
|
|
261
164
|
},
|
|
262
|
-
run
|
|
263
|
-
return (
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
}
|
|
280
|
-
result = _step2.value;
|
|
281
|
-
try {
|
|
282
|
-
eventId = result.lastId;
|
|
283
|
-
if (eventId > getId()) {
|
|
284
|
-
emitter.emit('last-event-id.new', eventId);
|
|
285
|
-
result.messageDelivered.forEach(function (it) {
|
|
286
|
-
return emitter.emit('message.delivered', it);
|
|
287
|
-
});
|
|
288
|
-
result.messageDeleted.forEach(function (it) {
|
|
289
|
-
return emitter.emit('message.deleted', it);
|
|
290
|
-
});
|
|
291
|
-
result.messageRead.forEach(function (it) {
|
|
292
|
-
return emitter.emit('message.read', it);
|
|
293
|
-
});
|
|
294
|
-
result.roomCleared.forEach(function (it) {
|
|
295
|
-
return emitter.emit('room.cleared', it);
|
|
296
|
-
});
|
|
297
|
-
}
|
|
298
|
-
} catch (e) {
|
|
299
|
-
logger('error when sync event', e.message);
|
|
165
|
+
run() {
|
|
166
|
+
return __async(this, null, function* () {
|
|
167
|
+
try {
|
|
168
|
+
for (var iter = __forAwait(generator()), more, temp, error; more = !(temp = yield iter.next()).done; more = false) {
|
|
169
|
+
let result = temp.value;
|
|
170
|
+
try {
|
|
171
|
+
const eventId = result.lastId;
|
|
172
|
+
if (eventId > getId2()) {
|
|
173
|
+
emitter.emit("last-event-id.new", eventId);
|
|
174
|
+
result.messageDelivered.forEach(
|
|
175
|
+
(it) => emitter.emit("message.delivered", it)
|
|
176
|
+
);
|
|
177
|
+
result.messageDeleted.forEach(
|
|
178
|
+
(it) => emitter.emit("message.deleted", it)
|
|
179
|
+
);
|
|
180
|
+
result.messageRead.forEach((it) => emitter.emit("message.read", it));
|
|
181
|
+
result.roomCleared.forEach((it) => emitter.emit("room.cleared", it));
|
|
300
182
|
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
break;
|
|
305
|
-
case 5:
|
|
306
|
-
_context4.next = 7;
|
|
307
|
-
break;
|
|
308
|
-
case 6:
|
|
309
|
-
_context4.prev = 6;
|
|
310
|
-
_t2 = _context4["catch"](1);
|
|
311
|
-
_didIteratorError2 = true;
|
|
312
|
-
_iteratorError2 = _t2;
|
|
313
|
-
case 7:
|
|
314
|
-
_context4.prev = 7;
|
|
315
|
-
_context4.prev = 8;
|
|
316
|
-
if (!(_iteratorAbruptCompletion2 && _iterator2["return"] != null)) {
|
|
317
|
-
_context4.next = 9;
|
|
318
|
-
break;
|
|
319
|
-
}
|
|
320
|
-
_context4.next = 9;
|
|
321
|
-
return _iterator2["return"]();
|
|
322
|
-
case 9:
|
|
323
|
-
_context4.prev = 9;
|
|
324
|
-
if (!_didIteratorError2) {
|
|
325
|
-
_context4.next = 10;
|
|
326
|
-
break;
|
|
327
|
-
}
|
|
328
|
-
throw _iteratorError2;
|
|
329
|
-
case 10:
|
|
330
|
-
return _context4.finish(9);
|
|
331
|
-
case 11:
|
|
332
|
-
return _context4.finish(7);
|
|
333
|
-
case 12:
|
|
334
|
-
case "end":
|
|
335
|
-
return _context4.stop();
|
|
183
|
+
} catch (e) {
|
|
184
|
+
logger("error when sync event", e.message);
|
|
185
|
+
}
|
|
336
186
|
}
|
|
337
|
-
}
|
|
338
|
-
|
|
187
|
+
} catch (temp) {
|
|
188
|
+
error = [temp];
|
|
189
|
+
} finally {
|
|
190
|
+
try {
|
|
191
|
+
more && (temp = iter.return) && (yield temp.call(iter));
|
|
192
|
+
} finally {
|
|
193
|
+
if (error)
|
|
194
|
+
throw error[0];
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
});
|
|
339
198
|
}
|
|
340
199
|
};
|
|
341
200
|
}
|
|
342
|
-
function SyncAdapter(getHttpAdapter,
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
args[_key] = arguments[_key];
|
|
357
|
-
}
|
|
358
|
-
return isDebug ? (_console = console).log.apply(_console, ['QSync:'].concat(args)) : {};
|
|
359
|
-
};
|
|
360
|
-
var lastMessageId = 0;
|
|
361
|
-
var lastEventId = 0;
|
|
362
|
-
var getInterval = function getInterval() {
|
|
201
|
+
function SyncAdapter(getHttpAdapter, {
|
|
202
|
+
isDebug = false,
|
|
203
|
+
syncInterval,
|
|
204
|
+
getShouldSync,
|
|
205
|
+
syncOnConnect,
|
|
206
|
+
lastCommentId,
|
|
207
|
+
statusLogin,
|
|
208
|
+
enableSync,
|
|
209
|
+
enableSyncEvent
|
|
210
|
+
}) {
|
|
211
|
+
const emitter = mitt();
|
|
212
|
+
const logger = (...args) => isDebug ? console.log("QSync:", ...args) : {};
|
|
213
|
+
let lastEventId = 0;
|
|
214
|
+
const getInterval = () => {
|
|
363
215
|
if (statusLogin()) {
|
|
364
216
|
if (getShouldSync()) return syncInterval();
|
|
365
217
|
return syncOnConnect();
|
|
366
218
|
}
|
|
367
219
|
return 0;
|
|
368
220
|
};
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
syncFactory.on(
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
}, logger);
|
|
391
|
-
syncEventFactory.on('last-event-id.new', function (id) {
|
|
221
|
+
const _getShouldSync = () => getShouldSync() && enableSync();
|
|
222
|
+
const syncFactory = synchronizeFactory(
|
|
223
|
+
getHttpAdapter,
|
|
224
|
+
getInterval,
|
|
225
|
+
_getShouldSync,
|
|
226
|
+
lastCommentId,
|
|
227
|
+
logger
|
|
228
|
+
);
|
|
229
|
+
syncFactory.on("last-message-id.new", (id) => id);
|
|
230
|
+
syncFactory.on("message.new", (m) => emitter.emit("message.new", m));
|
|
231
|
+
syncFactory.on("synchronize", (m) => emitter.emit("synchronize", m));
|
|
232
|
+
syncFactory.run().catch((err) => logger("got error when sync", err));
|
|
233
|
+
const _getShouldSyncEvent = () => getShouldSync() && enableSyncEvent();
|
|
234
|
+
const syncEventFactory = synchronizeEventFactory(
|
|
235
|
+
getHttpAdapter,
|
|
236
|
+
getInterval,
|
|
237
|
+
_getShouldSyncEvent,
|
|
238
|
+
() => lastEventId,
|
|
239
|
+
logger
|
|
240
|
+
);
|
|
241
|
+
syncEventFactory.on("last-event-id.new", (id) => {
|
|
392
242
|
lastEventId = id;
|
|
393
243
|
});
|
|
394
|
-
syncEventFactory.on(
|
|
395
|
-
emitter.emit(
|
|
396
|
-
});
|
|
397
|
-
syncEventFactory.on('message.delivered', function (it) {
|
|
398
|
-
return emitter.emit('message.delivered', it);
|
|
399
|
-
});
|
|
400
|
-
syncEventFactory.on('message.deleted', function (it) {
|
|
401
|
-
return emitter.emit('message.deleted', it);
|
|
402
|
-
});
|
|
403
|
-
syncEventFactory.on('room.cleared', function (it) {
|
|
404
|
-
return emitter.emit('room.cleared', it);
|
|
405
|
-
});
|
|
406
|
-
syncEventFactory.run()["catch"](function (err) {
|
|
407
|
-
return logger('got error when sync event', err);
|
|
244
|
+
syncEventFactory.on("message.read", (it) => {
|
|
245
|
+
emitter.emit("message.read", it);
|
|
408
246
|
});
|
|
247
|
+
syncEventFactory.on(
|
|
248
|
+
"message.delivered",
|
|
249
|
+
(it) => emitter.emit("message.delivered", it)
|
|
250
|
+
);
|
|
251
|
+
syncEventFactory.on(
|
|
252
|
+
"message.deleted",
|
|
253
|
+
(it) => emitter.emit("message.deleted", it)
|
|
254
|
+
);
|
|
255
|
+
syncEventFactory.on("room.cleared", (it) => emitter.emit("room.cleared", it));
|
|
256
|
+
syncEventFactory.run().catch((err) => logger("got error when sync event", err));
|
|
409
257
|
return {
|
|
410
258
|
get on() {
|
|
411
259
|
return emitter.on;
|
|
@@ -419,78 +267,42 @@ function SyncAdapter(getHttpAdapter, _ref) {
|
|
|
419
267
|
get enabled() {
|
|
420
268
|
return enableSync();
|
|
421
269
|
},
|
|
422
|
-
synchronize
|
|
423
|
-
return (
|
|
424
|
-
var
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
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 2:
|
|
451
|
-
case "end":
|
|
452
|
-
return _context5.stop();
|
|
453
|
-
}
|
|
454
|
-
}, _callee5);
|
|
455
|
-
}))();
|
|
270
|
+
synchronize() {
|
|
271
|
+
return __async(this, null, function* () {
|
|
272
|
+
var _a2, _b;
|
|
273
|
+
let id = lastCommentId();
|
|
274
|
+
let result = yield syncFactory.synchronize(id);
|
|
275
|
+
let messages = (_a2 = result == null ? void 0 : result.messages) != null ? _a2 : [];
|
|
276
|
+
let lastMessageId = (_b = result == null ? void 0 : result.lastMessageId) != null ? _b : -1;
|
|
277
|
+
for (let message of messages) {
|
|
278
|
+
emitter.emit("message.new", message);
|
|
279
|
+
}
|
|
280
|
+
if (lastMessageId > 0) {
|
|
281
|
+
emitter.emit("last-message-id.new", lastMessageId);
|
|
282
|
+
}
|
|
283
|
+
});
|
|
456
284
|
},
|
|
457
|
-
synchronizeEvent
|
|
458
|
-
return (
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
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 2:
|
|
488
|
-
case "end":
|
|
489
|
-
return _context6.stop();
|
|
285
|
+
synchronizeEvent() {
|
|
286
|
+
return __async(this, null, function* () {
|
|
287
|
+
let result = yield syncEventFactory.synchronize(lastEventId);
|
|
288
|
+
try {
|
|
289
|
+
const eventId = result.lastId;
|
|
290
|
+
if (eventId > getId()) {
|
|
291
|
+
emitter.emit("last-event-id.new", eventId);
|
|
292
|
+
result.messageDelivered.forEach(
|
|
293
|
+
(it) => emitter.emit("message.delivered", it)
|
|
294
|
+
);
|
|
295
|
+
result.messageDeleted.forEach(
|
|
296
|
+
(it) => emitter.emit("message.deleted", it)
|
|
297
|
+
);
|
|
298
|
+
result.messageRead.forEach((it) => emitter.emit("message.read", it));
|
|
299
|
+
result.roomCleared.forEach((it) => emitter.emit("room.cleared", it));
|
|
490
300
|
}
|
|
491
|
-
}
|
|
492
|
-
|
|
301
|
+
} catch (e) {
|
|
302
|
+
logger("error when sync event", e.message);
|
|
303
|
+
}
|
|
304
|
+
});
|
|
493
305
|
}
|
|
494
306
|
};
|
|
495
307
|
}
|
|
496
|
-
module.exports =
|
|
308
|
+
module.exports = SyncAdapter;
|