Skip to content

Commit

Permalink
Prevent unnecessary rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
futursolo committed Nov 11, 2022
1 parent e9b60b3 commit c31c47b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
24 changes: 11 additions & 13 deletions packages/yew/src/app_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::scheduler;
#[derive(Debug)]
pub struct AppHandle<COMP: Component> {
/// `Scope` holder
pub(crate) scope: Scope,
scope: Scope,
_marker: PhantomData<COMP>,
destroyed: bool,
}
Expand Down Expand Up @@ -43,18 +43,16 @@ where
};
let hosting_root = BSubtree::create_root(&host);

{
let scope = app.scope.clone();
scheduler::push(move || {
scope.mount(
intrinsic,
hosting_root,
host,
NodeRef::default(),
NodeRef::default(),
);
});
}
let scope = app.scope.clone();
scheduler::push(move || {
scope.mount(
intrinsic,
hosting_root,
host,
NodeRef::default(),
NodeRef::default(),
);
});

app
}
Expand Down
1 change: 0 additions & 1 deletion packages/yew/src/dom_bundle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ pub(crate) struct Bundle(BNode);

impl Bundle {
/// Creates a new bundle.
pub const fn new() -> Self {
Self(BNode::List(BList::new()))
}
Expand Down
14 changes: 10 additions & 4 deletions packages/yew/src/html/component/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::scope::Scope;
use crate::dom_bundle::{DomSlot, Realized};
use crate::html::{Html, Intrinsical, NodeRef, RenderError};
use crate::suspense::{resume_suspension, suspend_suspension, DispatchSuspension, Suspension};
use crate::{scheduler, Callback, ContextProvider, HookContext};
use crate::{Callback, ContextProvider, HookContext};

pub(crate) struct ComponentState {
pub(super) ctx: HookContext,
Expand Down Expand Up @@ -64,7 +64,14 @@ impl ComponentState {
}
}

pub fn run_render(scope: &Scope) {
pub fn run_render(scope: &Scope, step: usize) {
let current_step = scope.render_step_cell().get();
// The desired change has been applied.
if current_step > step {
return;
}

scope.render_step_cell().set(current_step + 1);
if let Some(state) = scope.state_cell().borrow_mut().as_mut() {
state.render(scope);
}
Expand Down Expand Up @@ -166,8 +173,7 @@ impl ComponentState {
{
let scope = scope.clone();
suspension.listen(Callback::from(move |_| {
let scope = scope.clone();
scheduler::push(move || ComponentState::run_render(&scope));
scope.schedule_render();
}));
}

Expand Down
11 changes: 10 additions & 1 deletion packages/yew/src/html/component/scope.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Component scope module
use std::any::TypeId;
use std::cell::Cell;
#[cfg(feature = "csr")]
use std::cell::RefCell;
use std::rc::Rc;
Expand All @@ -21,6 +22,7 @@ struct ScopeInner {
#[cfg(feature = "csr")]
pub(crate) state: RefCell<Option<ComponentState>>,

render_step: Cell<usize>,
parent: Option<Scope>,
}

Expand All @@ -41,14 +43,19 @@ impl Scope {
self.inner.id
}

pub(crate) fn render_step_cell(&self) -> &Cell<usize> {
&self.inner.render_step
}

/// Schedules a render.
pub(crate) fn schedule_render(&self) {
#[cfg(feature = "csr")]
{
use crate::scheduler;

let scope = self.clone();
scheduler::push(move || ComponentState::run_render(&scope));
let step = self.inner.render_step.get();
scheduler::push(move || ComponentState::run_render(&scope, step));
}
}

Expand Down Expand Up @@ -166,6 +173,7 @@ mod feat_csr_ssr {
state: RefCell::new(None),
parent,

render_step: Cell::new(0),
id: COMP_ID_COUNTER.fetch_add(1, Ordering::SeqCst),
}),
}
Expand All @@ -191,6 +199,7 @@ mod feat_csr {
type_id: TypeId::of::<()>(),
state: RefCell::default(),
parent: None,
render_step: Cell::new(0),
}),
}
}
Expand Down

0 comments on commit c31c47b

Please sign in to comment.