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

Setting an @attr property in a click listener not reflected in getter until next run loop (v2.0.0-beta) #293

Closed
ryecroft opened this issue Oct 24, 2022 · 2 comments

Comments

@ryecroft
Copy link

I'm confused by the behaviour of properties managed by the @attr decorator during click listener callbacks. Setting one of these properties during the connectedCallback() is immediately reflected when calling the getter for it, but this is not the case in click listeners.

Here's my component definition:

import { controller, attr } from '@github/catalyst'


@controller
class TestComponentElement extends HTMLElement {

  @attr testProp: string

  connectedCallback() {
    this.innerHTML = `hello`
    this.setAttribute( 'data-action', 'click:test-component#wasClicked' )
    console.log( this.testProp )  // prints 'undefined' as expected
    this.testProp = 'A'
    console.log( this.testProp ) // prints 'A' as expected
    this.testProp = 'B'
    console.log( this.testProp ) // prints 'B' as expected
  }


  // this is called when clicking the element
  wasClicked( _evt ) {
    console.log( this.testProp ) // prints 'B' as expected (set to this at end of connectedCallback())
    this.testProp = 'C'
    console.log( this.testProp ) // prints 'B' - not expected
    this.testProp = 'D'
    console.log( this.testProp ) // prints 'B' - not expected
    setTimeout( () => {
      console.log( this.testProp ) // prints 'D'
    }, 0 )
  }


}


export default TestComponentElement

the html is like this:

<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='UTF-8' />
    </head>
    <body>
        <div style='min-height: 20pt;'></div>
        <test-component></test-component>
    </div>
    <script type='module' src='/src/main.ts'></script>
</html>

I see here that the injected setter for the @attr decorator is async, and that it's not awaited here

Removing that async and the await Promise.resolve() here makes this unexpected behaviour go away (but breaks a load of other stuff in a real project).

Is this behaviour expected? Thanks

@ryecroft
Copy link
Author

Closed this issue because I think there's something weird with my setup. I now cannot reproduce it =\

@koddsson
Copy link
Contributor

Closed this issue because I think there's something weird with my setup. I now cannot reproduce it =\

Probably! Me and @keithamus looked at this and couldn't reproduce either. We added a test to validate in a874af9. Feel free to re-open with more info if you get it.

Thanks for the bug report, hopefully you get this sorted in your app!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants