@reactoo/watchtogether-sdk-js 2.7.31 → 2.7.33

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.31",
3
+ "version": "2.7.33",
4
4
  "description": "Javascript SDK for Reactoo",
5
5
  "main": "src/index.js",
6
6
  "unpkg": "dist/watchtogether-sdk.min.js",
package/src/models/iot.js CHANGED
@@ -28,6 +28,33 @@ let iot = function () {
28
28
  .then(client => client.apis.auth.iotSignIn({}, {requestBody: {suggestedTopics:true, domain: location.hostname}}))
29
29
 
30
30
  },
31
+
32
+ onConnect: () => {
33
+ this.__privates.iot.log('MQTT client connected');
34
+ this.iot.setupVisibilityChangeListener();
35
+ },
36
+ onClosed: () => {
37
+ this.__privates.iot.log('MQTT client closed');
38
+ this.iot.disableVisibilityChangeListener();
39
+ },
40
+ onError: () => {
41
+ this.__privates.iot.log('MQTT client error');
42
+ },
43
+ onDisconnect: () => {
44
+ this.__privates.iot.log('MQTT client disconnect');
45
+ },
46
+ onInterrupt: () => {
47
+ this.__privates.iot.log('MQTT client interrupt');
48
+ },
49
+ onResume: () => {
50
+ this.__privates.iot.log('MQTT client resume');
51
+ },
52
+ onConnectionSuccess: () => {
53
+ this.__privates.iot.log('MQTT client connection_success');
54
+ },
55
+ onConnectionFailure: () => {
56
+ this.__privates.iot.log('MQTT client connection_failure');
57
+ },
31
58
 
