@reactoo/watchtogether-sdk-js 2.7.33 → 2.7.35

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.33",
3
+ "version": "2.7.35",
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
@@ -31,11 +31,9 @@ let iot = function () {
31
31
 
32
32
  onConnect: () => {
33
33
  this.__privates.iot.log('MQTT client connected');
34
- this.iot.setupVisibilityChangeListener();
35
34
  },
36
35
  onClosed: () => {
37
36
  this.__privates.iot.log('MQTT client closed');
38
- this.iot.disableVisibilityChangeListener();
39
37
  },
40
38
  onError: () => {
41
39
  this.__privates.iot.log('MQTT client error');
@@ -80,6 +78,7 @@ let iot = function () {
80
78
  let __currentTopicsCopy = new Set(__currentTopics); // Keep this line
81
79
  __currentTopics.clear();
82
80
 
81
+ this.iot.setupVisibilityChangeListener();
83
82
  this.iot.$on('connect', this.iot.onConnect, this);
84
83
  this.iot.$on('closed', this.iot.onClosed, this);
85
84
  this.iot.$on('error', this.iot.onError, this);
@@ -116,6 +115,7 @@ let iot = function () {
116
115
  return this.__privates.iot.disconnect()
117
116
  .then(() => {
118
117
 
118
+ this.iot.disableVisibilityChangeListener();
119
119
  this.iot.$off('connect', this.iot.onConnect, this);
120
120
  this.iot.$off('closed', this.iot.onClosed, this);
121
121
  this.iot.$off('error', this.iot.onError, this);
@@ -6,7 +6,6 @@ class Iot {
6
6
  constructor(enableDebugFlag) {
7
7
  Object.assign(this, emitter());
8
8
  this.decoder = new TextDecoder('utf-8');
9
- this.connectionActive = false;
10
9
  this.log = Iot.noop;
11
10
  // Remove: this.debugFlag = enableDebugFlag;
12
11
  this.credentialsExpirationCheckIntervalId = null;
@@ -26,46 +25,6 @@ class Iot {
26
25
  this.log = console.log.bind(console);
27
26
  }
28
27
 
29
- onConnect() {
30
- this.connectionActive = true;
31
- this.emit('connect');
32
- }
33
- onClosed(error) {
34
- this.connectionActive = false;
35
- this.emit('closed', error);
36
- }
37
-
38
- onDisconnect() {
39
- this.connectionActive = false;
40
- this.emit('disconnect');
41
- }
42
- onError(error) {
43
- this.connectionActive = false;
44
- this.emit('error', error);
45
- }
46
-
47
- onInterrupt(error) {
48
- this.connectionActive = false;
49
- this.emit('interrupt', error);
50
- }
51
- onResume(r) {
52
- this.connectionActive = true;
53
- this.emit('resume', r);
54
- }
55
-
56
- onMessage(topic, payload) {
57
- this.handleMessage(topic, new Uint8Array(payload));
58
- }
59
- onConnectionSuccess(r) {
60
- this.connectionActive = true;
61
- this.emit('connection_success', r);
62
- }
63
- onConnectionFailure(error) {
64
- this.connectionActive = false;
65
- this.emit('connection_failure', error);
66
- }
67
-
68
-
69
28
  connect(apiMqttUrl, apiMqttClientId, region, accessKeyId, secretAccessKey, sessionToken, expiration) {
70
29
  this.log('iot connect called, we disconnect first just to be sure');
71
30
  return this.disconnect()
@@ -88,15 +47,42 @@ class Iot {
88
47
 
89
48
  const client = new mqtt.MqttClient();
90
49
  this.connection = client.new_connection(config);
91
- this.connection.on('connect', this.onConnect, this);
92
- this.connection.on('closed', this.onClosed, this);
93
- this.connection.on('disconnect', this.onDisconnect, this);
94
- this.connection.on('error', this.onError, this);
95
- this.connection.on('interrupt', this.onInterrupt, this);
96
- this.connection.on('resume', this.onResume, this);
97
- this.connection.on('message', this.onMessage, this);
98
- this.connection.on('connection_success', this.onConnectionSuccess, this);
99
- this.connection.on('connection_failure', this.onConnectionFailure, this);
50
+
51
+ this.connection.on('connect', () => {
52
+ this.emit('connect');
53
+ });
54
+
55
+ this.connection.on('closed', (error) => {
56
+ this.emit('closed', error);
57
+ });
58
+
59
+ this.connection.on('disconnect', () => {
60
+ this.emit('disconnect');
61
+ });
62
+
63
+ this.connection.on('error', (error) => {
64
+ this.emit('error', error);
65
+ });
66
+
67
+ this.connection.on('interrupt', (error) => {
68
+ this.emit('interrupt', error);
69
+ });
70
+
71
+ this.connection.on('resume', (error) => {
72
+ this.emit('resume', error);
73
+ });
74
+
75
+ this.connection.on('message', (topic, payload) => {
76
+ this.handleMessage(topic, new Uint8Array(payload));
77
+ });
78
+
79
+ this.connection.on('connection_success', (error) => {
80
+ this.emit('connection_success', error);
81
+ });
82
+
83
+ this.connection.on('connection_failure', (error) => {
84
+ this.emit('connection_failure', error);
85
+ });
100
86
 
101
87
  return this.connection.connect()
102
88
  .then(() => {
@@ -111,13 +97,12 @@ class Iot {
111
97
  this.log('iot disconnect');
112
98
  this.stopCredentialsExpirationCheck();
113
99
  if (this.connection) {
114
- this.connectionActive = false;
115
100
  return this.connection.disconnect();
116
101
  } else return Promise.resolve();
117
102
  }
118
103
 
119
104
  isConnected() {
120
- return this.connectionActive;
105
+ return this.connection && this.connection.currentState === 0 && this.connection.desiredState === 0;
121
106
  }
122
107
 
123
108
  subscribe(topic) {
@@ -250,7 +235,8 @@ class Iot {
250
235
  this.currentCredentialsExpirationStamp = new Date(expiration).getTime();
251
236
  this.credentialsExpirationCheckIntervalId = setInterval(() => {
252
237
  const currentTimeStamp = new Date().getTime();
253
- if(this.currentCredentialsExpirationStamp - currentTimeStamp <= 300000) {
238
+ // update 15 minutes before expiration
239
+ if(this.currentCredentialsExpirationStamp - currentTimeStamp <= 900000) {
254
240
  this.emit('updateCredentials');
255
241
  }
256
242
  }, 5000);
@@ -284,7 +270,7 @@ class Iot {
284
270
 
285
271
  checkConnection() {
286
272
  return new Promise((resolve, reject) => {
287
- if (!this.connection || !this.connectionActive) {
273
+ if (!this.connection || !(this.connection.currentState === 0 && this.connection.desiredState === 0)) {
288
274
  reject(new Error('Not connected'));
289
275
  return;
290
276
  }