@faybl/client 4.1.2 → 4.1.3

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/README.md CHANGED
@@ -98,7 +98,9 @@ Changes are published on the bus as they happen:
98
98
  gets `{ entity, operation, rid, record }`, where `record` is the whole record as read after the
99
99
  change, or `{}` for a delete.
100
100
  - `conf.subscribeWithAck(...)` takes the same and returns `{ ready, unsubscribe }`; `ready` resolves
101
- once the broker has acknowledged the subscription, and rejects if it refused.
101
+ once the broker has acknowledged the subscription, and rejects only if the broker refused it. A
102
+ connection that closes first is waited out: the subscription is asked for again on the
103
+ reconnect.
102
104
  - `conf.unsubscribe({ entity, operation, rid, callback })`. Without a callback it removes every
103
105
  callback on that subscription.
104
106
 
@@ -174,7 +176,9 @@ subscription may be a filter with wildcards.
174
176
  - `mqttClient.subscribe(topic, callback, { qos })` returns the function that undoes it. The
175
177
  callback gets `{ topic, data }`, with `data` parsed from JSON.
176
178
  - `mqttClient.subscribeWithAck(topic, callback, { qos })` returns `{ ready, unsubscribe }`; `ready`
177
- resolves once the broker has granted the QoS asked for, 0 by default.
179
+ resolves once the broker has granted the QoS asked for, 0 by default, and rejects only if it
180
+ refused, as above. Several subscriptions to one topic share one acknowledgement, and the topic
181
+ keeps the strictest QoS asked for.
178
182
  - `mqttClient.unsubscribe(topic, callback)`. Without a callback it removes every callback on the
179
183
  topic.
180
184
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faybl/client",
3
- "version": "4.1.2",
3
+ "version": "4.1.3",
4
4
  "description": "Faybl JavaScript Client",
5
5
  "license": "MIT",
6
6
  "author": "Arcturitech",
package/src/mqttUtils.js CHANGED
@@ -103,13 +103,20 @@ export const createMqttHandler = (client, { report = () => undefined } = {}) =>
103
103
  return () => unsubscribe(topic, callback);
104
104
  };
105
105
 
106
+ // Only the broker can refuse a subscription. An error instead of a SUBACK means the connection
107
+ // closed first, so the request is made again once it reconnects, rather than failing. A client
108
+ // that is being closed rejects. MQTT.js counts the topic as held from the first attempt and would
109
+ // answer a plain retry at once with no grants; `resubscribe`, the flag it uses for its own
110
+ // resubscriptions, makes it send the SUBSCRIBE so the retry waits for the broker's answer.
106
111
  const requestAcknowledgement = (topic, qos) =>
107
112
  new Promise((resolve, reject) => {
108
- client.subscribe(topic, { qos }, (error, granted) => {
109
- if (error) return reject(error);
113
+ const answer = (error, granted) => {
114
+ if (error) return client.disconnecting ? reject(error) : client.once('connect', retry);
110
115
  const refusal = subscriptionRefusal(topic, qos, grantedQos(granted, topic));
111
116
  return refusal ? reject(refusal) : resolve();
112
- });
117
+ };
118
+ const retry = () => client.subscribe({ [topic]: { qos }, resubscribe: true }, answer);
119
+ client.subscribe(topic, { qos }, answer);
113
120
  });
114
121
 
115
122
  const acknowledgementFor = (topic, qos) => {