Skip to content

Commit

Permalink
Merge #583
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bors[bot] and hgzimmerman committed Aug 13, 2019
2 parents b9894dc + 1ef1b1a commit 104c6b2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ pub trait Component: Sized + 'static {
type Properties: Properties;
/// Initialization routine which could use a context.
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self;
/// Called after the component has been attached to the VDOM and it is safe to receive messages
/// from agents but before the browser updates the screen. If true is returned, the view will
/// be re-rendered and the user will not see the initial render.
fn mounted(&mut self) -> ShouldRender { false }
/// Called everytime when a messages of `Msg` type received. It also takes a
/// reference to a context.
fn update(&mut self, msg: Self::Message) -> ShouldRender;
Expand Down
22 changes: 14 additions & 8 deletions src/html/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,24 @@ struct CreatedState<COMP: Component> {
}

impl<COMP: Component + Renderable<COMP>> CreatedState<COMP> {
/// Called once immediately after the component is created.
fn mounted(mut self) -> Self {
if self.component.mounted() {
self.update()
} else {
self
}
}

fn update(mut self) -> Self {
let mut next_frame = self.component.view();
let node = next_frame.apply(&self.element, None, self.last_frame, &self.env);
if let Some(ref mut cell) = self.occupied {
*cell.borrow_mut() = node;
}

Self {
env: self.env,
component: self.component,
last_frame: Some(next_frame),
element: self.element,
occupied: self.occupied,
}
self.last_frame = Some(next_frame);
self
}
}

Expand Down Expand Up @@ -178,7 +182,9 @@ where
fn run(self: Box<Self>) {
let current_state = self.shared_state.replace(ComponentState::Processing);
self.shared_state.replace(match current_state {
ComponentState::Ready(state) => ComponentState::Created(state.create().update()),
ComponentState::Ready(state) => {
ComponentState::Created(state.create().update().mounted())
},
ComponentState::Created(_) | ComponentState::Destroyed => current_state,
ComponentState::Empty | ComponentState::Processing => {
panic!("unexpected component state: {}", current_state);
Expand Down

0 comments on commit 104c6b2

Please sign in to comment.