Add waitForMessage utility for one-shot message reception#1444
Merged
minggangw merged 2 commits intoRobotWebTools:developfrom Mar 18, 2026
Merged
Add waitForMessage utility for one-shot message reception#1444minggangw merged 2 commits intoRobotWebTools:developfrom
minggangw merged 2 commits intoRobotWebTools:developfrom
Conversation
There was a problem hiding this comment.
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.jsimplementing a temporary subscription that resolves on the first received message and always cleans up. - Exposes the new utility via
index.jsasrclnodejs.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.
test/test-wait-for-message.js
Outdated
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); |
This was referenced Mar 18, 2026
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
waitForMessage(), the rclnodejs equivalent of rclpy'swait_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 asettledguard to prevent double-resolution. Cleanup runs unconditionally via thesettle()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— Addedrequire('./lib/wait_for_message.js')and re-exported aswaitForMessageproperty on the module.types/index.d.ts— AddedWaitForMessageOptionsinterface and genericwaitForMessage<T>(typeClass, node, topic, options?)function declaration.Fix: #1443