@faybl/client 4.1.1 → 4.1.2

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/mqttUtils.js +43 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faybl/client",
3
- "version": "4.1.1",
3
+ "version": "4.1.2",
4
4
  "description": "Faybl JavaScript Client",
5
5
  "license": "MIT",
6
6
  "author": "Arcturitech",
package/src/mqttUtils.js CHANGED
@@ -69,14 +69,32 @@ const messageHandler = (getState, report) => async (topic, jsonData) => {
69
69
  return undefined;
70
70
  };
71
71
 
72
+ // A SUBACK return code of 0x80 or above refuses the subscription.
73
+ const SUBACK_FAILURE = 0x80;
74
+
75
+ // The broker's answer for one topic: undefined when MQTT.js sent nothing for it, which it does for
76
+ // a topic this connection already holds at that QoS or higher.
77
+ const grantedQos = (granted, topic) => granted?.find((grant) => grant.topic === topic)?.qos;
78
+
79
+ const subscriptionRefusal = (topic, qos, granted) =>
80
+ granted !== undefined && (granted >= SUBACK_FAILURE || granted < qos)
81
+ ? Error(`MQTT subscription to ${topic} was not granted QoS ${qos}`)
82
+ : undefined;
83
+
72
84
  export const createMqttHandler = (client, { report = () => undefined } = {}) => {
73
85
  const { getState, dispatch } = createStore({}, reducer);
86
+ // The acknowledgement each topic was subscribed with, so a second subscription to a topic waits
87
+ // for the same SUBACK rather than settling before the broker has answered.
88
+ // eslint-disable-next-line functional/no-let -- Acknowledgements outlive the independent subscribe and unsubscribe calls that share them; each change replaces the object.
89
+ let acknowledgements = {};
74
90
 
75
91
  const handleMessage = messageHandler(getState, report);
76
92
  client.on('message', handleMessage);
77
93
  const unsubscribe = (topic, callback) => {
78
94
  dispatch({ type: 'remove', topic, callback });
79
- if (!getState(topic)) client.unsubscribe(topic);
95
+ if (getState(topic)) return;
96
+ acknowledgements = R.dissoc(topic, acknowledgements);
97
+ client.unsubscribe(topic);
80
98
  };
81
99
 
82
100
  const subscribe = (topic, callback, { preCallback, qos } = {}) => {
@@ -85,18 +103,34 @@ export const createMqttHandler = (client, { report = () => undefined } = {}) =>
85
103
  return () => unsubscribe(topic, callback);
86
104
  };
87
105
 
88
- const subscribeWithAck = (topic, callback, { qos = 0, preCallback } = {}) => {
89
- dispatch({ type: 'add', topic, callback, preCallback });
90
- const ready = new Promise((resolve, reject) => {
106
+ const requestAcknowledgement = (topic, qos) =>
107
+ new Promise((resolve, reject) => {
91
108
  client.subscribe(topic, { qos }, (error, granted) => {
92
109
  if (error) return reject(error);
93
- if (granted?.[0]?.qos !== qos) {
94
- return reject(Error(`MQTT subscription to ${topic} was not granted QoS ${qos}`));
95
- }
96
- return resolve();
110
+ const refusal = subscriptionRefusal(topic, qos, grantedQos(granted, topic));
111
+ return refusal ? reject(refusal) : resolve();
97
112
  });
98
113
  });
99
- return { ready, unsubscribe: () => unsubscribe(topic, callback) };
114
+
115
+ const acknowledgementFor = (topic, qos) => {
116
+ const held = acknowledgements[topic];
117
+ if (held && held.qos >= qos) return held.ready;
118
+ const ready = requestAcknowledgement(topic, qos);
119
+ acknowledgements = R.assoc(topic, { qos, ready }, acknowledgements);
120
+ // A refused subscription is asked for again by the next caller.
121
+ ready.catch(() => {
122
+ if (acknowledgements[topic]?.ready === ready)
123
+ acknowledgements = R.dissoc(topic, acknowledgements);
124
+ });
125
+ return ready;
126
+ };
127
+
128
+ const subscribeWithAck = (topic, callback, { qos = 0, preCallback } = {}) => {
129
+ dispatch({ type: 'add', topic, callback, preCallback });
130
+ return {
131
+ ready: acknowledgementFor(topic, qos),
132
+ unsubscribe: () => unsubscribe(topic, callback),
133
+ };
100
134
  };
101
135
 
102
136
  const publish = (topic, message, options) => client.publish(topic, toPayload(message), options);