@reactoo/watchtogether-sdk-js 2.7.55 → 2.7.56

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.55",
3
+ "version": "2.7.56",
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
@@ -44,23 +44,48 @@ let iot = function () {
44
44
 
45
45
  },
46
46
 
47
- onConnect: () => {
47
+ onConnect: (data, connectionId) => {
48
+
49
+ if(this.__privates.iot.currentConnectionId !== null && this.__privates.iot.currentConnectionId !== connectionId) {
50
+ return;
51
+ }
52
+
48
53
  this.__privates.iot.log('MQTT client connected');
49
54
  },
50
- onClosed: () => {
55
+ onClosed: (data, connectionId) => {
56
+
57
+ if(this.__privates.iot.currentConnectionId !== null && this.__privates.iot.currentConnectionId !== connectionId) {
58
+ return;
59
+ }
60
+
51
61
  this.__privates.iot.log('MQTT client closed');
52
62
  if(shouldBeConnected && !isReconnecting) {
53
63
  this.__privates.iot.log('Connection unexpectedly closed, reconnecting...');
54
64
  this.iot.__updateCredentials();
55
65
  }
56
66
  },
57
- onError: () => {
67
+ onError: (data, connectionId) => {
68
+
69
+ if(this.__privates.iot.currentConnectionId !== null && this.__privates.iot.currentConnectionId !== connectionId) {
70
+ return;
71
+ }
72
+
58
73
  this.__privates.iot.log('MQTT client error');
59
74
  },
60
- onDisconnect: () => {
75
+ onDisconnect: (data, connectionId) => {
76
+
77
+ if(this.__privates.iot.currentConnectionId !== null && this.__privates.iot.currentConnectionId !== connectionId) {
78
+ return;
79
+ }
80
+
61
81
  this.__privates.iot.log('MQTT client disconnect');
62
82
  },
63
- onInterrupt: () => {
83
+ onInterrupt: (data, connectionId) => {
84
+
85
+ if(this.__privates.iot.currentConnectionId !== null && this.__privates.iot.currentConnectionId !== connectionId) {
86
+ return;
87
+ }
88
+
64
89
  if(shouldBeConnected && !isReconnecting && !document.hidden && interruptCount > 10) {
65
90
  this.__privates.iot.log('Interrupt count exceeded, reconnecting...');
66
91
  this.iot.__updateCredentials();
@@ -68,13 +93,33 @@ let iot = function () {
68
93
  interruptCount++;
69
94
  this.__privates.iot.log('MQTT client interrupt');
70
95
  },
71
- onResume: () => {
96
+ onResume: (data, connectionId) => {
97
+
98
+ if(this.__privates.iot.currentConnectionId !== null && this.__privates.iot.currentConnectionId !== connectionId) {
99
+ return;
100
+ }
101
+
72
102
  this.__privates.iot.log('MQTT client resume');
73
103
  },
74
- onConnectionSuccess: () => {
104
+ onConnectionSuccess: (data, connectionId) => {
105
+
106
+ if(this.__privates.iot.currentConnectionId !== null && this.__privates.iot.currentConnectionId !== connectionId) {
107
+ return;
108
+ }
109
+
75
110
  this.__privates.iot.log('MQTT client connection_success');
76
111
  },
77
- onConnectionFailure: () => {
112
+ onConnectionFailure: (data, connectionId) => {
113
+
114
+ if(this.__privates.iot.currentConnectionId !== null && this.__privates.iot.currentConnectionId !== connectionId) {
115
+ return;
116
+ }
117
+
118
+ if(shouldBeConnected && !isReconnecting && !document.hidden && interruptCount > 10) {
119
+ this.__privates.iot.log('Interrupt count exceeded, reconnecting...');
120
+ this.iot.__updateCredentials();
121
+ }
122
+ interruptCount++;
78
123
  this.__privates.iot.log('MQTT client connection_failure');
79
124
  },
80
125
 
@@ -2,6 +2,7 @@ import { mqtt, iot } from 'aws-iot-device-sdk-v2';
2
2
  console.log('Worker: Starting up');
3
3
 
4
4
  let connection = null;
5
+ let currentConnectionId = null;
5
6
 
6
7
  self.onmessage = function(event) {
7
8
  const { type, params, topic, message } = event.data;
@@ -48,16 +49,18 @@ function connect(params) {
48
49
  configBuilder.with_reconnect_min_sec(1);
49
50
 
50
51
  const config = configBuilder.build();
51
-
52
52
  const client = new mqtt.MqttClient();
53
+
53
54
  connection = client.new_connection(config);
55
+ // generate random connection id
56
+ currentConnectionId = Math.floor(Math.random() * 1000000);
54
57
 
55
- setupConnectionListeners();
58
+ setupConnectionListeners(currentConnectionId);
56
59
 
57
60
  connection.connect()
58
61
  .then(() => {
59
62
  console.log('Worker: Connection successful');
60
- self.postMessage({ type: 'connect_result', data: {success: true} });
63
+ self.postMessage({ type: 'connect_result', data: {success: true, connectionId: currentConnectionId} });
61
64
  })
62
65
  .catch((error) => {
63
66
  console.error('Worker: Connection failed', error);
@@ -120,43 +123,69 @@ function send(topic, message) {
120
123
  }
121
124
  }
122
125
 
123
- function setupConnectionListeners() {
126
+ function setupConnectionListeners(connectionId) {
124
127
  connection.on('connect', () => {
125
- self.postMessage({ type: 'connect' });
128
+ if(connectionId !== currentConnectionId) {
129
+ console.log('Worker: Connection Id mismatch, ignoring connect event');
130
+ return;
131
+ }
132
+ self.postMessage({ type: 'connect', connectionId: connectionId});
126
133
  });
127
134
 
128
135
  connection.on('disconnect', () => {
129
- self.postMessage({ type: 'disconnect' });
136
+ if(connectionId !== currentConnectionId) {
137
+ console.log('Worker: Connection Id mismatch, ignoring disconnect event');
138
+ return;
139
+ }
140
+ self.postMessage({ type: 'disconnect', connectionId: connectionId });
130
141
  });
131
142
 
132
143
  connection.on('error', (error) => {
144
+ if(connectionId !== currentConnectionId) {
145
+ console.log('Worker: Connection Id mismatch, ignoring error event');
146
+ return;
147
+ }
133
148
  self.postMessage({ type: 'error', data: error });
134
149
  });
135
150
 
136
151
  connection.on('interrupt', (error) => {
137
- self.postMessage({ type: 'interrupt', data: error });
152
+ if(connectionId !== currentConnectionId) {
153
+ console.log('Worker: Connection Id mismatch, ignoring interrupt event');
154
+ return;
155
+ }
156
+ self.postMessage({ type: 'interrupt', data: error, connectionId: connectionId });
138
157
  });
139
158
 
140
159
  connection.on('resume', (error) => {
141
- self.postMessage({ type: 'resume', data: error });
160
+ if(connectionId !== currentConnectionId) {
161
+ console.log('Worker: Connection Id mismatch, ignoring resume event');
162
+ return;
163
+ }
164
+ self.postMessage({ type: 'resume', data: error, connectionId: connectionId });
142
165
  });
143
166
 
144
167
  connection.on('message', (topic, payload) => {
145
- self.postMessage({ type: 'message', data: { topic, payload } });
168
+ if(connectionId !== currentConnectionId) {
169
+ console.log('Worker: Connection Id mismatch, ignoring message event');
170
+ return;
171
+ }
172
+ self.postMessage({ type: 'message', data: { topic, payload }, connectionId: connectionId });
146
173
  });
147
174
 
148
175
  connection.on('connection_success', (error) => {
149
- self.postMessage({ type: 'connection_success', data: error });
176
+ if(connectionId !== currentConnectionId) {
177
+ console.log('Worker: Connection Id mismatch, ignoring connection_success event');
178
+ return;
179
+ }
180
+ self.postMessage({ type: 'connection_success', data: error, connectionId: connectionId });
150
181
  });
151
182
 
152
183
  connection.on('connection_failure', (error) => {
153
- self.postMessage({ type: 'connection_failure', data: error });
154
- });
155
-
156
- // Add a general error handler for uncaught exceptions
157
- self.addEventListener('error', (error) => {
158
- console.error('Worker: Uncaught error', error);
159
- self.postMessage({ type: 'uncaught_error', error: error.message });
184
+ if(connectionId !== currentConnectionId) {
185
+ console.log('Worker: Connection Id mismatch, ignoring connection_failure event');
186
+ return;
187
+ }
188
+ self.postMessage({ type: 'connection_failure', data: error, connectionId: connectionId });
160
189
  });
161
190
  }
162
191
 
@@ -11,6 +11,7 @@ class Iot {
11
11
  this.currentCredentialsExpirationStamp = null;
12
12
  this.lastConnectParams = null;
13
13
  this.subscribedTopics = new Set();
14
+ this.currentConnectionId = null;
14
15
  this.initWorker();
15
16
 
16
17
  if (enableDebugFlag) {
@@ -51,6 +52,7 @@ class Iot {
51
52
  clearTimeout(timeoutId);
52
53
  this.off('worker:connect_result', handleConnectResult);
53
54
  if (event.success) {
55
+ this.currentConnectionId = event.connectionId;
54
56
  resolve();
55
57
  } else {
56
58
  reject(new Error(event.error));
@@ -160,7 +162,7 @@ class Iot {
160
162
  }
161
163
 
162
164
  handleWorkerMessage(event) {
163
- const { type, data } = event.data;
165
+ const { type, data, connectionId } = event.data;
164
166
  switch (type) {
165
167
  case 'message':
166
168
  this.handleMessage(data.topic, new Uint8Array(data.payload));
@@ -172,7 +174,7 @@ class Iot {
172
174
  case 'resume':
173
175
  case 'connection_success':
174
176
  case 'connection_failure':
175
- this.emit(type, data);
177
+ this.emit(type, data, connectionId);
176
178
  break;
177
179
  case 'connect_result':
178
180
  case 'disconnect_result':