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

Feature: on_mount() component lifecycle method #572

Closed
hgzimmerman opened this issue Aug 8, 2019 · 3 comments · Fixed by #583
Closed

Feature: on_mount() component lifecycle method #572

hgzimmerman opened this issue Aug 8, 2019 · 3 comments · Fixed by #583

Comments

@hgzimmerman
Copy link
Member

Description

**I'm submitting a Feature Request

I want to have one of my components send a request to an agent as soon as it is created and have its reply message assist in setting up the component.

What I currently do is send the message off in the create() method and hope that the component mounts and is ready to receive the response from the agent in time. In practice, it always does, but it doesn't seem correct to send off requests before the newly-created component is mounted and ready to handle replies.

I propose that an overridable on_mount() method be added to the Component trait. By default, it would not contain any code, and would be called after the component is attached to the vdom(?).

I admit that I'm coming from a point of ignorance and I accept that some queuing mechanism may already exist to avoid reply messages being dropped when their target component isn't ready. If that is the case, I'll happily close this issue and consider the sending messages to agents in create() acceptable.

@jstarry
Copy link
Member

jstarry commented Aug 12, 2019

@hgzimmerman Yeah, this proposal sounds great! Feel free to get started and ping on gitter or here if you have any questions

@hgzimmerman
Copy link
Member Author

I can open a PR whenever, I'd just like to check some assumptions first:

If I understand correctly, the component lifecycle is as follows:

  • Empty
  • Ready
  • Created
  • Destroyed
    with Processing as a standin during updates/transitions.

I've stuck the on_mount() call (embedded in mounted() for convenience) in the transition between Ready and Created, like this:

 ComponentState::Created(state.create().update().mounted())

The way a component is mounted appears to rely on CreatedState::update() calling VNode::apply() calling VComp::apply(), which in turn calls Unmounted::mount() and then sets the VComp's state to Mounted(mounted). As far as I can tell, the first call to the aforementioned update() is the first time the component will be attached to the vdom tree.

If the above assumption is correct, then I think I stuck the on_mount() call in the correct location.

@jstarry
Copy link
Member

jstarry commented Aug 12, 2019

Yeah sounds right to me!

bors bot added a commit that referenced this issue Aug 13, 2019
583: Add default mounted() method to Component trait r=jstarry a=hgzimmerman

Fixes #572

I made the choice to have `on_mount()` not return a `ShouldRender` bool, because I don't think there is a situation where altering parts of a component state relevant to the `view()` should ever take place in `on_mount()` instead of `create()`. Let me know your thoughts, because its an easy enough change to make.

Co-authored-by: Henry Zimmerman <zimhen7@gmail.com>
@bors bors bot closed this as completed in #583 Aug 13, 2019
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

Successfully merging a pull request may close this issue.

2 participants