Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry committed Apr 25, 2020
1 parent 48ed5c1 commit 2a2ef46
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
11 changes: 7 additions & 4 deletions yew-router/examples/guide/src/guide.rs
Expand Up @@ -34,18 +34,21 @@ impl Component for Guide {
}
}

fn mounted(&mut self) -> ShouldRender {
fn rendered(&mut self, _first_render: bool) {
self.router_agent.send(GetCurrentRoute);
false
}

fn update(&mut self, msg: Self::Message) -> bool {
match msg {
Msg::UpdateRoute(route) => {
self.route = Some(route);
let new_route = Some(route);
if self.route != new_route {
self.route = new_route;
return true;
}
}
}
true
false
}

fn change(&mut self, _: Self::Properties) -> bool {
Expand Down
4 changes: 0 additions & 4 deletions yew-router/examples/guide/src/markdown_window.rs
Expand Up @@ -41,10 +41,6 @@ impl Component for MarkdownWindow {
}
}

fn mounted(&mut self) -> ShouldRender {
false
}

fn update(&mut self, msg: Self::Message) -> bool {
match msg {
Msg::MarkdownArrived(md) => {
Expand Down
3 changes: 1 addition & 2 deletions yew-router/src/router.rs
Expand Up @@ -185,9 +185,8 @@ where
}
}

fn mounted(&mut self) -> ShouldRender {
fn rendered(&mut self, _first_render: bool) {
self.router_agent.send(RouteRequest::GetCurrentRoute);
false
}

fn update(&mut self, msg: Self::Message) -> ShouldRender {
Expand Down
32 changes: 17 additions & 15 deletions yew/src/html/scope.rs
Expand Up @@ -174,7 +174,7 @@ impl<COMP: Component> Scope<COMP> {
enum ComponentState<COMP: Component> {
Empty,
Ready(ReadyState<COMP>),
Created((bool, CreatedState<COMP>)),
Created(CreatedState<COMP>),
Processing,
Destroyed,
}
Expand Down Expand Up @@ -203,6 +203,7 @@ struct ReadyState<COMP: Component> {
impl<COMP: Component> ReadyState<COMP> {
fn create(self) -> CreatedState<COMP> {
CreatedState {
rendered: false,
component: COMP::create(self.props, self.scope),
element: self.element,
last_frame: self.ancestor,
Expand All @@ -212,6 +213,7 @@ impl<COMP: Component> ReadyState<COMP> {
}

struct CreatedState<COMP: Component> {
rendered: bool,
element: Element,
component: COMP,
last_frame: Option<VNode>,
Expand All @@ -221,6 +223,7 @@ struct CreatedState<COMP: Component> {
impl<COMP: Component> CreatedState<COMP> {
/// Called after a component and all of its children have been rendered.
fn rendered(mut self, first_render: bool) -> Self {
self.rendered = true;
self.component.rendered(first_render);
self
}
Expand Down Expand Up @@ -256,14 +259,10 @@ where
fn run(self: Box<Self>) {
let current_state = self.shared_state.replace(ComponentState::Processing);
self.shared_state.replace(match current_state {
ComponentState::Created((needs_render, state)) => {
if needs_render {
ComponentState::Created((false, state.rendered(self.first_render)))
} else {
ComponentState::Created((needs_render, state))
}
ComponentState::Created(s) if !s.rendered => {
ComponentState::Created(s.rendered(self.first_render))
}
ComponentState::Destroyed => current_state,
ComponentState::Destroyed | ComponentState::Created(_) => current_state,
ComponentState::Empty | ComponentState::Processing | ComponentState::Ready(_) => {
panic!("unexpected component state: {}", current_state);
}
Expand All @@ -285,9 +284,7 @@ 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((true, state.create().update()))
}
ComponentState::Ready(s) => ComponentState::Created(s.create().update()),
ComponentState::Created(_) | ComponentState::Destroyed => current_state,
ComponentState::Empty | ComponentState::Processing => {
panic!("unexpected component state: {}", current_state);
Expand All @@ -309,7 +306,7 @@ where
{
fn run(self: Box<Self>) {
match self.shared_state.replace(ComponentState::Destroyed) {
ComponentState::Created((_, mut this)) => {
ComponentState::Created(mut this) => {
this.component.destroy();
if let Some(last_frame) = &mut this.last_frame {
last_frame.detach(&this.element);
Expand Down Expand Up @@ -341,7 +338,7 @@ where
fn run(self: Box<Self>) {
let current_state = self.shared_state.replace(ComponentState::Processing);
self.shared_state.replace(match current_state {
ComponentState::Created((needs_render, mut this)) => {
ComponentState::Created(mut this) => {
let should_update = match self.update {
ComponentUpdate::Message(message) => this.component.update(message),
ComponentUpdate::MessageBatch(messages) => messages
Expand All @@ -354,8 +351,13 @@ where
this.component.change(props)
}
};
let next_state = if should_update { this.update() } else { this };
ComponentState::Created((!needs_render && !should_update, next_state))
let next_state = if should_update {
this.rendered = false;
this.update()
} else {
this
};
ComponentState::Created(next_state)
}
ComponentState::Destroyed => current_state,
ComponentState::Processing | ComponentState::Ready(_) | ComponentState::Empty => {
Expand Down

0 comments on commit 2a2ef46

Please sign in to comment.