Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/yew/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ trybuild = "1"
[features]
ssr = ["futures", "html-escape"]
csr = []
doc_test = ["csr"]
doc_test = ["csr", "ssr"]
wasm_test = ["csr"]
default = []

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
tokio = { version = "1.15.0", features = ["full"] }

[package.metadata.docs.rs]
features = ["doc_test", "ssr"]
features = ["doc_test"]
rustdoc-args = ["--cfg", "documenting"]
33 changes: 16 additions & 17 deletions packages/yew/src/html/component/lifecycle.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Component lifecycle module

use super::scope::{AnyScope, Scope};
use super::BaseComponent;
use super::{BaseComponent, ComponentId};
use crate::html::{Html, RenderError};
use crate::scheduler::{self, Runnable, Shared};
use crate::suspense::{BaseSuspense, Suspension};
Expand Down Expand Up @@ -153,9 +153,7 @@ pub(crate) struct ComponentState {

suspension: Option<Suspension>,

// Used for debug logging
#[cfg(debug_assertions)]
pub(crate) vcomp_id: usize,
pub(crate) comp_id: ComponentId,
}

impl ComponentState {
Expand All @@ -164,8 +162,7 @@ impl ComponentState {
scope: Scope<COMP>,
props: Rc<COMP::Properties>,
) -> Self {
#[cfg(debug_assertions)]
let vcomp_id = scope.vcomp_id;
let comp_id = scope.id;
let context = Context { scope, props };

let inner = Box::new(CompStateInner {
Expand All @@ -181,8 +178,7 @@ impl ComponentState {
#[cfg(feature = "csr")]
has_rendered: false,

#[cfg(debug_assertions)]
vcomp_id,
comp_id,
}
}

Expand All @@ -208,7 +204,7 @@ impl<COMP: BaseComponent> Runnable for CreateRunner<COMP> {
let mut current_state = self.scope.state.borrow_mut();
if current_state.is_none() {
#[cfg(debug_assertions)]
super::log_event(self.scope.vcomp_id, "create");
super::log_event(self.scope.id, "create");

*current_state = Some(ComponentState::new(
self.initial_render_state,
Expand Down Expand Up @@ -240,6 +236,7 @@ impl Runnable for UpdateRunner {
if let Some(state) = self.state.borrow_mut().as_mut() {
let schedule_render = match self.event {
UpdateEvent::Message => state.inner.flush_messages(),

#[cfg(feature = "csr")]
UpdateEvent::Properties(props, next_node_ref, next_sibling) => {
match state.render_state {
Expand Down Expand Up @@ -297,13 +294,13 @@ impl Runnable for UpdateRunner {

#[cfg(debug_assertions)]
super::log_event(
state.vcomp_id,
state.comp_id,
format!("update(schedule_render={})", schedule_render),
);

if schedule_render {
scheduler::push_component_render(
self.state.as_ptr() as usize,
state.comp_id,
RenderRunner {
state: self.state.clone(),
},
Expand All @@ -325,7 +322,7 @@ impl Runnable for DestroyRunner {
fn run(self: Box<Self>) {
if let Some(mut state) = self.state.borrow_mut().take() {
#[cfg(debug_assertions)]
super::log_event(state.vcomp_id, "destroy");
super::log_event(state.comp_id, "destroy");

state.inner.destroy();

Expand Down Expand Up @@ -357,7 +354,7 @@ impl Runnable for RenderRunner {
fn run(self: Box<Self>) {
if let Some(state) = self.state.borrow_mut().as_mut() {
#[cfg(debug_assertions)]
super::log_event(state.vcomp_id, "render");
super::log_event(state.comp_id, "render");

match state.inner.view() {
Ok(m) => self.render(state, m),
Expand All @@ -373,11 +370,13 @@ impl RenderRunner {
// suspension to parent element.
let shared_state = self.state.clone();

let comp_id = state.comp_id;

if suspension.resumed() {
// schedule a render immediately if suspension is resumed.

scheduler::push_component_render(
shared_state.as_ptr() as usize,
state.comp_id,
RenderRunner {
state: shared_state,
},
Expand All @@ -393,7 +392,7 @@ impl RenderRunner {

suspension.listen(Callback::from(move |_| {
scheduler::push_component_render(
shared_state.as_ptr() as usize,
comp_id,
RenderRunner {
state: shared_state.clone(),
},
Expand Down Expand Up @@ -442,7 +441,7 @@ impl RenderRunner {
state.has_rendered = true;

scheduler::push_component_rendered(
self.state.as_ptr() as usize,
state.comp_id,
RenderedRunner {
state: self.state.clone(),
first_render,
Expand Down Expand Up @@ -474,7 +473,7 @@ mod feat_csr {
fn run(self: Box<Self>) {
if let Some(state) = self.state.borrow_mut().as_mut() {
#[cfg(debug_assertions)]
super::super::log_event(state.vcomp_id, "rendered");
super::super::log_event(state.comp_id, "rendered");

if state.suspension.is_none() {
state.inner.rendered(self.first_render);
Expand Down
46 changes: 32 additions & 14 deletions packages/yew/src/html/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,46 @@ pub(crate) use scope::Scoped;
pub use scope::{AnyScope, Scope, SendAsMessage};
use std::rc::Rc;

use std::sync::atomic::{AtomicUsize, Ordering};

/// A unique component ID.
///
/// This type is provided to better distinguish between component IDs and the older pointer-based
/// component ids.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
pub(crate) struct ComponentId(usize);

impl Default for ComponentId {
fn default() -> Self {
static COMP_ID_COUNTER: AtomicUsize = AtomicUsize::new(0);

Self(COMP_ID_COUNTER.fetch_add(1, Ordering::SeqCst))
}
}

#[cfg(any(feature = "csr", feature = "ssr"))]
mod feat_csr_ssr {
use super::*;

impl ComponentId {
#[inline]
pub fn new() -> Self {
Self::default()
}
}

#[cfg(debug_assertions)]
thread_local! {
static EVENT_HISTORY: std::cell::RefCell<std::collections::HashMap<usize, Vec<String>>>
static EVENT_HISTORY: std::cell::RefCell<std::collections::HashMap<ComponentId, Vec<String>>>
= Default::default();
static COMP_ID_COUNTER: AtomicUsize = AtomicUsize::new(0);
}

/// Push [Component] event to lifecycle debugging registry
#[cfg(debug_assertions)]
pub(crate) fn log_event(vcomp_id: usize, event: impl ToString) {
pub(crate) fn log_event(comp_id: ComponentId, event: impl ToString) {
EVENT_HISTORY.with(|h| {
h.borrow_mut()
.entry(vcomp_id)
.entry(comp_id)
.or_default()
.push(event.to_string())
});
Expand All @@ -37,23 +62,16 @@ mod feat_csr_ssr {
/// Get [Component] event log from lifecycle debugging registry
#[cfg(debug_assertions)]
#[allow(dead_code)]
pub(crate) fn get_event_log(vcomp_id: usize) -> Vec<String> {
pub(crate) fn get_event_log(comp_id: ComponentId) -> Vec<String> {
EVENT_HISTORY.with(|h| {
h.borrow()
.get(&vcomp_id)
.get(&comp_id)
.map(|l| (*l).clone())
.unwrap_or_default()
})
}

#[cfg(debug_assertions)]
pub(crate) fn next_id() -> usize {
COMP_ID_COUNTER.with(|m| m.fetch_add(1, Ordering::Relaxed))
}

#[cfg(debug_assertions)]
use std::sync::atomic::{AtomicUsize, Ordering};
}

#[cfg(debug_assertions)]
#[cfg(any(feature = "csr", feature = "ssr"))]
pub(crate) use feat_csr_ssr::*;
Expand Down
16 changes: 8 additions & 8 deletions packages/yew/src/html/component/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use std::cell::RefCell;

#[cfg(any(feature = "csr", feature = "ssr"))]
use super::lifecycle::{ComponentState, UpdateEvent, UpdateRunner};
use super::BaseComponent;
use super::{BaseComponent, ComponentId};

use crate::callback::Callback;
use crate::context::{ContextHandle, ContextProvider};
use crate::html::IntoComponent;
Expand Down Expand Up @@ -112,8 +113,7 @@ pub struct Scope<COMP: BaseComponent> {
#[cfg(any(feature = "csr", feature = "ssr"))]
pub(crate) state: Shared<Option<ComponentState>>,

#[cfg(debug_assertions)]
pub(crate) vcomp_id: usize,
pub(crate) id: ComponentId,
}

impl<COMP: BaseComponent> fmt::Debug for Scope<COMP> {
Expand All @@ -134,8 +134,7 @@ impl<COMP: BaseComponent> Clone for Scope<COMP> {
#[cfg(any(feature = "csr", feature = "ssr"))]
state: self.state.clone(),

#[cfg(debug_assertions)]
vcomp_id: self.vcomp_id,
id: self.id,
}
}
}
Expand Down Expand Up @@ -211,6 +210,7 @@ mod feat_ssr {
let state = ComponentRenderState::Ssr { sender: Some(tx) };

scheduler::push_component_create(
self.id,
CreateRunner {
initial_render_state: state,
props,
Expand Down Expand Up @@ -325,8 +325,7 @@ mod feat_csr_ssr {
state,
parent,

#[cfg(debug_assertions)]
vcomp_id: super::super::next_id(),
id: ComponentId::new(),
}
}

Expand Down Expand Up @@ -415,6 +414,7 @@ mod feat_csr {
};

scheduler::push_component_create(
self.id,
CreateRunner {
initial_render_state: state,
props,
Expand All @@ -435,7 +435,7 @@ mod feat_csr {
next_sibling: NodeRef,
) {
#[cfg(debug_assertions)]
super::super::log_event(self.vcomp_id, "reuse");
super::super::log_event(self.id, "reuse");

self.push_update(UpdateEvent::Properties(props, node_ref, next_sibling));
}
Expand Down
Loading