@reactoo/watchtogether-sdk-js 2.7.34 → 2.7.36

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.34",
3
+ "version": "2.7.36",
4
4
  "description": "Javascript SDK for Reactoo",
5
5
  "main": "src/index.js",
6
6
  "unpkg": "dist/watchtogether-sdk.min.js",
@@ -47,11 +47,11 @@ let asset = function() {
47
47
  .then(client => client.apis.asset.initiateAssetUpload())
48
48
  },
49
49
 
50
- uploadAsset: (file, roomIds, id = null, initiationData = null) => {
50
+ uploadAsset: (file, roomIds, id = null, initiationData = null, assetData = null) => {
51
51
  return this.__privates.auth.__client
52
52
  .then(client => Promise.all([client, initiationData ? Promise.resolve(initiationData) : client.apis.asset.initiateAssetUpload({id: id || generateUUID()})]))
53
53
  .then(([client, response]) => Promise.all([client, client.http({url: response.data.signedUrl, method: response.data.httpMethod, headers: {"Content-Type":file.type}, body:file}), response.data.id]))
54
- .then(([client, response, idn]) => Promise.all([client.apis.asset.publishAsset({id:idn}, {requestBody: {title: file.name, ...(roomIds ? {roomIds} : {})}}), idn]))
54
+ .then(([client, response, idn]) => Promise.all([client.apis.asset.publishAsset({id:idn}, {requestBody: {title: file.name, ...(roomIds ? {roomIds} : {}), ...(assetData ? assetData : {})}}), idn]))
55
55
  ;
56
56
  },
57
57
  downloadAsset: (url, options = {}) => {
package/src/models/iot.js CHANGED
@@ -3,6 +3,8 @@
3
3
  let iot = function () {
4
4
  let __currentTopics = new Set();
5
5
  let visibilityChangeHandler = null;
6
+ let keepAliveIntervalId = null;
7
+ let shouldBeConnected = false;
6
8
 
7
9
  return {
8
10
 
@@ -31,11 +33,12 @@ let iot = function () {
31
33
 
32
34
  onConnect: () => {
33
35
  this.__privates.iot.log('MQTT client connected');
34
- this.iot.setupVisibilityChangeListener();
35
36
  },
36
37
  onClosed: () => {
37
38
  this.__privates.iot.log('MQTT client closed');
38
- this.iot.disableVisibilityChangeListener();
39
+ if(shouldBeConnected) {
40
+ this.iot.__updateCredentials();
41
+ }
39
42
  },
40
43
  onError: () => {
41
44
  this.__privates.iot.log('MQTT client error');
@@ -80,6 +83,10 @@ let iot = function () {
80
83
  let __currentTopicsCopy = new Set(__currentTopics); // Keep this line
81
84
  __currentTopics.clear();
82
85
 
86
+ shouldBeConnected = true;
87
+
88
+ this.iot.setupVisibilityChangeListener();
89
+ //this.iot.setupKeepAliveInterval();
83
90
  this.iot.$on('connect', this.iot.onConnect, this);
84
91
  this.iot.$on('closed', this.iot.onClosed, this);
85
92
  this.iot.$on('error', this.iot.onError, this);
@@ -116,6 +123,10 @@ let iot = function () {
116
123
  return this.__privates.iot.disconnect()
117
124
  .then(() => {
118
125
 
126
+ shouldBeConnected = false;
127
+
128
+ this.iot.disableVisibilityChangeListener();
129
+ //this.iot.disableKeepAliveInterval();
119
130
  this.iot.$off('connect', this.iot.onConnect, this);
120
131
  this.iot.$off('closed', this.iot.onClosed, this);
121
132
  this.iot.$off('error', this.iot.onError, this);
@@ -177,7 +188,17 @@ let iot = function () {
177
188
  return this.iot.__updateCredentials();
178
189
  });
179
190
  },
180
-
191
+
192
+ setupKeepAliveInterval: () => {
193
+ clearInterval(keepAliveIntervalId);
194
+ keepAliveIntervalId = setInterval(this.iot.checkConnection, 30000);
195
+ },
196
+
197
+ disableKeepAliveInterval: () => {
198
+ clearInterval(keepAliveIntervalId);
199
+ keepAliveIntervalId = null;
200
+ },
201
+
181
202
  setupVisibilityChangeListener: () => {
182
203
  if (visibilityChangeHandler) {
183
204
  // Listener is already set up
@@ -6,7 +6,6 @@ class Iot {
6
6
  constructor(enableDebugFlag) {
7
7
  Object.assign(this, emitter());
8
8
  this.decoder = new TextDecoder('utf-8');
9
- this.connectionActive = false;
10
9
  this.log = Iot.noop;
11
10
  // Remove: this.debugFlag = enableDebugFlag;
12
11
  this.credentialsExpirationCheckIntervalId = null;
@@ -50,32 +49,26 @@ class Iot {
50
49
  this.connection = client.new_connection(config);
51
50
 
52
51
  this.connection.on('connect', () => {
53
- this.connectionActive = true;
54
52
  this.emit('connect');
55
53
  });
56
54
 
57
55
  this.connection.on('closed', (error) => {
58
- this.connectionActive = false;
59
56
  this.emit('closed', error);
60
57
  });
61
58
 
62
59
  this.connection.on('disconnect', () => {
63
- this.connectionActive = false;
64
60
  this.emit('disconnect');
65
61
  });
66
62
 
67
63
  this.connection.on('error', (error) => {
68
- this.connectionActive = false;
69
64
  this.emit('error', error);
70
65
  });
71
66
 
72
67
  this.connection.on('interrupt', (error) => {
73
- this.connectionActive = false;
74
68
  this.emit('interrupt', error);
75
69
  });
76
70
 
77
71
  this.connection.on('resume', (error) => {
78
- this.connectionActive = true;
79
72
  this.emit('resume', error);
80
73
  });
81
74
 
@@ -84,12 +77,10 @@ class Iot {
84
77
  });
85
78
 
86
79
  this.connection.on('connection_success', (error) => {
87
- this.connectionActive = true;
88
80
  this.emit('connection_success', error);
89
81
  });
90
82
 
91
83
  this.connection.on('connection_failure', (error) => {
92
- this.connectionActive = false;
93
84
  this.emit('connection_failure', error);
94
85
  });
95
86
 
@@ -106,13 +97,12 @@ class Iot {
106
97
  this.log('iot disconnect');
107
98
  this.stopCredentialsExpirationCheck();
108
99
  if (this.connection) {
109
- this.connectionActive = false;
110
100
  return this.connection.disconnect();
111
101
  } else return Promise.resolve();
112
102
  }
113
103
 
114
104
  isConnected() {
115
- return this.connectionActive;
105
+ return this.connection && this.connection.currentState === 0 && this.connection.desiredState === 0;
116
106
  }
117
107
 
118
108
  subscribe(topic) {
@@ -208,7 +198,8 @@ class Iot {
208
198
  event === 'handRaised' ||
209
199
  event === 'handLowered' ||
210
200
  event === 'handsCleared' ||
211
- event === 'volume_set'
201
+ event === 'volume_set' ||
202
+ event === 'asset_created'
212
203
  ) {
213
204
  this.emit('message', {event, ...message, roomId})
214
205
  }
@@ -245,7 +236,8 @@ class Iot {
245
236
  this.currentCredentialsExpirationStamp = new Date(expiration).getTime();
246
237
  this.credentialsExpirationCheckIntervalId = setInterval(() => {
247
238
  const currentTimeStamp = new Date().getTime();
248
- if(this.currentCredentialsExpirationStamp - currentTimeStamp <= 300000) {
239
+ // update 15 minutes before expiration
240
+ if(this.currentCredentialsExpirationStamp - currentTimeStamp <= 900000) {
249
241
  this.emit('updateCredentials');
250
242
  }
251
243
  }, 5000);
@@ -279,7 +271,7 @@ class Iot {
279
271
 
280
272
  checkConnection() {
281
273
  return new Promise((resolve, reject) => {
282
- if (!this.connection || !this.connectionActive) {
274
+ if (!this.connection || !(this.connection.currentState === 0 && this.connection.desiredState === 0)) {
283
275
  reject(new Error('Not connected'));
284
276
  return;
285
277
  }