Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed callbacks can fire before connect #726

Closed
ianterrell opened this issue Oct 11, 2023 · 2 comments
Closed

Changed callbacks can fire before connect #726

ianterrell opened this issue Oct 11, 2023 · 2 comments

Comments

@ianterrell
Copy link

I have a situation where I want polymorphic outlets, and I've solved it by having "bridging" controllers so that I can reference a homogeneous collection of outlets, where each member of that collection can reference another specific controller. I do it by setting a controller value on the bridging controller from the non-homogenous bridged controller.

In some circumstances I notice that the controllerValueChanged callback occurs before the bridging controller's connect is executed.

Bridging controller:

import { Controller } from "@hotwired/stimulus"

let xid = 0
export default class extends Controller {
  static values = {
    controller: String,
  }

  connect() {
    this.id = xid++
    console.log("ExtensionBridgeController#connect", this.id)
  }

  controllerValueChanged() {
    console.log("ExtensionBridgeController#controllerValueChanged", this.id, this.controllerValue)
  }
}

Bridged controller base class:

import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  connect() {
    this.element.setAttribute('data-ui--admin--wysiwyg--extension-bridge-controller-value', this.identifier)
  }
}

Observed order of operations:
Screenshot 2023-10-11 at 3 14 41 PM

For context, ultimately I do something like this in the controllerValueChanged:

this._bridgedController = this.application.getControllerForElementAndIdentifier(this.element, this.controllerValue)

But it's now growing more complex managing the synchronization of everything. I think my approach can use some work generally, and I'd love to see #628 or other approach fleshed out, but I think this may be a bug regardless.

@brunoprietog
Copy link
Collaborator

Hi, as mentioned in #490, invoking that callback after initializing the controller and before the controller is connected is the expected behavior. Maybe you could stop the method execution if the element is not connected with something like:

if (!this.element.isConnected) return

@ianterrell
Copy link
Author

Thank you, I misread and misinterpreted the commentary on #490. I had been erroneously conceptualizing connect() as a constructor and conflated it with initialization.

@ianterrell ianterrell closed this as not planned Won't fix, can't repro, duplicate, stale Oct 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants