Add spinUntilFutureComplete for Promise-based spin lifecycle#1442
Open
minggangw wants to merge 2 commits intoRobotWebTools:developfrom
Open
Add spinUntilFutureComplete for Promise-based spin lifecycle#1442minggangw wants to merge 2 commits intoRobotWebTools:developfrom
minggangw wants to merge 2 commits intoRobotWebTools:developfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a Promise-oriented spinning helper to rclnodejs, mirroring rclpy’s spin_until_future_complete to simplify async workflows and reduce missed node.stop() cleanup.
Changes:
- Added
Node.spinUntilFutureComplete(promise, timeoutMs?)to manage spin start/stop around an awaited Promise. - Added module-level
rclnodejs.spinUntilFutureComplete(node, promise, timeoutMs?)wrapper. - Added TypeScript typings and a new mocha test suite covering resolution/rejection/timeout and spin state preservation.
Reviewed changes
Copilot reviewed 3 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| lib/node.js | Implements spinUntilFutureComplete() including spin lifecycle management and optional timeout. |
| index.js | Exposes a module-level wrapper function delegating to the Node method. |
| types/node.d.ts | Adds the Node method TypeScript declaration and docs. |
| types/index.d.ts | Adds the module-level TypeScript declaration and docs. |
| test/test-spin-until-future-complete.js | Adds test coverage for success, failure, timeout, and wrapper usage. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+583
to
+588
| reject( | ||
| new Error( | ||
| `spinUntilFutureComplete timed out after ${timeoutMs}ms` | ||
| ) | ||
| ); | ||
| }, timeoutMs); |
lib/node.js
Outdated
Comment on lines
+573
to
+579
| const wasSpinning = this.spinning; | ||
| if (!wasSpinning) { | ||
| this.spin(); | ||
| } | ||
|
|
||
| try { | ||
| if (timeoutMs != null && timeoutMs >= 0) { |
| const neverResolves = new Promise(() => {}); | ||
| await assert.rejects( | ||
| () => node.spinUntilFutureComplete(neverResolves, 500), | ||
| { message: /timed out/ } |
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
spinUntilFutureComplete(), the rclnodejs equivalent of rclpy'sspin_until_future_complete(node, future, timeout_sec). This method starts spinning the node (if not already spinning), awaits a Promise to settle, and automatically stops spinning when done (if it started it). Supports an optional timeout that rejects withTimeoutErroron expiry. ValidatestimeoutMswithTypeValidationError/RangeValidationErrorfor type safety. Guarantees cleanup in all code paths viatry/finally, eliminating a common source of resource leaks where users forget to callnode.stop()after async operations.New files:
test/test-spin-until-future-complete.js— 8 tests covering: promise resolution, rejection, timeout (assertsTimeoutErrorby name), already-spinning preservation, auto-stop on completion, delayed promises, invalid argument validation, and module-level function.Modified files:
lib/node.js— AddedspinUntilFutureComplete(promise, timeoutMs)async method. Validates the promise argument viaTypeValidationError. ValidatestimeoutMswhen provided:TypeValidationErrorfor non-finite numbers,RangeValidationErrorfor negative values. Manages spin start/stop lifecycle viatry/finally, and usesPromise.racewith aTimeoutError-rejecting timeout promise whentimeoutMsis provided. AddedTimeoutErrorto the destructured imports fromerrors.js.index.js— Added module-levelspinUntilFutureComplete(node, promise, timeoutMs)wrapper matching the rclpy global function pattern.types/node.d.ts— Added generic TypeScript declarationspinUntilFutureComplete<T>(promise: Promise<T>, timeoutMs?: number): Promise<T>.types/index.d.ts— Added module-level TypeScript declarationspinUntilFutureComplete<T>(node: Node, promise: Promise<T>, timeoutMs?: number): Promise<T>.Fix: #1441