Feat: Add public API for tracking and reacting to controllers lifecycle by christophehenry · Pull Request #860 · hotwired/stimulus

Conversation

@christophehenry

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

Comment on lines +52 to +54

get lifecycle() {
return this.context.lifecycle
}

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?

christophehenry

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()
}
}

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

Is this project abandonned?

1 participant

@christophehenry