@reactoo/watchtogether-sdk-js 2.8.48 → 2.8.52

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.8.48",
3
+ "version": "2.8.52",
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,19 +7,12 @@ let iot = function () {
7
7
  let keepAliveIntervalId = null;
8
8
  let shouldBeConnected = false;
9
9
  let isConnecting = false;
10
- let interruptCount = 0;
11
10
  let subscriptionFailureCount = 0;
12
11
 
13
12
  return {
14
13
 
15
14
  __promise: null,
16
15
 
17
- __createNewWorker: () => {
18
- this.__privates.iot.log('Creating new worker...');
19
- this.__privates.iot.terminateWorker();
20
- this.__privates.iot.initWorker();
21
- },
22
-
23
16
  __updateCredentials: () => {
24
17
 
25
18
  if(isConnecting) {
@@ -28,7 +21,6 @@ let iot = function () {
28
21
  }
29
22
 
30
23
  this.__privates.iot.log('Updating Credentials...');
31
- interruptCount = 0;
32
24
  subscriptionFailureCount = 0;
33
25
  isConnecting = true;
34
26
 
@@ -37,7 +29,8 @@ let iot = function () {
37
29
  response.data.credentials.accessKeyId,
38
30
  response.data.credentials.secretAccessKey,
39
31
  response.data.credentials.sessionToken,
40
- response.data.credentials.expiration
32
+ response.data.credentials.expiration,
33
+ __currentTopics
41
34
  ))
42
35
  .then((r) => {
43
36
  isConnecting = false;
@@ -57,46 +50,38 @@ let iot = function () {
57
50
  return this.__privates.auth.__client
58
51
  .then(client => client.apis.auth.iotSignIn({}, {requestBody: {suggestedTopics:true, domain: location.hostname}}))
59
52
  },
60
-
61
- onConnect: (data, connectionId) => {
62
- this.__privates.iot.log('MQTT client connected');
63
- },
64
- onClosed: (data, connectionId) => {
65
- this.__privates.iot.log('MQTT client closed');
66
- if(shouldBeConnected && !isConnecting) {
67
- this.__privates.iot.log('Connection unexpectedly closed, reconnecting...');
68
- this.iot.__createNewWorker();
69
- this.iot.__updateCredentials();
70
- }
53
+ onStopped: (data, connectionId) => {
54
+ this.__privates.iot.log('MQTT client disconnected');
71
55
  },
72
56
  onError: (data, connectionId) => {
73
- this.__privates.iot.log('MQTT client error');
57
+ this.__privates.iot.log('MQTT client error');
58
+ if(shouldBeConnected && !isConnecting) {
59
+ this.__privates.iot.log('Connection unexpectedly closed, reconnecting...');
60
+ this.iot.__updateCredentials();
61
+ }
74
62
  },
75
63
  onDisconnect: (data, connectionId) => {
76
- this.__privates.iot.log('MQTT client disconnect');
77
- },
78
- onInterrupt: (data, connectionId) => {
79
- if(shouldBeConnected && !isConnecting && interruptCount > 10) {
80
- this.__privates.iot.log('Interrupt count exceeded, reconnecting...');
81
- this.iot.__createNewWorker();
82
- this.iot.__updateCredentials();
83
- }
84
- interruptCount++;
85
- this.__privates.iot.log('MQTT client interrupt');
86
- },
87
- onResume: (data, connectionId) => {
88
- this.__privates.iot.log('MQTT client resume');
64
+ this.__privates.iot.log('MQTT client closed');
65
+ if(shouldBeConnected && !isConnecting) {
66
+ this.__privates.iot.log('Connection unexpectedly closed, reconnecting...');
67
+ this.iot.__updateCredentials();
68
+ }
89
69
  },
70
+ onAttemptingConnect: (data, connectionId) => {
71
+ this.__privates.iot.log('MQTT client attempting_connect');
72
+ },
90
73
  onConnectionSuccess: (data, connectionId) => {
91
74
  this.__privates.iot.log('MQTT client connection_success');
92
75
  },
93
76
  onConnectionFailure: (data, connectionId) => {
94
- if(shouldBeConnected && !isConnecting && interruptCount > 10) {
95
- this.__privates.iot.log('Interrupt count exceeded, reconnecting...');
96
- this.iot.__createNewWorker();
77
+
78
+ // not having !isConnecting here because isConnecting flips to false after this event so we wouldn't retry connection
79
+ // to stop retrying connection user needs to call iotLogout which sets shouldBeConnected to false
80
+
81
+ if(shouldBeConnected) {
82
+ this.__privates.iot.log('Retrying connection...');
97
83
  this.iot.__updateCredentials();
98
84
  }
99
- interruptCount++;
100
85
  this.__privates.iot.log('MQTT client connection_failure');
101
86
  },
102
87
 
@@ -107,12 +92,18 @@ let iot = function () {
107
92
  return this.iot.__promise;
108
93
  }
109
94
 
110
- interruptCount = 0;
111
95
  subscriptionFailureCount = 0;
112
96
  shouldBeConnected = true;
113
97
  isConnecting = true;
114
98
 
115
- this.iot.__createNewWorker();
99
+ this.iot.$on('stopped', this.iot.onStopped, this);
100
+ this.iot.$on('error', this.iot.onError, this);
101
+ this.iot.$on('disconnection', this.iot.onDisconnect, this);
102
+ this.iot.$on('attemptingConnect', this.iot.onAttemptingConnect, this);
103
+ this.iot.$on('connectionSuccess', this.iot.onConnectionSuccess, this);
104
+ this.iot.$on('connectionFailure', this.iot.onConnectionFailure, this);
105
+ this.iot.$on('updateCredentials', this.iot.__updateCredentials, this);
106
+
116
107
  this.iot.__promise = new Promise((resolve, reject) => {
117
108
  this.iot.getCredentials()
118
109
  .then(response => {
@@ -141,16 +132,6 @@ let iot = function () {
141
132
 
142
133
  __currentTopics.clear();
143
134
 
144
- this.iot.$on('connect', this.iot.onConnect, this);
145
- this.iot.$on('closed', this.iot.onClosed, this);
146
- this.iot.$on('error', this.iot.onError, this);
147
- this.iot.$on('disconnect', this.iot.onDisconnect, this);
148
- this.iot.$on('interrupt', this.iot.onInterrupt, this);
149
- this.iot.$on('resume', this.iot.onResume, this);
150
- this.iot.$on('connection_success', this.iot.onConnectionSuccess, this);
151
- this.iot.$on('connection_failure', this.iot.onConnectionFailure, this);
152
- this.iot.$on('updateCredentials', this.iot.__updateCredentials, this);
153
-
154
135
  this.iot.__promise
155
136
  .then(([suggestedTopic, instance]) => {
156
137
  if(!subscribeToSuggestedTopics) {
@@ -166,41 +147,39 @@ let iot = function () {
166
147
  })
167
148
  .catch((error) => {
168
149
  this.__privates.iot.log('MQTT Login Error:', error);
169
- throw error;
170
150
  });
171
151
 
172
- return this.iot.__promise;
152
+ // we're not returning the promise because on error we will retry connection indefinitely
153
+ return true;
173
154
  },
174
155
 
175
156
  iotLogout: (keepCurrentTopics = false) => {
176
157
 
177
158
  if(!keepCurrentTopics) {
178
159
  __currentTopics.clear();
179
- this.__privates.iot.clearTopics();
180
160
  }
181
161
 
182
- interruptCount = 0;
183
162
  subscriptionFailureCount = 0;
184
163
  shouldBeConnected = false;
185
164
 
186
165
  this.iot.disableVisibilityChangeListener();
187
166
  this.iot.disableKeepAliveInterval();
188
167
 
189
- return this.__privates.iot.disconnect()
168
+ this.__privates.iot.disconnect()
190
169
  .finally(() => {
191
- this.__privates.iot.terminateWorker();
192
- this.iot.$off('connect', this.iot.onConnect, this);
193
- this.iot.$off('closed', this.iot.onClosed, this);
194
- this.iot.$off('error', this.iot.onError, this);
195
- this.iot.$off('disconnect', this.iot.onDisconnect, this);
196
- this.iot.$off('interrupt', this.iot.onInterrupt, this);
197
- this.iot.$off('resume', this.iot.onResume, this);
198
- this.iot.$off('connection_success', this.iot.onConnectionSuccess, this);
199
- this.iot.$off('connection_failure', this.iot.onConnectionFailure, this);
200
- this.iot.$off('updateCredentials', this.iot.__updateCredentials, this);
170
+ this.iot.$off('stopped', this.iot.onStopped, this);
171
+ this.iot.$off('error', this.iot.onError, this);
172
+ this.iot.$off('disconnection', this.iot.onDisconnect, this);
173
+ this.iot.$off('attemptingConnect', this.iot.onAttemptingConnect, this);
174
+ this.iot.$off('connectionSuccess', this.iot.onConnectionSuccess, this);
175
+ this.iot.$off('connectionFailure', this.iot.onConnectionFailure, this);
176
+ this.iot.$off('updateCredentials', this.iot.__updateCredentials, this);
201
177
  this.iot.__promise = null;
202
178
  return true;
203
179
  });
180
+
181
+ // we're not returning the promise because we're not returning anything useful anyway
182
+ return true;
204
183
  },
205
184
 
206
185
  isConnected: () => {
@@ -228,7 +207,6 @@ let iot = function () {
228
207
 
229
208
  if(shouldBeConnected && !isConnecting && subscriptionFailureCount > 5) {
230
209
  this.__privates.iot.log('Subscription failure exceeded, reconnecting...');
231
- this.iot.__createNewWorker();
232
210
  this.iot.__updateCredentials();
233
211
  return Promise.reject('subscription_failed');
234
212
  }
@@ -278,11 +256,11 @@ let iot = function () {
278
256
  },
279
257
 
280
258
  checkConnection: () => {
281
- return this.__privates.iot.checkConnection()
259
+ return this.__privates.iot.checkConnection(__currentTopics)
282
260
  .catch(error => {
283
261
 
284
262
  if(error?.cause === -1) {
285
- this.__privates.iot.log("We don't have a topic to check connection with");
263
+ this.__privates.iot.log("We don't have a topic to check connection with or internet is down");
286
264
  return;
287
265
  }
288
266
 
@@ -297,7 +275,7 @@ let iot = function () {
297
275
  }
298
276
 
299
277
  this.__privates.iot.log('Connection check failed:', error);
300
- this.iot.__createNewWorker();
278
+ this.iot.$emit('iotConnectionLost');
301
279
  return this.iot.__updateCredentials();
302
280
  });
303
281
  },
@@ -308,20 +286,7 @@ let iot = function () {
308
286
 
309
287
  clearInterval(keepAliveIntervalId);
310
288
  keepAliveIntervalId = setInterval(() => {
311
- this.__privates.iot.checkConnection()
312
- .catch(error => {
313
-
314
- if(error?.cause === -1) {
315
- this.__privates.iot.log("We don't have a topic to check connection with or check was aborted");
316
- return;
317
- }
318
-
319
- if(shouldBeConnected && !isConnecting) {
320
- this.__privates.iot.log('Keepalive failed:', error);
321
- this.iot.__createNewWorker();
322
- this.iot.__updateCredentials();
323
- }
324
- });
289
+ this.iot.checkConnection();
325
290
  }, 15000);
326
291
  },
327
292
 
@@ -75,6 +75,10 @@ let system = function () {
75
75
  }
76
76
  }));
77
77
  },
78
+ trackPlayback: ({objectId, eventType, currentTime, duration, quality, pageUrl, data} = {}) => {
79
+ return this.__privates.auth.__client
80
+ .then(client => client.apis.wt.trackPlayback({}, {requestBody: {objectId, eventType, currentTime, duration, quality, pageUrl, data}}));
81
+ },
78
82
  getSchemas: () => {
79
83
  return this.__privates.auth.__client.then(client => {
80
84
  return client.spec?.components?.schemas