Skip to content

Add waitForMessage utility for one-shot message reception#1444

Merged
minggangw merged 2 commits intoRobotWebTools:developfrom
minggangw:fix-1443
Mar 18, 2026
Merged

Add waitForMessage utility for one-shot message reception#1444
minggangw merged 2 commits intoRobotWebTools:developfrom
minggangw:fix-1443

Conversation

@minggangw
Copy link
Member

Adds waitForMessage(), the rclnodejs equivalent of rclpy's wait_for_message(msg_type, node, topic, time_to_wait). This utility creates a temporary subscription, waits for the first message to arrive on a topic, and returns it as a Promise. The temporary subscription is always cleaned up — on success, timeout, or error. Supports an optional timeout and QoS profile. The node must be spinning before calling this function.

New files:

  • lib/wait_for_message.js — Core implementation. Creates a temporary subscription with a one-shot callback that resolves the Promise on first message. Uses a settled guard to prevent double-resolution. Cleanup runs unconditionally via the settle() helper. Accepts optional { timeout, qos } options.
  • test/test-wait-for-message.js — 7 tests covering: successful message reception, timeout rejection, first-message-only semantics, different message types (String, Int32), indefinite wait without timeout, subscription cleanup after receiving, and subscription cleanup on timeout.

Modified files:

  • index.js — Added require('./lib/wait_for_message.js') and re-exported as waitForMessage property on the module.
  • types/index.d.ts — Added WaitForMessageOptions interface and generic waitForMessage<T>(typeClass, node, topic, options?) function declaration.

Fix: #1443

Copilot AI review requested due to automatic review settings March 18, 2026 02:30
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new waitForMessage() helper to rclnodejs to support one-shot reception of a message from a topic (Promise-based), similar to rclpy’s wait_for_message, including optional timeout and QoS configuration.

Changes:

  • Introduces lib/wait_for_message.js implementing a temporary subscription that resolves on the first received message and always cleans up.
  • Exposes the new utility via index.js as rclnodejs.waitForMessage.
  • Adds TypeScript typings and a new test suite covering success, timeout, first-message semantics, and cleanup behavior.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.

File Description
lib/wait_for_message.js Implements Promise-based one-shot subscription with optional timeout and unconditional cleanup.
index.js Re-exports the new utility on the main module API surface.
types/index.d.ts Declares WaitForMessageOptions and the waitForMessage() function type signature.
test/test-wait-for-message.js Adds tests for message reception, timeout behavior, and subscription cleanup.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +83 to +84
let receiveCount = 0;

Comment on lines +92 to +99
if (options.timeout != null && options.timeout >= 0) {
timer = setTimeout(() => {
settle(
new Error(
`waitForMessage timed out after ${options.timeout}ms on topic '${topic}'`
)
);
}, options.timeout);
@coveralls
Copy link

Coverage Status

coverage: 85.995% (+0.03%) from 85.964%
when pulling 11d2700 on minggangw:fix-1443
into aa6f1d3 on RobotWebTools:develop.

@minggangw minggangw merged commit 95e5a42 into RobotWebTools:develop Mar 18, 2026
16 checks passed
minggangw added a commit that referenced this pull request Mar 18, 2026
Adds `waitForMessage()`, the rclnodejs equivalent of rclpy's `wait_for_message(msg_type, node, topic, time_to_wait)`. This utility creates a temporary subscription, waits for the first message to arrive on a topic, and returns it as a Promise. The temporary subscription is always cleaned up — on success, timeout, or error. Supports an optional timeout and QoS profile. The node must be spinning before calling this function.

**New files:**

- `lib/wait_for_message.js` — Core implementation. Creates a temporary subscription with a one-shot callback that resolves the Promise on first message. Uses a `settled` guard to prevent double-resolution. Cleanup runs unconditionally via the `settle()` helper. Accepts optional `{ timeout, qos }` options.
- `test/test-wait-for-message.js` — 7 tests covering: successful message reception, timeout rejection, first-message-only semantics, different message types (String, Int32), indefinite wait without timeout, subscription cleanup after receiving, and subscription cleanup on timeout.

**Modified files:**

- `index.js` — Added `require('./lib/wait_for_message.js')` and re-exported as `waitForMessage` property on the module.
- `types/index.d.ts` — Added `WaitForMessageOptions` interface and generic `waitForMessage<T>(typeClass, node, topic, options?)` function declaration.

Fix: #1443
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add waitForMessage utility for one-shot message reception

3 participants