@reactoo/watchtogether-sdk-js 2.7.40 → 2.7.41

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reactoo/watchtogether-sdk-js",
3
- "version": "2.7.40",
3
+ "version": "2.7.41",
4
4
  "description": "Javascript SDK for Reactoo",
5
5
  "main": "src/index.js",
6
6
  "unpkg": "dist/watchtogether-sdk.min.js",
package/src/models/iot.js CHANGED
@@ -13,6 +13,7 @@ let iot = function () {
13
13
  __promise: null,
14
14
 
15
15
  __updateCredentials: () => {
16
+ this.__privates.iot.log('Updating Credentials...');
16
17
  interruptCount = 0;
17
18
  isReconnecting = true;
18
19
  return this.iot.getCredentials()
@@ -23,10 +24,7 @@ let iot = function () {
23
24
  response.data.credentials.expiration
24
25
  ))
25
26
  .catch(error => {
26
- console.log('Failed to update credentials:', error);
27
27
  this.__privates.iot.log('Failed to update credentials:', error);
28
- // Let the error propagate to be handled by the Iot class
29
- throw error;
30
28
  })
31
29
  .finally(() => {
32
30
  isReconnecting = false;
@@ -45,6 +43,7 @@ let iot = function () {
45
43
  onClosed: () => {
46
44
  this.__privates.iot.log('MQTT client closed');
47
45
  if(shouldBeConnected && !isReconnecting) {
46
+ this.__privates.iot.log('Connection unexpectedly closed, reconnecting...');
48
47
  this.iot.__updateCredentials();
49
48
  }
50
49
  },
@@ -56,6 +55,7 @@ let iot = function () {
56
55
  },
57
56
  onInterrupt: () => {
58
57
  if(shouldBeConnected && !isReconnecting && !document.hidden && interruptCount > 10) {
58
+ this.__privates.iot.log('Interrupt count exceeded, reconnecting...');
59
59
  this.iot.__updateCredentials();
60
60
  }
61
61
  interruptCount++;
@@ -109,6 +109,7 @@ let iot = function () {
109
109
  this.iot.$on('resume', this.iot.onResume, this);
110
110
  this.iot.$on('connection_success', this.iot.onConnectionSuccess, this);
111
111
  this.iot.$on('connection_failure', this.iot.onConnectionFailure, this);
112
+ this.iot.$on('updateCredentials', this.iot.__updateCredentials, this);
112
113
 
113
114
  this.iot.__promise
114
115
  .then(([suggestedTopic, instance]) => {
@@ -118,10 +119,6 @@ let iot = function () {
118
119
  let topics = new Set([...suggestedTopic, ...__currentTopicsCopy]); // Filter out any undefined or null topics
119
120
  return Promise.all(Array.from(topics).map(topic => this.iot.subscribe(topic))).then(() => instance)
120
121
  })
121
- .then((instance) => {
122
- this.iot.$on('updateCredentials', this.iot.__updateCredentials, this);
123
- return instance;
124
- })
125
122
  .catch((error) => {
126
123
  this.__privates.iot.log('MQTT Login Error:', error);
127
124
  throw error;
@@ -151,6 +148,7 @@ let iot = function () {
151
148
  this.iot.$off('resume', this.iot.onResume, this);
152
149
  this.iot.$off('connection_success', this.iot.onConnectionSuccess, this);
153
150
  this.iot.$off('connection_failure', this.iot.onConnectionFailure, this);
151
+ this.iot.$off('updateCredentials', this.iot.__updateCredentials, this);
154
152
 
155
153
  this.iot.__promise = null;
156
154
  return true;
@@ -200,6 +198,12 @@ let iot = function () {
200
198
  checkConnection: () => {
201
199
  return this.__privates.iot.checkConnection()
202
200
  .catch(error => {
201
+
202
+ if(!shouldBeConnected || isReconnecting) {
203
+ this.__privates.iot.log('Ignoring connection check failure, not connected');
204
+ return Promise.resolve();
205
+ }
206
+
203
207
  this.__privates.iot.log('Connection check failed:', error);
204
208
  return this.iot.__updateCredentials();
205
209
  });
@@ -208,8 +212,14 @@ let iot = function () {
208
212
  setupKeepAliveInterval: () => {
209
213
  clearInterval(keepAliveIntervalId);
210
214
  keepAliveIntervalId = setInterval(() => {
211
- this.__privates.iot.keepAliveMessage();
212
- }, 30000);
215
+ this.__privates.iot.checkConnection()
216
+ .catch(error => {
217
+ if(!document.hidden && shouldBeConnected && !isReconnecting) {
218
+ this.__privates.iot.log('Keepalive failed:', error);
219
+ this.iot.__updateCredentials();
220
+ }
221
+ });
222
+ }, 15000);
213
223
  },
214
224
 
215
225
  disableKeepAliveInterval: () => {
@@ -29,6 +29,7 @@ class Iot {
29
29
  return this.disconnect()
30
30
  .finally(() => {
31
31
  this.log('iot connect');
32
+ this.startCredentialsExpirationCheck(expiration);
32
33
  this.lastConnectParams = { apiMqttUrl, apiMqttClientId, region, accessKeyId, secretAccessKey, sessionToken, expiration };
33
34
 
34
35
  const configBuilder = iot.AwsIotMqttConnectionConfigBuilder.new_with_websockets();
@@ -83,11 +84,7 @@ class Iot {
83
84
  this.emit('connection_failure', error);
84
85
  });
85
86
 
86
- return this.connection.connect()
87
- .then(() => {
88
- this.startCredentialsExpirationCheck(expiration);
89
- });
90
-
87
+ return this.connection.connect();
91
88
  });
92
89
  }
93
90
 
@@ -237,6 +234,7 @@ class Iot {
237
234
  const currentTimeStamp = new Date().getTime();
238
235
  // update 15 minutes before expiration
239
236
  if(this.currentCredentialsExpirationStamp - currentTimeStamp <= 900000) {
237
+ this.log('iot credentials expired, updating');
240
238
  this.emit('updateCredentials');
241
239
  }
242
240
  }, 5000);
@@ -263,7 +261,6 @@ class Iot {
263
261
  ).then(() => {
264
262
  // Resubscribe to topics
265
263
  currentTopics.forEach(topic => this.subscribe(topic));
266
- this.startCredentialsExpirationCheck(expiration);
267
264
  })
268
265
  }
269
266
 
@@ -292,7 +289,7 @@ class Iot {
292
289
  }
293
290
 
294
291
  const testMessage = {
295
- type: 'connection_check',
292
+ type: 'keep_alive',
296
293
  timestamp: Date.now()
297
294
  };
298
295
 
@@ -302,7 +299,7 @@ class Iot {
302
299
  }, 5000); // 5 seconds timeout
303
300
 
304
301
  const checkMessageHandler = (message) => {
305
- if (message.type === 'connection_check' && message.timestamp === testMessage.timestamp) {
302
+ if (message.type === 'keep_alive' && message.timestamp === testMessage.timestamp) {
306
303
  clearTimeout(timeoutId);
307
304
  this.off('message', checkMessageHandler);
308
305
  resolve();
@@ -320,34 +317,6 @@ class Iot {
320
317
  }
321
318
  });
322
319
  }
323
-
324
- keepAliveMessage() {
325
- if (this.subscribedTopics.size === 0) {
326
- this.log('No subscribed topics available for keep alive message');
327
- return;
328
- }
329
- // Find a suitable topic for the connection check
330
- let suitableTopic = Array.from(this.subscribedTopics).find(topic => topic.indexOf('user') > -1);
331
-
332
- if(!suitableTopic) {
333
- suitableTopic = Array.from(this.subscribedTopics).find(topic => topic.indexOf('room') > -1 && topic !== 'wt/instanceroom/reactooDemo');
334
- }
335
-
336
- if (!suitableTopic) {
337
- this.log('No suitable topic found for keep alive message');
338
- return;
339
- }
340
- const testMessage = {
341
- type: 'keep_alive',
342
- timestamp: Date.now()
343
- };
344
- try {
345
- this.send(suitableTopic, testMessage);
346
- }
347
- catch (error) {
348
- this.log(`Keep-alive error: ${error.message}`);
349
- }
350
- }
351
320
  }
352
321
 
353
322
  export default Iot;