@transitive-sdk/utils-web 0.12.1 → 0.12.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/dist/utils-web.js +25 -12
  2. package/package.json +1 -1
package/dist/utils-web.js CHANGED
@@ -1086,16 +1086,24 @@ var require_MqttSync = __commonJS({
1086
1086
  * Remote Procedure Calls (RPC)
1087
1087
  */
1088
1088
  /* Handle RPC requests */
1089
- handleRPCRequest(topic, json) {
1089
+ async handleRPCRequest(topic, json) {
1090
1090
  log2.debug("handling RPC request for", topic, json);
1091
1091
  const handler = this.rpcHandlers[topic];
1092
1092
  const result = handler(json.args);
1093
1093
  const responseTopic = `${topic.replace("/request", "/response")}/${json.id}`;
1094
- this.mqtt.publish(
1095
- responseTopic,
1096
- JSON.stringify({ id: json.id, result }),
1097
- { retain: false, qos: 2 }
1098
- );
1094
+ if (result instanceof Promise) {
1095
+ result.then((resultValue) => this.mqtt.publish(
1096
+ responseTopic,
1097
+ JSON.stringify({ id: json.id, result: resultValue }),
1098
+ { retain: false, qos: 2 }
1099
+ ));
1100
+ } else {
1101
+ this.mqtt.publish(
1102
+ responseTopic,
1103
+ JSON.stringify({ id: json.id, result }),
1104
+ { retain: false, qos: 2 }
1105
+ );
1106
+ }
1099
1107
  }
1100
1108
  /* Handle RPC response */
1101
1109
  handleRPCResponse(topic, json) {
@@ -1106,8 +1114,8 @@ var require_MqttSync = __commonJS({
1106
1114
  }
1107
1115
  /** Register an RPC request handler. Example:
1108
1116
  * ```js
1109
- * mqttSync.register('/mycommand', arg => {
1110
- * log.debug('running /mycommand with args', arg);
1117
+ * mqttSync.register('/mySquare', arg => {
1118
+ * log.debug('running /mySquare with args', arg);
1111
1119
  * return arg * arg;
1112
1120
  * });
1113
1121
  * ```
@@ -1117,6 +1125,11 @@ var require_MqttSync = __commonJS({
1117
1125
  * topics in their respective namespace. In the cloud and on the web you will
1118
1126
  * need to use the respective namespace, i.e.,
1119
1127
  * `/orgId/deviceId/@scope/capName/capVersion/`.
1128
+ *
1129
+ * #### Async/Await
1130
+ * Yes, you can make the handler `async` and use `await` inside of it. This
1131
+ * will be handled correctly, i.e., MqttSync will await the result of the
1132
+ * handler before responding to the RPC request client.
1120
1133
  */
1121
1134
  register(command, handler) {
1122
1135
  log2.debug("registering RPC handler for", command);
@@ -1132,14 +1145,14 @@ var require_MqttSync = __commonJS({
1132
1145
  }
1133
1146
  /** Make an RPC request. Example:
1134
1147
  * ```js
1135
- * mqttSync.call('/mycommand', 11, result => {
1136
- * log.debug(`Called /mycommand with arg 11 and got ${result}`);
1148
+ * mqttSync.call('/mySquare', 11, result => {
1149
+ * log.debug(`Called /mySquare with arg 11 and got ${result}`);
1137
1150
  * });
1138
1151
  * ```
1139
1152
  * Alternative you can omit the callback and use async/await:
1140
1153
  * ```js
1141
- * const result = await mqttSync.call('/mycommand', 11);
1142
- * log.debug(`Called /mycommand with arg 11 and got ${result}`);
1154
+ * const result = await mqttSync.call('/mySquare', 11);
1155
+ * log.debug(`Called /mySquare with arg 11 and got ${result}`);
1143
1156
  * ```
1144
1157
  * See the note about namespaces in `register`.
1145
1158
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@transitive-sdk/utils-web",
3
- "version": "0.12.1",
3
+ "version": "0.12.2",
4
4
  "description": "Web utils for the Transitive framework",
5
5
  "homepage": "https://transitiverobotics.com",
6
6
  "repository": {