Skip to content

Conversation

@christophehenry
Copy link

@christophehenry christophehenry commented Sep 17, 2025

This is somewhat related to #859. This PR proposes a public API to track and react to controller's lifecycle in order to simplfy writing Stimulus extensions. This API is constituted from 2 parts:

  • the controller publicly exposing their current lifecycle state with the lifecycle property,
  • events trigered after each lifecycle callback reached.

There is currently no test on this. I'd appreciate any guidance on this matter. This is an early open to gather feedback as fast as possible.

@christophehenry christophehenry force-pushed the public-lifecycle branch 3 times, most recently from 1203e91 to d3462df Compare September 17, 2025 16:41
Comment on lines +52 to +54
get lifecycle() {
return this.context.lifecycle
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder is lifecycleIsAtLeast(lifecycle: Lifecycle), lifecycleIsAtMost(lifecycle: Lifecycle) and lifecycleIsBetween(from: Lifecycle, to: Lifecycle) utility functions are necessary here or if controller.lifecycle < Lifecycle.Initialized is sufficient?

Comment on lines +86 to +96
function onConnected() {
controller.element.addEventListener(`${controller.identifier}:connected`, removeEventListener)
// extend the controller
}

if(controller.lifecycle < Lifecycle.connected) {
controller.element.addEventListener(`${controller.identifier}:connected`, onConnected)
} else {
onConnected()
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There could be a controller method for this pattern, IDK.

@christophehenry
Copy link
Author

Is this project abandonned?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant