Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion .github/workflows/windows-build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:

jobs:
build:
runs-on: windows-2025
runs-on: windows-2022
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -88,6 +88,7 @@ jobs:
- uses: actions/checkout@v7

- name: Build rclnodejs
id: native-build
shell: cmd
run: |
set PATH=C:\pixi_ws\.pixi\envs\default\Library\bin;C:\pixi_ws\.pixi\envs\default\Scripts;C:\pixi_ws\.pixi\envs\default\bin;%PATH%
Expand All @@ -101,6 +102,9 @@ jobs:
- name: Test rclnodejs
if: ${{ matrix.run_tests }}
uses: nick-fields/retry@v4
env:
# 'native' keeps exception tracing; '1' also enables synchronous JS logs.
RCLNODEJS_ACTION_DIAGNOSTICS: 'native'
with:
shell: cmd
max_attempts: 2
Expand All @@ -112,3 +116,15 @@ jobs:
call "C:\pixi_ws\ros2-windows\setup.bat" &&
py -3 -m pip install "lark==1.1.9" &&
call npm test

- name: Probe rcutils string boundaries
if: ${{ always() && matrix.run_tests && steps.native-build.outcome == 'success' }}
shell: cmd
run: |
set PATH=C:\pixi_ws\.pixi\envs\default\Library\bin;C:\pixi_ws\.pixi\envs\default\Scripts;C:\pixi_ws\.pixi\envs\default\bin;%PATH%
call "C:\pixi_ws\ros2-windows\setup.bat"
cl /nologo /EHsc /std:c++17 /MD /I"C:\pixi_ws\ros2-windows\include" /I"C:\pixi_ws\ros2-windows\include\rcutils" test\rcutils-string-boundary-probe.cpp /Fe:build\Release\rcutils-string-probe.exe /link /LIBPATH:"C:\pixi_ws\ros2-windows\lib" rcutils.lib
if errorlevel 1 exit /b 1
build\Release\rcutils-string-probe.exe bounded
if errorlevel 1 exit /b 1
build\Release\rcutils-string-probe.exe unbounded
33 changes: 29 additions & 4 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ import QoS from './qos.js';
import * as Rates from './rate.js';
import Service from './service.js';
import Subscription from './subscription.js';
import ObservableSubscription from './observable_subscription.js';
import ObservableSubscription, {
ObservableSubscriptionEntity,
} from './observable_subscription.js';
import MessageInfo from './message_info.js';
import ActionEndpointInfo from './action/endpoint_info.js';
import { declareQosParameters, _resolveQoS } from './qos_overriding_options.js';
Expand Down Expand Up @@ -910,6 +912,25 @@ class Node extends rclnodejs.ShadowNode {
* @see {@link https://www.omg.org/spec/DDS/1.4/PDF|Content-filter details at DDS 1.4 specification, Annex B}
*/
createSubscription(typeClass, topic, options, callback, eventCallbacks) {
const subscription = this._createSubscription(
Subscription,
typeClass,
topic,
options,
callback,
eventCallbacks
);
return this._registerEntity('subscriptions', subscription);
}

_createSubscription(
SubscriptionClass,
typeClass,
topic,
options,
callback,
eventCallbacks
) {
if (typeof typeClass === 'string' || typeof typeClass === 'object') {
typeClass = loader.loadInterface(typeClass);
}
Expand Down Expand Up @@ -970,7 +991,7 @@ class Node extends rclnodejs.ShadowNode {
);
}

let subscription = Subscription.createSubscription(
let subscription = SubscriptionClass.createSubscription(
this,
typeClass,
topic,
Expand All @@ -979,12 +1000,14 @@ class Node extends rclnodejs.ShadowNode {
eventCallbacks
);
debug('Finish creating subscription, topic = %s.', topic);
return this._registerEntity('subscriptions', subscription);
return subscription;
}

/**
* Create a Subscription that returns an RxJS Observable.
* This allows using reactive programming patterns with ROS 2 messages.
* The node owns the underlying subscription and completes the observable
* when that subscription or the node is destroyed.
*
* @param {function|string|object} typeClass - The ROS message class,
* OR a string representing the message class, e.g. 'std_msgs/msg/String',
Expand All @@ -1002,7 +1025,8 @@ class Node extends rclnodejs.ShadowNode {
createObservableSubscription(typeClass, topic, options, eventCallbacks) {
let observableSubscription = null;

const subscription = this.createSubscription(
const subscription = this._createSubscription(
ObservableSubscriptionEntity,
typeClass,
topic,
options,
Expand All @@ -1015,6 +1039,7 @@ class Node extends rclnodejs.ShadowNode {
);

observableSubscription = new ObservableSubscription(subscription, this);
this._registerEntity('subscriptions', subscription);
return observableSubscription;
}

Expand Down
63 changes: 42 additions & 21 deletions lib/observable_subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,55 @@
// limitations under the License.

import { Subject } from 'rxjs';
import Subscription from './subscription.js';

/**
* Native-backed subscription that finalizes its observable with the entity.
* @ignore
*/
export class ObservableSubscriptionEntity extends Subscription {
#subject = new Subject();

get observable() {
return this.#subject.asObservable();
}

_emit(message) {
if (!this.isDestroyed()) {
this.#subject.next(message);
}
}

finalizeDestruction() {
if (this.isDestroyed()) return;

try {
super.finalizeDestruction();
} finally {
this.#subject.complete();
}
}
}

/**
* A wrapper that provides RxJS Observable support for ROS 2 subscriptions.
* This class wraps a standard Subscription and emits messages through an Observable.
* The node owns the underlying subscription and completes the observable when
* that subscription or the node is destroyed.
*
* @class ObservableSubscription
* @hideconstructor
*/
class ObservableSubscription {
#subscription;
#subject;
#destroyed;
#node;

/**
* Create an ObservableSubscription wrapper.
* @param {Subscription} subscription - The underlying ROS 2 subscription
* @param {ObservableSubscriptionEntity} subscription - The underlying ROS 2 subscription
* @param {Node} node - The node that owns the subscription, used to destroy it
*/
constructor(subscription, node) {
this.#subscription = subscription;
this.#subject = new Subject();
this.#destroyed = false;
this.#node = node;
}

Expand All @@ -45,7 +71,7 @@ class ObservableSubscription {
* @type {Observable}
*/
get observable() {
return this.#subject.asObservable();
return this.#subscription.observable;
}

/**
Expand All @@ -65,11 +91,12 @@ class ObservableSubscription {
}

/**
* Check if this observable subscription has been destroyed.
* Check if the underlying subscription has been destroyed, including by
* node teardown.
* @type {boolean}
*/
get isDestroyed() {
return this.#destroyed;
return this.#subscription.isDestroyed();
}

/**
Expand All @@ -79,23 +106,17 @@ class ObservableSubscription {
* @param {any} message - The message to emit
*/
_emit(message) {
if (!this.#destroyed) {
this.#subject.next(message);
}
this.#subscription._emit(message);
}

/**
* Complete the observable and clean up resources.
* After calling this, no more messages will be emitted, and the underlying
* ROS 2 subscription is destroyed so it stops receiving and deserializing
* messages.
* Destroy the underlying ROS 2 subscription through its node and complete
* the observable. Destruction is idempotent and can be retried if handle
* synchronization fails before native cleanup.
* Unsubscribing from the observable alone does not destroy the subscription.
*/
complete() {
if (!this.#destroyed) {
this.#destroyed = true;
this.#subject.complete();
this.#node.destroySubscription(this.#subscription);
}
this.#node.destroySubscription(this.#subscription);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class Subscription extends Entity {
topic,
options
);
return new Subscription(
return new this(
handle,
typeClass,
topic,
Expand Down
Loading
Loading