@reactoo/watchtogether-sdk-js 2.7.62 → 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.62",
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
  },
@@ -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);
@@ -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
 
@@ -133,7 +133,15 @@ class Iot {
133
133
  this.log('iot subscribe', topic);
134
134
  if (typeof topic === 'string' && topic.trim() !== '') {
135
135
  return new Promise((resolve, reject) => {
136
+
137
+ const stamp = new Date().getTime();
136
138
  const handleSubscribeResult = (event) => {
139
+
140
+ if(event.stamp !== stamp) {
141
+ this.log('subscribe event stamp mismatch', event.stamp, stamp);
142
+ return;
143
+ }
144
+
137
145
  this.off('worker:subscribe_result', handleSubscribeResult);
138
146
  if (event.success) {
139
147
  this.subscribedTopics.add(topic);
@@ -144,11 +152,11 @@ class Iot {
144
152
  };
145
153
 
146
154
  this.on('worker:subscribe_result', handleSubscribeResult);
147
- this.worker.postMessage({ type: 'subscribe', topic });
155
+ this.worker.postMessage({ type: 'subscribe', topic, stamp });
148
156
  });
149
157
  } else {
150
158
  this.log('Invalid topic:', topic);
151
- return Promise.reject(new Error('Invalid topic'));
159
+ return Promise.reject(new Error('Invalid topic', {cause: -1}));
152
160
  }
153
161
  }
154
162
 
@@ -156,7 +164,16 @@ class Iot {
156
164
  this.log('iot unsubscribe', topic);
157
165
  if (typeof topic === 'string' && topic.trim() !== '') {
158
166
  return new Promise((resolve, reject) => {
167
+
168
+ const stamp = new Date().getTime();
169
+
159
170
  const handleUnsubscribeResult = (event) => {
171
+
172
+ if(event.stamp !== stamp) {
173
+ this.log('unsubscribe event stamp mismatch', event.stamp, stamp);
174
+ return;
175
+ }
176
+
160
177
  this.off('worker:unsubscribe_result', handleUnsubscribeResult);
161
178
  if (event.success) {
162
179
  this.subscribedTopics.delete(topic);
@@ -167,7 +184,7 @@ class Iot {
167
184
  };
168
185
 
169
186
  this.on('worker:unsubscribe_result', handleUnsubscribeResult);
170
- this.worker.postMessage({ type: 'unsubscribe', topic });
187
+ this.worker.postMessage({ type: 'unsubscribe', topic, stamp });
171
188
  });
172
189
  } else {
173
190
  this.log('Invalid topic:', topic);