@reactoo/watchtogether-sdk-js 2.7.66 → 2.7.67
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/watchtogether-sdk.min.js +2 -2
- package/package.json +1 -1
- package/src/models/iot.js +20 -15
- package/src/modules/wt-iot2.js +17 -3
package/package.json
CHANGED
package/src/models/iot.js
CHANGED
|
@@ -10,10 +10,11 @@ let iot = function () {
|
|
|
10
10
|
let subscriptionFailureCount = 0;
|
|
11
11
|
|
|
12
12
|
return {
|
|
13
|
-
|
|
13
|
+
|
|
14
14
|
__promise: null,
|
|
15
15
|
|
|
16
16
|
__createNewWorker: () => {
|
|
17
|
+
this.__privates.iot.log('Creating new worker...');
|
|
17
18
|
this.__privates.iot.terminateWorker();
|
|
18
19
|
this.__privates.iot.initWorker();
|
|
19
20
|
},
|
|
@@ -44,11 +45,11 @@ let iot = function () {
|
|
|
44
45
|
return Promise.reject(error);
|
|
45
46
|
});
|
|
46
47
|
},
|
|
47
|
-
|
|
48
|
+
|
|
48
49
|
getCredentials: () => {
|
|
49
50
|
return this.__privates.auth.__client
|
|
50
51
|
.then(client => client.apis.auth.iotSignIn({}, {requestBody: {suggestedTopics:true, domain: location.hostname}}))
|
|
51
|
-
|
|
52
|
+
|
|
52
53
|
},
|
|
53
54
|
|
|
54
55
|
onConnect: (data, connectionId) => {
|
|
@@ -58,6 +59,7 @@ let iot = function () {
|
|
|
58
59
|
this.__privates.iot.log('MQTT client closed');
|
|
59
60
|
if(shouldBeConnected && !isReconnecting) {
|
|
60
61
|
this.__privates.iot.log('Connection unexpectedly closed, reconnecting...');
|
|
62
|
+
this.iot.__createNewWorker();
|
|
61
63
|
this.iot.__updateCredentials();
|
|
62
64
|
}
|
|
63
65
|
},
|
|
@@ -91,7 +93,7 @@ let iot = function () {
|
|
|
91
93
|
interruptCount++;
|
|
92
94
|
this.__privates.iot.log('MQTT client connection_failure');
|
|
93
95
|
},
|
|
94
|
-
|
|
96
|
+
|
|
95
97
|
iotLogin: (subscribeToSuggestedTopics = true) => {
|
|
96
98
|
|
|
97
99
|
interruptCount = 0;
|
|
@@ -150,12 +152,13 @@ let iot = function () {
|
|
|
150
152
|
|
|
151
153
|
return this.iot.__promise;
|
|
152
154
|
},
|
|
153
|
-
|
|
155
|
+
|
|
154
156
|
iotLogout: (keepCurrentTopics = false) => {
|
|
155
157
|
|
|
156
158
|
if(!keepCurrentTopics) {
|
|
157
159
|
__currentTopics.clear();
|
|
158
160
|
this.__privates.iot.clearTopics();
|
|
161
|
+
this.__privates.iot.terminateWorker();
|
|
159
162
|
}
|
|
160
163
|
|
|
161
164
|
interruptCount = 0;
|
|
@@ -180,11 +183,11 @@ let iot = function () {
|
|
|
180
183
|
return true;
|
|
181
184
|
});
|
|
182
185
|
},
|
|
183
|
-
|
|
186
|
+
|
|
184
187
|
isConnected: () => {
|
|
185
188
|
return this.__privates.iot.isConnected();
|
|
186
189
|
},
|
|
187
|
-
|
|
190
|
+
|
|
188
191
|
subscribe: (topic) => {
|
|
189
192
|
if(!__currentTopics.has(topic)) {
|
|
190
193
|
__currentTopics.add(topic);
|
|
@@ -216,7 +219,7 @@ let iot = function () {
|
|
|
216
219
|
});
|
|
217
220
|
}
|
|
218
221
|
},
|
|
219
|
-
|
|
222
|
+
|
|
220
223
|
unsubscribe: (topic) => {
|
|
221
224
|
__currentTopics.delete(topic);
|
|
222
225
|
if(!this.iot.__promise) return Promise.reject('not_connected');
|
|
@@ -228,19 +231,19 @@ let iot = function () {
|
|
|
228
231
|
return Promise.reject('unsubscription_failed');
|
|
229
232
|
});
|
|
230
233
|
},
|
|
231
|
-
|
|
234
|
+
|
|
232
235
|
send: (topic, message) => {
|
|
233
236
|
return this.iot.__promise.then(() => this.__privates.iot.send(topic,message));
|
|
234
237
|
},
|
|
235
|
-
|
|
238
|
+
|
|
236
239
|
$once: (key, callback, that) => {
|
|
237
240
|
return this.__privates.iot.once(key, callback, that || this);
|
|
238
241
|
},
|
|
239
|
-
|
|
242
|
+
|
|
240
243
|
$on: (key, callback, that) => {
|
|
241
244
|
return this.__privates.iot.on(key, callback, that || this);
|
|
242
245
|
},
|
|
243
|
-
|
|
246
|
+
|
|
244
247
|
$off: (key, callback, that) => {
|
|
245
248
|
return this.__privates.iot.off(key, callback, that || this);
|
|
246
249
|
},
|
|
@@ -250,7 +253,7 @@ let iot = function () {
|
|
|
250
253
|
$emit: (key, ...args) => {
|
|
251
254
|
return this.__privates.iot.emit(key, ...args);
|
|
252
255
|
},
|
|
253
|
-
|
|
256
|
+
|
|
254
257
|
checkConnection: () => {
|
|
255
258
|
return this.__privates.iot.checkConnection()
|
|
256
259
|
.catch(error => {
|
|
@@ -271,6 +274,7 @@ let iot = function () {
|
|
|
271
274
|
}
|
|
272
275
|
|
|
273
276
|
this.__privates.iot.log('Connection check failed:', error);
|
|
277
|
+
this.iot.__createNewWorker();
|
|
274
278
|
return this.iot.__updateCredentials();
|
|
275
279
|
});
|
|
276
280
|
},
|
|
@@ -285,12 +289,13 @@ let iot = function () {
|
|
|
285
289
|
.catch(error => {
|
|
286
290
|
|
|
287
291
|
if(error?.cause === -1) {
|
|
288
|
-
this.__privates.iot.log("We don't have a topic to check connection with");
|
|
292
|
+
this.__privates.iot.log("We don't have a topic to check connection with or check was aborted");
|
|
289
293
|
return;
|
|
290
294
|
}
|
|
291
295
|
|
|
292
|
-
if(
|
|
296
|
+
if(shouldBeConnected && !isReconnecting) {
|
|
293
297
|
this.__privates.iot.log('Keepalive failed:', error);
|
|
298
|
+
this.iot.__createNewWorker();
|
|
294
299
|
this.iot.__updateCredentials();
|
|
295
300
|
}
|
|
296
301
|
});
|
package/src/modules/wt-iot2.js
CHANGED
|
@@ -11,7 +11,7 @@ class Iot {
|
|
|
11
11
|
this.currentCredentialsExpirationStamp = null;
|
|
12
12
|
this.lastConnectParams = null;
|
|
13
13
|
this.subscribedTopics = new Set();
|
|
14
|
-
this.
|
|
14
|
+
this.abortController = null;
|
|
15
15
|
this.initWorker();
|
|
16
16
|
|
|
17
17
|
if (enableDebugFlag) {
|
|
@@ -26,11 +26,13 @@ class Iot {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
initWorker() {
|
|
29
|
+
this.log('iot initWorker');
|
|
29
30
|
this.worker = new Worker();
|
|
30
31
|
this.worker.onmessage = this.handleWorkerMessage.bind(this);
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
terminateWorker() {
|
|
35
|
+
this.log('iot terminateWorker');
|
|
34
36
|
this.worker.terminate();
|
|
35
37
|
}
|
|
36
38
|
|
|
@@ -43,6 +45,7 @@ class Iot {
|
|
|
43
45
|
})
|
|
44
46
|
.then(() => {
|
|
45
47
|
this.log('iot connect');
|
|
48
|
+
this.abortController = new AbortController();
|
|
46
49
|
this.startCredentialsExpirationCheck(expiration);
|
|
47
50
|
this.lastConnectParams = { apiMqttUrl, apiMqttClientId, region, accessKeyId, secretAccessKey, sessionToken, expiration };
|
|
48
51
|
|
|
@@ -60,7 +63,6 @@ class Iot {
|
|
|
60
63
|
clearTimeout(timeoutId);
|
|
61
64
|
this.off('worker:connect_result', handleConnectResult);
|
|
62
65
|
if (event.success) {
|
|
63
|
-
this.currentConnectionId = event.connectionId;
|
|
64
66
|
resolve();
|
|
65
67
|
} else {
|
|
66
68
|
reject(new Error(event.error));
|
|
@@ -86,6 +88,7 @@ class Iot {
|
|
|
86
88
|
disconnect() {
|
|
87
89
|
this.log('iot disconnect');
|
|
88
90
|
this.stopCredentialsExpirationCheck();
|
|
91
|
+
this.abortController?.abort();
|
|
89
92
|
return new Promise((resolve, reject) => {
|
|
90
93
|
|
|
91
94
|
const stamp = new Date().getTime();
|
|
@@ -377,12 +380,23 @@ class Iot {
|
|
|
377
380
|
|
|
378
381
|
const timeoutId = setTimeout(() => {
|
|
379
382
|
this.off('message', checkMessageHandler);
|
|
383
|
+
this.abortController.signal.removeEventListener('abort', abort);
|
|
380
384
|
reject(new Error('Connection check timeout'));
|
|
381
385
|
}, 5000);
|
|
382
386
|
|
|
387
|
+
const abort = () => {
|
|
388
|
+
clearTimeout(timeoutId);
|
|
389
|
+
this.off('message', checkMessageHandler);
|
|
390
|
+
this.abortController.signal.removeEventListener('abort', abort);
|
|
391
|
+
reject(new Error('Connection check aborted', {cause: -1}));
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
this.abortController.signal.addEventListener('abort', abort);
|
|
395
|
+
|
|
383
396
|
const checkMessageHandler = (message) => {
|
|
384
397
|
if (message.type === 'keep_alive' && message.timestamp === testMessage.timestamp) {
|
|
385
398
|
clearTimeout(timeoutId);
|
|
399
|
+
this.abortController.signal.removeEventListener('abort', abort);
|
|
386
400
|
this.off('message', checkMessageHandler);
|
|
387
401
|
resolve();
|
|
388
402
|
}
|
|
@@ -395,4 +409,4 @@ class Iot {
|
|
|
395
409
|
}
|
|
396
410
|
}
|
|
397
411
|
|
|
398
|
-
export default Iot;
|
|
412
|
+
export default Iot;
|