Skip to content

Commit 711e42a

Browse files
authored
Support getting the lifecycle state machine status (#1136)
This PR introduces support for retrieving the lifecycle state machine status by adding a new "isInitialized" property. - Added a type test for the new boolean property in the TypeScript tests. - Implemented a corresponding native binding in C++ to expose the state. - Exposed the property via a getter in the JavaScript lifecycle module. Fix: #1135
1 parent aeaf2e8 commit 711e42a

File tree

5 files changed

+31
-0
lines changed

5 files changed

+31
-0
lines changed

lib/lifecycle.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,15 @@ class LifecycleNode extends Node {
591591
return this._changeState(SHUTDOWN_TRANSITION_LABEL, callbackReturnValue);
592592
}
593593

594+
/**
595+
* Check if state machine is initialized.
596+
*
597+
* @returns {boolean} true if the state machine is initialized; otherwise false.
598+
*/
599+
get isInitialized() {
600+
return rclnodejs.isInitialized(this._stateMachineHandle);
601+
}
602+
594603
/**
595604
* The GetState service handler.
596605
* @param {Object} request - The GetState service request.

src/rcl_lifecycle_bindings.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,18 @@ Napi::Value GetLifecycleShutdownTransitionLabel(
360360
return Napi::String::New(env, rcl_lifecycle_shutdown_label);
361361
}
362362

363+
Napi::Value IsInitialized(const Napi::CallbackInfo& info) {
364+
Napi::Env env = info.Env();
365+
RclHandle* state_machine_handle =
366+
RclHandle::Unwrap(info[0].As<Napi::Object>());
367+
rcl_lifecycle_state_machine_t* state_machine =
368+
reinterpret_cast<rcl_lifecycle_state_machine_t*>(
369+
state_machine_handle->ptr());
370+
const bool is_initialized =
371+
RCL_RET_OK == rcl_lifecycle_state_machine_is_initialized(state_machine);
372+
return Napi::Boolean::New(env, is_initialized);
373+
}
374+
363375
Napi::Object InitLifecycleBindings(Napi::Env env, Napi::Object exports) {
364376
exports.Set("createLifecycleStateMachine",
365377
Napi::Function::New(env, CreateLifecycleStateMachine));
@@ -383,6 +395,7 @@ Napi::Object InitLifecycleBindings(Napi::Env env, Napi::Object exports) {
383395
Napi::Function::New(env, GetLifecycleTransitionIdToLabel));
384396
exports.Set("getLifecycleShutdownTransitionLabel",
385397
Napi::Function::New(env, GetLifecycleShutdownTransitionLabel));
398+
exports.Set("isInitialized", Napi::Function::New(env, IsInitialized));
386399
return exports;
387400
}
388401

test/test-lifecycle.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ describe('LifecycleNode test suite', function () {
7272
it('lifecycleNode initial state', function () {
7373
const state = node.currentState;
7474
assert.equal(state.id, StateInterface.PRIMARY_STATE_UNCONFIGURED);
75+
assert.equal(node.isInitialized, true);
7576
});
7677

7778
it('lifecycleNode transitions', function () {

test/types/index.test-d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ expectType<rclnodejs.lifecycle.State>(lifecycleNode.activate());
120120
expectType<rclnodejs.lifecycle.State>(lifecycleNode.deactivate());
121121
expectType<rclnodejs.lifecycle.State>(lifecycleNode.cleanup());
122122
expectType<rclnodejs.lifecycle.State>(lifecycleNode.shutdown());
123+
expectType<boolean>(lifecycleNode.isInitialized);
123124

124125
// ---- Publisher ----
125126
const publisher = node.createPublisher(TYPE_CLASS, TOPIC);

types/lifecycle.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,13 @@ declare module 'rclnodejs' {
326326
topic: string,
327327
options?: Options
328328
): LifecyclePublisher<T>;
329+
330+
/**
331+
* Check if state machine is initialized.
332+
*
333+
* @returns {boolean} true if the state machine is initialized; otherwise false.
334+
*/
335+
get isInitialized(): boolean;
329336
}
330337
} // lifecycle namespace
331338
} // rclnodejs namespace

0 commit comments

Comments
 (0)