32
59
  iotLogin: (subscribeToSuggestedTopics = true) => {
33
60
  this.iot.__promise = new Promise((resolve, reject) => {
@@ -53,39 +80,14 @@ let iot = function () {
53
80
  let __currentTopicsCopy = new Set(__currentTopics); // Keep this line
54
81
  __currentTopics.clear();
55
82
 
56
- this.iot.$on('connect', () => {
57
- this.__privates.iot.log('MQTT client connected');
58
- this.iot.setupVisibilityChangeListener();
59
- });
60
-
61
- this.iot.$on('closed', () => {
62
- this.__privates.iot.log('MQTT client closed');
63
- this.iot.disableVisibilityChangeListener();
64
- });
65
-
66
- this.iot.$on('error', () => {
67
- this.__privates.iot.log('MQTT client error');
68
- });
69
-
70
- this.iot.$on('disconnect', () => {
71
- this.__privates.iot.log('MQTT client disconnect');
72
- });
73
-
74
- this.iot.$on('interrupt', () => {
75
- this.__privates.iot.log('MQTT client interrupt');
76
- });
77
-
78
- this.iot.$on('resume', () => {
79
- this.__privates.iot.log('MQTT client resume');
80
- });
81
-
82
- this.iot.$on('connection_success', () => {
83
- this.__privates.iot.log('MQTT client connection_success');
84
- });
85
-
86
- this.iot.$on('connection_failure', () => {
87
- this.__privates.iot.log('MQTT client connection_failure');
88
- });
83
+ this.iot.$on('connect', this.iot.onConnect, this);
84
+ this.iot.$on('closed', this.iot.onClosed, this);
85
+ this.iot.$on('error', this.iot.onError, this);
86
+ this.iot.$on('disconnect', this.iot.onDisconnect, this);
87
+ this.iot.$on('interrupt', this.iot.onInterrupt, this);
88
+ this.iot.$on('resume', this.iot.onResume, this);
89
+ this.iot.$on('connection_success', this.iot.onConnectionSuccess, this);
90
+ this.iot.$on('connection_failure', this.iot.onConnectionFailure, this);
89
91
 
90
92
  this.iot.__promise
91
93
  .then(([suggestedTopic, instance]) => {
@@ -113,8 +115,17 @@ let iot = function () {
113
115
  }
114
116
  return this.__privates.iot.disconnect()
115
117
  .then(() => {
118
+
119
+ this.iot.$off('connect', this.iot.onConnect, this);
120
+ this.iot.$off('closed', this.iot.onClosed, this);
121
+ this.iot.$off('error', this.iot.onError, this);
122
+ this.iot.$off('disconnect', this.iot.onDisconnect, this);
123
+ this.iot.$off('interrupt', this.iot.onInterrupt, this);
124
+ this.iot.$off('resume', this.iot.onResume, this);
125
+ this.iot.$off('connection_success', this.iot.onConnectionSuccess, this);
126
+ this.iot.$off('connection_failure', this.iot.onConnectionFailure, this);
127
+
116
128
  this.iot.__promise = null;
117
- this.iot.$clear();
118
129
  return true;
119
130
  });
120
131
  },
@@ -26,6 +26,46 @@ class Iot {
26
26
  this.log = console.log.bind(console);
27
27
  }
28
28
 
29
+ onConnect() {
30
+ this.connectionActive = true;
31
+ this.emit('connect');
32
+ }
33
+ onClosed(error) {
34
+ this.connectionActive = false;
35
+ this.emit('closed', error);
36
+ }
37
+
38
+ onDisconnect() {
39
+ this.connectionActive = false;
40
+ this.emit('disconnect');
41
+ }
42
+ onError(error) {
43
+ this.connectionActive = false;
44
+ this.emit('error', error);
45
+ }
46
+
47
+ onInterrupt(error) {
48
+ this.connectionActive = false;
49
+ this.emit('interrupt', error);
50
+ }
51
+ onResume(r) {
52
+ this.connectionActive = true;
53
+ this.emit('resume', r);
54
+ }
55
+
56
+ onMessage(topic, payload) {
57
+ this.handleMessage(topic, new Uint8Array(payload));
58
+ }
59
+ onConnectionSuccess(r) {
60
+ this.connectionActive = true;
61
+ this.emit('connection_success', r);
62
+ }
63
+ onConnectionFailure(error) {
64
+ this.connectionActive = false;
65
+ this.emit('connection_failure', error);
66
+ }
67
+
68
+
29
69
  connect(apiMqttUrl, apiMqttClientId, region, accessKeyId, secretAccessKey, sessionToken, expiration) {
30
70
  this.log('iot connect called, we disconnect first just to be sure');
31
71
  return this.disconnect()
@@ -48,50 +88,15 @@ class Iot {
48
88
 
49
89
  const client = new mqtt.MqttClient();
50
90
  this.connection = client.new_connection(config);
51
-
52
- this.connection.on('connect', () => {
53
- this.connectionActive = true;
54
- this.emit('connect');
55
- });
56
-
57
- this.connection.on('disconnect', () => {
58
- this.connectionActive = false;
59
- this.emit('disconnect');
60
- });
61
-
62
- this.connection.on('error', (error) => {
63
- this.connectionActive = false;
64
- this.emit('error', error);
65
- });
66
-
67
- this.connection.on('interrupt', (error) => {
68
- this.connectionActive = false;
69
- this.emit('interrupt', error);
70
- });
71
-
72
- this.connection.on('resume', (error) => {
73
- this.connectionActive = true;
74
- this.emit('resume', error);
75
- });
76
-
77
- this.connection.on('message', (topic, payload) => {
78
- this.handleMessage(topic, new Uint8Array(payload));
79
- });
80
-
81
- this.connection.on('connection_success', (error) => {
82
- this.connectionActive = true;
83
- this.emit('connection_success', error);
84
- });
85
-
86
- this.connection.on('connection_failure', (error) => {
87
- this.connectionActive = false;
88
- this.emit('connection_failure', error);
89
- });
90
-
91
- this.connection.on('closed', (error) => {
92
- this.connectionActive = false;
93
- this.emit('closed', error);
94
- });
91
+ this.connection.on('connect', this.onConnect, this);
92
+ this.connection.on('closed', this.onClosed, this);
93
+ this.connection.on('disconnect', this.onDisconnect, this);
94
+ this.connection.on('error', this.onError, this);
95
+ this.connection.on('interrupt', this.onInterrupt, this);
96
+ this.connection.on('resume', this.onResume, this);
97
+ this.connection.on('message', this.onMessage, this);
98
+ this.connection.on('connection_success', this.onConnectionSuccess, this);
99
+ this.connection.on('connection_failure', this.onConnectionFailure, this);
95
100
 
96
101
  return this.connection.connect()
97
102
  .then(() => {
@@ -142,8 +147,10 @@ class Iot {
142
147
  send(topic, message) {
143
148
  this.log('iot send', topic, message);
144
149
  let msg = typeof message === 'object' ? JSON.stringify(message) : message;
145
- if (this.connection) {
150
+ if (this.connection && this.connection.currentState === 0 && this.connection.desiredState === 0) {
146
151
  this.connection.publish(topic, msg, mqtt.QoS.AtLeastOnce, false);
152
+ } else {
153
+ throw new Error('Invalid topic or not connected:', topic);
147
154
  }
148
155
  }
149
156