|
| 1 | +// Copyright (c) 2025 Mahmoud Alghalayini. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +const { Subject } = require('rxjs'); |
| 18 | + |
| 19 | +/** |
| 20 | + * A wrapper that provides RxJS Observable support for ROS 2 subscriptions. |
| 21 | + * This class wraps a standard Subscription and emits messages through an Observable. |
| 22 | + * |
| 23 | + * @class ObservableSubscription |
| 24 | + * @hideconstructor |
| 25 | + */ |
| 26 | +class ObservableSubscription { |
| 27 | + #subscription; |
| 28 | + #subject; |
| 29 | + #destroyed; |
| 30 | + |
| 31 | + /** |
| 32 | + * Create an ObservableSubscription wrapper. |
| 33 | + * @param {Subscription} subscription - The underlying ROS 2 subscription |
| 34 | + */ |
| 35 | + constructor(subscription) { |
| 36 | + this.#subscription = subscription; |
| 37 | + this.#subject = new Subject(); |
| 38 | + this.#destroyed = false; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Get the RxJS Observable for this subscription. |
| 43 | + * Use this to pipe operators and subscribe to messages. |
| 44 | + * @type {Observable} |
| 45 | + */ |
| 46 | + get observable() { |
| 47 | + return this.#subject.asObservable(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Get the underlying ROS 2 subscription. |
| 52 | + * @type {Subscription} |
| 53 | + */ |
| 54 | + get subscription() { |
| 55 | + return this.#subscription; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Get the topic name. |
| 60 | + * @type {string} |
| 61 | + */ |
| 62 | + get topic() { |
| 63 | + return this.#subscription.topic; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Check if this observable subscription has been destroyed. |
| 68 | + * @type {boolean} |
| 69 | + */ |
| 70 | + get isDestroyed() { |
| 71 | + return this.#destroyed; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Internal method to emit a message to subscribers. |
| 76 | + * Called by the subscription's processResponse. |
| 77 | + * @private |
| 78 | + * @param {any} message - The message to emit |
| 79 | + */ |
| 80 | + _emit(message) { |
| 81 | + if (!this.#destroyed) { |
| 82 | + this.#subject.next(message); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Complete the observable and clean up resources. |
| 88 | + * After calling this, no more messages will be emitted. |
| 89 | + */ |
| 90 | + complete() { |
| 91 | + if (!this.#destroyed) { |
| 92 | + this.#destroyed = true; |
| 93 | + this.#subject.complete(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Alias for complete() for consistency with RxJS naming. |
| 99 | + */ |
| 100 | + destroy() { |
| 101 | + this.complete(); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +module.exports = ObservableSubscription; |
0 commit comments