Skip to content
Merged
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
9 changes: 9 additions & 0 deletions lib/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,15 @@ class LifecycleNode extends Node {
return this._changeState(SHUTDOWN_TRANSITION_LABEL, callbackReturnValue);
}

/**
* Check if state machine is initialized.
*
* @returns {boolean} true if the state machine is initialized; otherwise false.
*/
get isInitialized() {
return rclnodejs.isInitialized(this._stateMachineHandle);
}

/**
* The GetState service handler.
* @param {Object} request - The GetState service request.
Expand Down
13 changes: 13 additions & 0 deletions src/rcl_lifecycle_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,18 @@ Napi::Value GetLifecycleShutdownTransitionLabel(
return Napi::String::New(env, rcl_lifecycle_shutdown_label);
}

Napi::Value IsInitialized(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
RclHandle* state_machine_handle =
RclHandle::Unwrap(info[0].As<Napi::Object>());
rcl_lifecycle_state_machine_t* state_machine =
reinterpret_cast<rcl_lifecycle_state_machine_t*>(
state_machine_handle->ptr());
const bool is_initialized =
RCL_RET_OK == rcl_lifecycle_state_machine_is_initialized(state_machine);
return Napi::Boolean::New(env, is_initialized);
}

Napi::Object InitLifecycleBindings(Napi::Env env, Napi::Object exports) {
exports.Set("createLifecycleStateMachine",
Napi::Function::New(env, CreateLifecycleStateMachine));
Expand All @@ -383,6 +395,7 @@ Napi::Object InitLifecycleBindings(Napi::Env env, Napi::Object exports) {
Napi::Function::New(env, GetLifecycleTransitionIdToLabel));
exports.Set("getLifecycleShutdownTransitionLabel",
Napi::Function::New(env, GetLifecycleShutdownTransitionLabel));
exports.Set("isInitialized", Napi::Function::New(env, IsInitialized));
return exports;
}

Expand Down
1 change: 1 addition & 0 deletions test/test-lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ describe('LifecycleNode test suite', function () {
it('lifecycleNode initial state', function () {
const state = node.currentState;
assert.equal(state.id, StateInterface.PRIMARY_STATE_UNCONFIGURED);
assert.equal(node.isInitialized, true);
});

it('lifecycleNode transitions', function () {
Expand Down
1 change: 1 addition & 0 deletions test/types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ expectType<rclnodejs.lifecycle.State>(lifecycleNode.activate());
expectType<rclnodejs.lifecycle.State>(lifecycleNode.deactivate());
expectType<rclnodejs.lifecycle.State>(lifecycleNode.cleanup());
expectType<rclnodejs.lifecycle.State>(lifecycleNode.shutdown());
expectType<boolean>(lifecycleNode.isInitialized);

// ---- Publisher ----
const publisher = node.createPublisher(TYPE_CLASS, TOPIC);
Expand Down
7 changes: 7 additions & 0 deletions types/lifecycle.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,13 @@ declare module 'rclnodejs' {
topic: string,
options?: Options
): LifecyclePublisher<T>;

/**
* Check if state machine is initialized.
*
* @returns {boolean} true if the state machine is initialized; otherwise false.
*/
get isInitialized(): boolean;
}
} // lifecycle namespace
} // rclnodejs namespace
Loading