@transitive-sdk/utils-web 0.12.0 → 0.12.1

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 +28 -36
  2. package/package.json +1 -1
package/dist/utils-web.js CHANGED
@@ -655,7 +655,6 @@ var require_MqttSync = __commonJS({
655
655
  var log2 = getLogger2("MqttSync");
656
656
  var HEARTBEAT_TOPIC = "$SYS/broker/uptime";
657
657
  var specialKey = "$_";
658
- var RPC_PREFIX = "_RPCv1_";
659
658
  var noop = () => {
660
659
  };
661
660
  var clone2 = (payload) => {
@@ -733,8 +732,10 @@ var require_MqttSync = __commonJS({
733
732
  topic = pathToTopic2(path);
734
733
  }
735
734
  const json = mqttParsePayload(payload);
736
- if (path[0] == RPC_PREFIX) {
737
- this.handleRPC(path.slice(1), json);
735
+ if (this.rpcHandlers[topic]) {
736
+ this.handleRPCRequest(topic, json);
737
+ } else if (this.rpcCallbacks[topic]) {
738
+ this.handleRPCResponse(topic, json);
738
739
  } else if (this.isPublished(topic)) {
739
740
  this.publishedMessages.updateFromArray([...path, specialKey], json);
740
741
  this.data.update(topic, json, { external: true });
@@ -1084,33 +1085,24 @@ var require_MqttSync = __commonJS({
1084
1085
  /* --------------------------------------------------------------------------
1085
1086
  * Remote Procedure Calls (RPC)
1086
1087
  */
1087
- /* Handle RPC requests anmd responses */
1088
- handleRPC(path, json) {
1089
- const type = path[0];
1090
- if (type == "request") {
1091
- const command = pathToTopic2(path.slice(1));
1092
- log2.debug("handling RPC request for", command, json);
1093
- const handler = this.rpcHandlers[command];
1094
- if (!handler) {
1095
- log2.warn("Received unknown RPC request", command);
1096
- return;
1097
- }
1098
- const result = handler(json.args);
1099
- const responseTopic = `/${RPC_PREFIX}/response/${json.id}`;
1100
- this.mqtt.publish(
1101
- responseTopic,
1102
- JSON.stringify({ id: json.id, command, result }),
1103
- { retain: false, qos: 2 }
1104
- );
1105
- } else if (type == "response") {
1106
- const requestId = path[1];
1107
- log2.debug("handle RPC response", requestId, json);
1108
- this.rpcCallbacks[requestId]?.(json.result);
1109
- delete this.rpcCallbacks[requestId];
1110
- this.mqtt.unsubscribe(`/${RPC_PREFIX}/response/${requestId}`);
1111
- } else {
1112
- log2.warn("Unknown RPC type", path);
1113
- }
1088
+ /* Handle RPC requests */
1089
+ handleRPCRequest(topic, json) {
1090
+ log2.debug("handling RPC request for", topic, json);
1091
+ const handler = this.rpcHandlers[topic];
1092
+ const result = handler(json.args);
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
+ );
1099
+ }
1100
+ /* Handle RPC response */
1101
+ handleRPCResponse(topic, json) {
1102
+ log2.debug("handle RPC response", topic, json);
1103
+ this.rpcCallbacks[topic](json.result);
1104
+ delete this.rpcCallbacks[topic];
1105
+ this.mqtt.unsubscribe(topic);
1114
1106
  }
1115
1107
  /** Register an RPC request handler. Example:
1116
1108
  * ```js
@@ -1128,8 +1120,8 @@ var require_MqttSync = __commonJS({
1128
1120
  */
1129
1121
  register(command, handler) {
1130
1122
  log2.debug("registering RPC handler for", command);
1131
- this.rpcHandlers[command] = handler;
1132
- const requestTopic = `/${RPC_PREFIX}/request${command}`;
1123
+ const requestTopic = `${command}/request`;
1124
+ this.rpcHandlers[requestTopic] = handler;
1133
1125
  this.mqtt.subscribe(requestTopic, { rap: true, qos: 2 }, (err, granted) => {
1134
1126
  if (err) {
1135
1127
  log2.warn(`Error subscribing to RPC topic ${requestTopic}`, err);
@@ -1153,7 +1145,7 @@ var require_MqttSync = __commonJS({
1153
1145
  */
1154
1146
  call(command, args, callback = void 0) {
1155
1147
  const id = crypto.randomUUID();
1156
- const responseTopic = `/${RPC_PREFIX}/response/${id}`;
1148
+ const responseTopic = `${command}/response/${id}`;
1157
1149
  this.mqtt.subscribe(responseTopic, { rap: true, qos: 2 }, (err, granted) => {
1158
1150
  if (err) {
1159
1151
  log2.warn(`Error subscribing to RPC response topic ${responseTopic}`, err);
@@ -1161,7 +1153,7 @@ var require_MqttSync = __commonJS({
1161
1153
  log2.warn(`Not allowed to subscribe to RPC response topic ${responseTopic}`);
1162
1154
  }
1163
1155
  });
1164
- const requestTopic = `/${RPC_PREFIX}/request${command}`;
1156
+ const requestTopic = `${command}/request`;
1165
1157
  log2.debug("calling RPC", requestTopic);
1166
1158
  this.mqtt.publish(
1167
1159
  requestTopic,
@@ -1169,10 +1161,10 @@ var require_MqttSync = __commonJS({
1169
1161
  { retain: false, qos: 2 }
1170
1162
  );
1171
1163
  if (callback) {
1172
- this.rpcCallbacks[id] = callback;
1164
+ this.rpcCallbacks[responseTopic] = callback;
1173
1165
  } else {
1174
1166
  return new Promise((resolve, reject) => {
1175
- this.rpcCallbacks[id] = resolve;
1167
+ this.rpcCallbacks[responseTopic] = resolve;
1176
1168
  });
1177
1169
  }
1178
1170
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@transitive-sdk/utils-web",
3
- "version": "0.12.0",
3
+ "version": "0.12.1",
4
4
  "description": "Web utils for the Transitive framework",
5
5
  "homepage": "https://transitiverobotics.com",
6
6
  "repository": {