@reactoo/watchtogether-sdk-js 2.7.69 → 2.7.70

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.69",
3
+ "version": "2.7.70",
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
@@ -99,7 +99,7 @@ let iot = function () {
99
99
  interruptCount = 0;
100
100
  subscriptionFailureCount = 0;
101
101
  shouldBeConnected = true;
102
-
102
+ this.__privates.iot.initWorker();
103
103
  this.iot.__promise = new Promise((resolve, reject) => {
104
104
  this.iot.getCredentials()
105
105
  .then(response => {
@@ -44,13 +44,16 @@ class Iot {
44
44
  })
45
45
  .then(() => {
46
46
  this.log('iot connect');
47
+
48
+ if(!this.worker) {
49
+ this.log('Worker not initialized');
50
+ return Promise.reject(new Error('Worker not initialized', {cause: -1}));
51
+ }
52
+
47
53
  this.abortController = new AbortController();
48
- this.initWorker();
49
54
  this.startCredentialsExpirationCheck(expiration);
50
55
  this.lastConnectParams = { apiMqttUrl, apiMqttClientId, region, accessKeyId, secretAccessKey, sessionToken, expiration };
51
-
52
56
  return new Promise((resolve, reject) => {
53
-
54
57
  const stamp = new Date().getTime();
55
58
 
56
59
  const handleConnectResult = (event) => {
@@ -76,7 +79,7 @@ class Iot {
76
79
 
77
80
  this.on('worker:connect_result', handleConnectResult);
78
81
 
79
- this.worker.postMessage({
82
+ this.worker?.postMessage({
80
83
  type: 'connect',
81
84
  params: this.lastConnectParams,
82
85
  stamp: stamp
@@ -89,6 +92,12 @@ class Iot {
89
92
  this.log('iot disconnect');
90
93
  this.stopCredentialsExpirationCheck();
91
94
  this.abortController?.abort();
95
+
96
+ if(!this.worker) {
97
+ this.log('Worker not initialized');
98
+ return Promise.reject(new Error('Worker not initialized', {cause: -1}));
99
+ }
100
+
92
101
  return new Promise((resolve, reject) => {
93
102
 
94
103
  const stamp = new Date().getTime();
@@ -115,7 +124,7 @@ class Iot {
115
124
  }, 5000);
116
125
 
117
126
  this.on('worker:disconnect_result', handleDisconnectResult);
118
- this.worker.postMessage({ type: 'disconnect', stamp: stamp });
127
+ this.worker?.postMessage({ type: 'disconnect', stamp: stamp });
119
128
  });
120
129
  }
121
130
 
@@ -127,20 +136,25 @@ class Iot {
127
136
  };
128
137
 
129
138
  this.on('worker:is_connected_result', handleIsConnectedResult);
130
- this.worker.postMessage({ type: 'is_connected' });
139
+ this.worker?.postMessage({ type: 'is_connected' });
131
140
  });
132
141
  }
133
142
 
134
143
  clearTopics() {
135
144
  this.subscribedTopics.clear();
136
- this.worker.postMessage({ type: 'clear_topics' });
145
+ this.worker?.postMessage({ type: 'clear_topics' });
137
146
  }
138
147
 
139
148
  subscribe(topic) {
140
149
  this.log('iot subscribe', topic);
141
150
  if (typeof topic === 'string' && topic.trim() !== '') {
142
- return new Promise((resolve, reject) => {
143
151
 
152
+ if(!this.worker) {
153
+ this.log('Worker not initialized');
154
+ return Promise.reject(new Error('Worker not initialized', {cause: -1}));
155
+ }
156
+
157
+ return new Promise((resolve, reject) => {
144
158
  const stamp = new Date().getTime();
145
159
  const handleSubscribeResult = (event) => {
146
160
 
@@ -159,7 +173,7 @@ class Iot {
159
173
  };
160
174
 
161
175
  this.on('worker:subscribe_result', handleSubscribeResult);
162
- this.worker.postMessage({ type: 'subscribe', topic, stamp });
176
+ this.worker?.postMessage({ type: 'subscribe', topic, stamp });
163
177
  });
164
178
  } else {
165
179
  this.log('Invalid topic:', topic);
@@ -170,6 +184,12 @@ class Iot {
170
184
  unsubscribe(topic) {
171
185
  this.log('iot unsubscribe', topic);
172
186
  if (typeof topic === 'string' && topic.trim() !== '') {
187
+
188
+ if(!this.worker) {
189
+ this.log('Worker not initialized');
190
+ return Promise.reject(new Error('Worker not initialized', {cause: -1}));
191
+ }
192
+
173
193
  return new Promise((resolve, reject) => {
174
194
 
175
195
  const stamp = new Date().getTime();
@@ -191,7 +211,7 @@ class Iot {
191
211
  };
192
212
 
193
213
  this.on('worker:unsubscribe_result', handleUnsubscribeResult);
194
- this.worker.postMessage({ type: 'unsubscribe', topic, stamp });
214
+ this.worker?.postMessage({ type: 'unsubscribe', topic, stamp });
195
215
  });
196
216
  } else {
197
217
  this.log('Invalid topic:', topic);
@@ -202,7 +222,7 @@ class Iot {
202
222
  send(topic, message) {
203
223
  this.log('iot send', topic, message);
204
224
  let msg = typeof message === 'object' ? JSON.stringify(message) : message;
205
- this.worker.postMessage({ type: 'send', topic, message: msg });
225
+ this.worker?.postMessage({ type: 'send', topic, message: msg });
206
226
  }
207
227
 
208
228
  handleWorkerMessage(event) {