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

Way to access rendered component instance #45

Open
lifeart opened this issue Feb 5, 2020 · 7 comments
Open

Way to access rendered component instance #45

lifeart opened this issue Feb 5, 2020 · 7 comments

Comments

@lifeart
Copy link
Contributor

lifeart commented Feb 5, 2020

wor web components usage and for inegration with different external APIS, it's will be grate to have official ability to access rendered glimmer app.

following examples:

import { renderComponent } from '@glimmerx/core';
// renderComponent(klass, opts) => Promise

is it possible to return component instance as resolved promise argument?
or opts = { connected(instance) {} }

@xcambar
Copy link

xcambar commented Apr 17, 2020

I'd like to second this statement, especially as detailed here: glimmerjs/glimmer.js#235 (comment)

The absence of such API forces implementors to use unpleasing (to say the least) workarounds ;)

@argarcia-ottersoft
Copy link

any update on this? any workarounds?

@lifeart
Copy link
Contributor Author

lifeart commented Nov 24, 2021

@argarcia-ottersoft workaround exists, you could pass service into component and inside it's contructor assign this to some service property.

class App extends Component {
   @service app;
   constructor() {
       super(...arguments);
       this.app.setComponent(this);
   }

}

class MyService {
   component = null;
   setComponent(value) {
     this.component = value;
   }
}

const appService = new MyService();


await renderComponent(App, { services: { app: appService } } )
appService.component === App instance

@argarcia-ottersoft
Copy link

awesome! thanks @lifeart

follow up question, is there any way to properly unload the component? I'm trying to do cleanup in web components disconnectedCallback method

@lifeart
Copy link
Contributor Author

lifeart commented Nov 24, 2021

@argarcia-ottersoft there is issue for it - #123

But, I could assume that minimal unload logic should be like:

// app's hbs

class App extends Component {
   @tracked
   isAlive = true;
   async destroy() {
       this.isAlive = false;
       await new Promise((resolve) => setTimeout(resolve)); // need to wait for rerender
       // nested components should be destroyed here and all you need is cleanup services used for component
   }
}
{{#if this.isAlive}}
   ... app logic
{{/if}}

@argarcia-ottersoft
Copy link

argarcia-ottersoft commented Nov 24, 2021

@lifeart, any reason why a nested component in App's {{#if this.isAlive}}<ChildComponent />{{/if}} would not fire the willDestroy() hook after setting the App's isAlive property to false? The <ChildComponent /> does successfully get removed from the screen

@argarcia-ottersoft
Copy link

never mind, it was an error on my end. An exception was being thrown while looping through scheduledDestructors cause of a version mismatch of @glimmer/destroyable. glimmer-class-based-modifier depended on version 0.77.3 while glimmer.js depended on 0.77.6 so updating glimmer-class-based-modifier to 0.77.6 fixed issue.

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

3 participants