Skip to content

Commit a5337a9

Browse files
committed
Replace id counter with UUIDs
1 parent e996525 commit a5337a9

File tree

8 files changed

+47
-31
lines changed

8 files changed

+47
-31
lines changed

package-lock.json

Lines changed: 36 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"cbor-js": "^0.1.0",
4646
"eventemitter3": "^5.0.1",
4747
"pngparse": "^2.0.0",
48+
"uuid": "^13.0.0",
4849
"ws": "^8.0.0"
4950
},
5051
"directories": {

src/actionlib/Goal.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import { EventEmitter } from "eventemitter3";
77
import ActionClient from "./ActionClient";
88
import { actionlib_msgs } from "../types/actionlib_msgs";
9+
import { v4 as uuidv4 } from "uuid";
910

1011
/**
1112
* An actionlib goal that is associated with an action server.
@@ -28,7 +29,7 @@ export default class Goal<
2829
result?: TResult = undefined;
2930
feedback?: TFeedback = undefined;
3031
// Create a random ID
31-
goalID = `goal_${Math.random().toString()}_${new Date().getTime().toString()}`;
32+
goalID = `goal_${uuidv4()}`;
3233
actionClient: ActionClient<TGoal, TFeedback, TResult>;
3334
goalMessage: { goal: TGoal; goal_id: actionlib_msgs.GoalID };
3435
/**

src/core/Action.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
RosbridgeSendActionGoalMessage,
1313
} from "../types/protocol.ts";
1414
import Ros from "./Ros.js";
15+
import { v4 as uuidv4 } from "uuid";
1516

1617
/**
1718
* A ROS 2 action client.
@@ -67,7 +68,7 @@ export default class Action<
6768
return;
6869
}
6970

70-
const actionGoalId = `send_action_goal:${this.name}:${(++this.ros.idCounter).toString()}`;
71+
const actionGoalId = `send_action_goal:${this.name}:${uuidv4()}`;
7172

7273
this.ros.on(actionGoalId, function (message) {
7374
if (isRosbridgeActionResultMessage<TResult>(message)) {

src/core/Ros.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ export default class Ros extends EventEmitter<
4949
> {
5050
/** @type {import('./SocketAdapter.js').default | null} */
5151
socket: import("./SocketAdapter.js").default | null = null;
52-
idCounter = 0;
5352
isConnected = false;
5453
transportLibrary: "websocket" | RTCPeerConnection;
5554
transportOptions: {

src/core/Service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
RosbridgeServiceResponseMessage,
1111
} from "../types/protocol.ts";
1212
import Ros from "./Ros.js";
13+
import { v4 as uuidv4 } from "uuid";
1314

1415
/**
1516
* A ROS service client.
@@ -73,8 +74,7 @@ export default class Service<TRequest, TResponse> extends EventEmitter {
7374
return;
7475
}
7576

76-
const serviceCallId =
77-
"call_service:" + this.name + ":" + (++this.ros.idCounter).toString();
77+
const serviceCallId = "call_service:" + this.name + ":" + uuidv4();
7878

7979
this.ros.once(serviceCallId, function (message) {
8080
if (isRosbridgeServiceResponseMessage<TResponse>(message)) {

src/core/Topic.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
RosbridgeSubscribeMessage,
1313
} from "../types/protocol.ts";
1414
import { rosapi } from "../types/rosapi.ts";
15+
import { v4 as uuidv4 } from "uuid";
1516

1617
/**
1718
* Publish and/or subscribe to a topic in ROS.
@@ -152,8 +153,7 @@ export default class Topic<T> extends EventEmitter<{
152153
return;
153154
}
154155
this.ros.on(this.name, this.#messageCallback);
155-
this.subscribeId =
156-
"subscribe:" + this.name + ":" + (++this.ros.idCounter).toString();
156+
this.subscribeId = "subscribe:" + this.name + ":" + uuidv4();
157157

158158
this.callForSubscribeAndAdvertise({
159159
op: "subscribe",
@@ -205,8 +205,7 @@ export default class Topic<T> extends EventEmitter<{
205205
if (this.isAdvertised) {
206206
return;
207207
}
208-
this.advertiseId =
209-
"advertise:" + this.name + ":" + (++this.ros.idCounter).toString();
208+
this.advertiseId = "advertise:" + this.name + ":" + uuidv4();
210209
this.callForSubscribeAndAdvertise({
211210
op: "advertise",
212211
id: this.advertiseId,
@@ -251,10 +250,9 @@ export default class Topic<T> extends EventEmitter<{
251250
this.advertise();
252251
}
253252

254-
this.ros.idCounter++;
255253
const call = {
256254
op: "publish",
257-
id: "publish:" + this.name + ":" + this.ros.idCounter.toString(),
255+
id: "publish:" + this.name + ":" + uuidv4(),
258256
topic: this.name,
259257
msg: message,
260258
latch: this.latch,

test/tfclient.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ describe("TFClient", function () {
1111
*/
1212

1313
const dummyROS = {
14-
idCounter: 0,
1514
on: () => {},
1615
off: () => {},
1716
callOnConnection: () => {},

0 commit comments

Comments
 (0)