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

An ever Increasing Component ID #2537

Merged
merged 9 commits into from Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/yew/Cargo.toml
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"]
31 changes: 15 additions & 16 deletions packages/yew/src/html/component/lifecycle.rs
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: usize,
}

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
22 changes: 6 additions & 16 deletions packages/yew/src/html/component/mod.rs
Expand Up @@ -14,46 +14,36 @@ pub(crate) use scope::Scoped;
pub use scope::{AnyScope, Scope, SendAsMessage};
use std::rc::Rc;

#[cfg(debug_assertions)]
#[cfg(any(feature = "csr", feature = "ssr"))]
mod feat_csr_ssr {
#[cfg(debug_assertions)]
thread_local! {
static EVENT_HISTORY: std::cell::RefCell<std::collections::HashMap<usize, 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: usize, event: impl ToString) {
EVENT_HISTORY.with(|h| {
h.borrow_mut()
.entry(vcomp_id)
.entry(comp_id)
.or_default()
.push(event.to_string())
});
}

/// 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: usize) -> 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
17 changes: 10 additions & 7 deletions packages/yew/src/html/component/scope.rs
Expand Up @@ -8,6 +8,7 @@ use std::cell::RefCell;
#[cfg(any(feature = "csr", feature = "ssr"))]
use super::lifecycle::{ComponentState, UpdateEvent, UpdateRunner};
use super::BaseComponent;

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: usize,
}

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 @@ -269,6 +269,7 @@ mod feat_csr_ssr {
use super::*;
use crate::scheduler::{self, Shared};
use std::cell::Ref;
use std::sync::atomic::{AtomicUsize, Ordering};

#[derive(Debug)]
pub(crate) struct MsgQueue<Msg>(Shared<Vec<Msg>>);
Expand Down Expand Up @@ -308,6 +309,8 @@ mod feat_csr_ssr {
}
}

static COMP_ID_COUNTER: AtomicUsize = AtomicUsize::new(0);

impl<COMP: BaseComponent> Scope<COMP> {
/// Crate a scope with an optional parent scope
pub(crate) fn new(parent: Option<AnyScope>) -> Self {
Expand All @@ -325,8 +328,7 @@ mod feat_csr_ssr {
state,
parent,

#[cfg(debug_assertions)]
vcomp_id: super::super::next_id(),
id: COMP_ID_COUNTER.fetch_add(1, Ordering::SeqCst),
}
}

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

scheduler::push_component_create(
self.id,
CreateRunner {
initial_render_state: state,
props,
Expand All @@ -435,7 +438,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