@reactoo/watchtogether-sdk-js 2.7.61 → 2.7.63

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.61",
3
+ "version": "2.7.63",
4
4
  "description": "Javascript SDK for Reactoo",
5
5
  "main": "dist/watchtogether-sdk.min.js",
6
6
  "module": "dist/watchtogether-sdk.min.js",
package/src/models/iot.js CHANGED
@@ -7,6 +7,7 @@ let iot = function () {
7
7
  let shouldBeConnected = false;
8
8
  let isReconnecting = false;
9
9
  let interruptCount = 0;
10
+ let subscriptionFailureCount = 0;
10
11
 
11
12
  return {
12
13
 
@@ -15,6 +16,7 @@ let iot = function () {
15
16
  __updateCredentials: () => {
16
17
  this.__privates.iot.log('Updating Credentials...');
17
18
  interruptCount = 0;
19
+ subscriptionFailureCount = 0;
18
20
  isReconnecting = true;
19
21
  this.iot.$emit('isReconnecting', isReconnecting, true);
20
22
  return this.iot.getCredentials()
@@ -86,6 +88,7 @@ let iot = function () {
86
88
  iotLogin: (subscribeToSuggestedTopics = true) => {
87
89
 
88
90
  interruptCount = 0;
91
+ subscriptionFailureCount = 0;
89
92
  shouldBeConnected = true;
90
93
 
91
94
  this.iot.__promise = new Promise((resolve, reject) => {
@@ -149,6 +152,7 @@ let iot = function () {
149
152
  }
150
153
 
151
154
  interruptCount = 0;
155
+ subscriptionFailureCount = 0;
152
156
  shouldBeConnected = false;
153
157
 
154
158
  this.iot.disableVisibilityChangeListener();
@@ -181,9 +185,21 @@ let iot = function () {
181
185
  // if subscription fails, remove the topic from the current topics
182
186
  return this.iot.__promise
183
187
  .then(() => this.__privates.iot.subscribe(topic))
184
- .catch(() => {
185
- __currentTopics.delete(topic);
186
- return Promise.reject('subscription_failed');
188
+ .catch((error) => {
189
+
190
+ if(error?.cause === -1) {
191
+ __currentTopics.delete(topic);
192
+ return Promise.reject('invalid_topic');
193
+ }
194
+
195
+ if(shouldBeConnected && !isReconnecting && subscriptionFailureCount > 5) {
196
+ this.__privates.iot.log('Subscription failure exceeded, reconnecting...');
197
+ this.iot.__updateCredentials();
198
+ return Promise.reject('subscription_failed');
199
+ }
200
+ subscriptionFailureCount++;
201
+
202
+ return this.subscribe(topic)
187
203
  });
188
204
  }
189
205
  },
@@ -5,15 +5,15 @@ let connection = null;
5
5
  let currentConnectionId = 1;
6
6
 
7
7
  self.onmessage = function(event) {
8
- const { type, params, topic, message } = event.data;
8
+ const { type, params, topic, message, stamp } = event.data;
9
9
  console.log(`Worker: Received message of type: ${type}`);
10
10
 
11
11
  switch (type) {
12
12
  case 'connect':
13
- connect(params);
13
+ connect(params, stamp);
14
14
  break;
15
15
  case 'disconnect':
16
- disconnect();
16
+ disconnect(stamp);
17
17
  break;
18
18
  case 'is_connected':
19
19
  isConnected();
@@ -22,10 +22,10 @@ self.onmessage = function(event) {
22
22
  // No action needed in the worker
23
23
  break;
24
24
  case 'subscribe':
25
- subscribe(topic);
25
+ subscribe(topic, stamp);
26
26
  break;
27
27
  case 'unsubscribe':
28
- unsubscribe(topic);
28
+ unsubscribe(topic, stamp);
29
29
  break;
30
30
  case 'send':
31
31
  send(topic, message);
@@ -33,7 +33,7 @@ self.onmessage = function(event) {
33
33
  }
34
34
  };
35
35
 
36
- function connect(params) {
36
+ function connect(params, stamp) {
37
37
  console.log('Worker: Attempting to connect');
38
38
  const { apiMqttUrl, apiMqttClientId, region, accessKeyId, secretAccessKey, sessionToken } = params;
39
39
 
@@ -60,15 +60,15 @@ function connect(params) {
60
60
  connection.connect()
61
61
  .then(() => {
62
62
  console.log('Worker: Connection successful');
63
- self.postMessage({ type: 'connect_result', data: {success: true, connectionId: currentConnectionId} });
63
+ self.postMessage({ type: 'connect_result', data: {success: true, connectionId: currentConnectionId, stamp} });
64
64
  })
65
65
  .catch((error) => {
66
66
  console.error('Worker: Connection failed', error);
67
- self.postMessage({ type: 'connect_result', data: {success: false, error: error.message} });
67
+ self.postMessage({ type: 'connect_result', data: {success: false, error: error.message, stamp} });
68
68
  });
69
69
  }
70
70
 
71
- function disconnect() {
71
+ function disconnect(stamp) {
72
72
  if (connection) {
73
73
  const connectionId = currentConnectionId;
74
74
  connection.disconnect()
@@ -77,18 +77,18 @@ function disconnect() {
77
77
  console.log('Worker: Connection Id mismatch, ignoring disconnect result', connectionId, currentConnectionId);
78
78
  return;
79
79
  }
80
- self.postMessage({ type: 'disconnect_result', data: {success: true} });
80
+ self.postMessage({ type: 'disconnect_result', data: {success: true, stamp} });
81
81
  })
82
82
  .catch((error) => {
83
83
  if(connectionId !== currentConnectionId) {
84
84
  console.log('Worker: Connection Id mismatch, ignoring disconnect result', connectionId, currentConnectionId);
85
85
  return;
86
86
  }
87
- self.postMessage({ type: 'disconnect_result', data: { success: false, error: error.message} });
87
+ self.postMessage({ type: 'disconnect_result', data: { success: false, error: error.message, stamp} });
88
88
  });
89
89
  connection = null;
90
90
  } else {
91
- self.postMessage({ type: 'disconnect_result', data: {success: true} });
91
+ self.postMessage({ type: 'disconnect_result', data: {success: true, stamp} });
92
92
  }
93
93
  }
94
94
 
@@ -97,31 +97,31 @@ function isConnected() {
97
97
  self.postMessage({ type: 'is_connected_result', data:{connected} });
98
98
  }
99
99
 
100
- function subscribe(topic) {
100
+ function subscribe(topic, stamp) {
101
101
  if (connection && connection.currentState === 0 && connection.desiredState === 0) {
102
102
  connection.subscribe(topic, mqtt.QoS.AtLeastOnce)
103
103
  .then(() => {
104
- self.postMessage({ type: 'subscribe_result', data: {success: true} });
104
+ self.postMessage({ type: 'subscribe_result', data: {success: true, stamp} });
105
105
  })
106
106
  .catch((error) => {
107
- self.postMessage({ type: 'subscribe_result', data: {success: false, error: error.message} });
107
+ self.postMessage({ type: 'subscribe_result', data: {success: false, error: error.message, stamp} });
108
108
  });
109
109
  } else {
110
- self.postMessage({ type: 'subscribe_result', data: {success: false, error: 'Not connected'} });
110
+ self.postMessage({ type: 'subscribe_result', data: {success: false, error: 'Not connected', stamp} });
111
111
  }
112
112
  }
113
113
 
114
- function unsubscribe(topic) {
114
+ function unsubscribe(topic, stamp) {
115
115
  if (connection && connection.currentState === 0 && connection.desiredState === 0) {
116
116
  connection.unsubscribe(topic)
117
117
  .then(() => {
118
- self.postMessage({ type: 'unsubscribe_result', data: {success: true} });
118
+ self.postMessage({ type: 'unsubscribe_result', data: {success: true, stamp} });
119
119
  })
120
120
  .catch((error) => {
121
- self.postMessage({ type: 'unsubscribe_result', data: {success: false, error: error.message} });
121
+ self.postMessage({ type: 'unsubscribe_result', data: {success: false, error: error.message, stamp} });
122
122
  });
123
123
  } else {
124
- self.postMessage({ type: 'unsubscribe_result', data: {success: false, error: 'Not connected'} });
124
+ self.postMessage({ type: 'unsubscribe_result', data: {success: false, error: 'Not connected', stamp} });
125
125
  }
126
126
  }
127
127
 
@@ -44,7 +44,15 @@ class Iot {
44
44
 
45
45
  return new Promise((resolve, reject) => {
46
46
 
47
+ const stamp = new Date().getTime();
48
+
47
49
  const handleConnectResult = (event) => {
50
+
51
+ if(event.stamp !== stamp) {
52
+ this.log('connect event stamp mismatch', event.stamp, stamp);
53
+ return;
54
+ }
55
+
48
56
  clearTimeout(timeoutId);
49
57
  this.off('worker:connect_result', handleConnectResult);
50
58
  if (event.success) {
@@ -64,7 +72,8 @@ class Iot {
64
72
 
65
73
  this.worker.postMessage({
66
74
  type: 'connect',
67
- params: this.lastConnectParams
75
+ params: this.lastConnectParams,
76
+ stamp: stamp
68
77
  });
69
78
  });
70
79
  });
@@ -75,7 +84,15 @@ class Iot {
75
84
  this.stopCredentialsExpirationCheck();
76
85
  return new Promise((resolve, reject) => {
77
86
 
87
+ const stamp = new Date().getTime();
88
+
78
89
  const handleDisconnectResult = (event) => {
90
+
91
+ if(event.stamp !== stamp) {
92
+ this.log('disconnect event stamp mismatch', event.stamp, stamp);
93
+ return;
94
+ }
95
+
79
96
  clearTimeout(timeoutId);
80
97
  this.off('worker:disconnect_result', handleDisconnectResult);
81
98
  if (event.success) {
@@ -91,7 +108,7 @@ class Iot {
91
108
  }, 5000);
92
109
 
93
110
  this.on('worker:disconnect_result', handleDisconnectResult);
94
- this.worker.postMessage({ type: 'disconnect' });
111
+ this.worker.postMessage({ type: 'disconnect', stamp: stamp });
95
112
  });
96
113
  }
97
114
 
@@ -116,7 +133,15 @@ class Iot {
116
133
  this.log('iot subscribe', topic);
117
134
  if (typeof topic === 'string' && topic.trim() !== '') {
118
135
  return new Promise((resolve, reject) => {
136
+
137
+ const stamp = new Date().getTime();
119
138
  const handleSubscribeResult = (event) => {
139
+
140
+ if(event.stamp !== stamp) {
141
+ this.log('subscribe event stamp mismatch', event.stamp, stamp);
142
+ return;
143
+ }
144
+
120
145
  this.off('worker:subscribe_result', handleSubscribeResult);
121
146
  if (event.success) {
122
147
  this.subscribedTopics.add(topic);
@@ -127,11 +152,11 @@ class Iot {
127
152
  };
128
153
 
129
154
  this.on('worker:subscribe_result', handleSubscribeResult);
130
- this.worker.postMessage({ type: 'subscribe', topic });
155
+ this.worker.postMessage({ type: 'subscribe', topic, stamp });
131
156
  });
132
157
  } else {
133
158
  this.log('Invalid topic:', topic);
134
- return Promise.reject(new Error('Invalid topic'));
159
+ return Promise.reject(new Error('Invalid topic', {cause: -1}));
135
160
  }
136
161
  }
137
162
 
@@ -139,7 +164,16 @@ class Iot {
139
164
  this.log('iot unsubscribe', topic);
140
165
  if (typeof topic === 'string' && topic.trim() !== '') {
141
166
  return new Promise((resolve, reject) => {
167
+
168
+ const stamp = new Date().getTime();
169
+
142
170
  const handleUnsubscribeResult = (event) => {
171
+
172
+ if(event.stamp !== stamp) {
173
+ this.log('unsubscribe event stamp mismatch', event.stamp, stamp);
174
+ return;
175
+ }
176
+
143
177
  this.off('worker:unsubscribe_result', handleUnsubscribeResult);
144
178
  if (event.success) {
145
179
  this.subscribedTopics.delete(topic);
@@ -150,7 +184,7 @@ class Iot {
150
184
  };
151
185
 
152
186
  this.on('worker:unsubscribe_result', handleUnsubscribeResult);
153
- this.worker.postMessage({ type: 'unsubscribe', topic });
187
+ this.worker.postMessage({ type: 'unsubscribe', topic, stamp });
154
188
  });
155
189
  } else {
156
190
  this.log('Invalid topic:', topic);