Skip to content

Commit

Permalink
Add ScopeId::ROOT (#1398)
Browse files Browse the repository at this point in the history
* add ScopeId::ROOT

* replace ScopeId(0) with ScopeId::ROOT

---------

Co-authored-by: Jani Mustonen <[email protected]>
  • Loading branch information
JaniM and JaniM authored Aug 25, 2023
1 parent de87ba6 commit 31f8bab
Show file tree
Hide file tree
Showing 22 changed files with 106 additions and 91 deletions.
2 changes: 1 addition & 1 deletion examples/mobile_demo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn app(cx: Scope) -> Element {
onclick: move|_| {
println!("Clicked!");
items.push(items.len());
cx.needs_update_any(ScopeId(0));
cx.needs_update_any(ScopeId::ROOT);
println!("Requested update");
},
"Add item"
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl ElementRef {
Self {
template: None,
path: ElementPath::Root(0),
scope: ScopeId(0),
scope: ScopeId::ROOT,
}
}
}
Expand All @@ -60,7 +60,7 @@ impl VirtualDom {
fn next_reference(&mut self, template: &VNode, path: ElementPath) -> ElementId {
let entry = self.elements.vacant_entry();
let id = entry.key();
let scope = self.runtime.current_scope_id().unwrap_or(ScopeId(0));
let scope = self.runtime.current_scope_id().unwrap_or(ScopeId::ROOT);

entry.insert(ElementRef {
// We know this is non-null because it comes from a reference
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pub struct EventHandler<'bump, T = ()> {
impl<T> Default for EventHandler<'_, T> {
fn default() -> Self {
Self {
origin: ScopeId(0),
origin: ScopeId::ROOT,
callback: Default::default(),
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/scope_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl ScopeContext {
pub fn provide_root_context<T: 'static + Clone>(&self, context: T) -> T {
with_runtime(|runtime| {
runtime
.get_context(ScopeId(0))
.get_context(ScopeId::ROOT)
.unwrap()
.provide_context(context)
})
Expand All @@ -203,7 +203,7 @@ impl ScopeContext {
/// This is good for tasks that need to be run after the component has been dropped.
pub fn spawn_forever(&self, fut: impl Future<Output = ()> + 'static) -> TaskId {
// The root scope will never be unmounted so we can just add the task at the top of the app
let id = self.tasks.spawn(ScopeId(0), fut);
let id = self.tasks.spawn(ScopeId::ROOT, fut);

// wake up the scheduler if it is sleeping
self.tasks
Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/scopes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ impl<'a, T> std::ops::Deref for Scoped<'a, T> {
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
pub struct ScopeId(pub usize);

impl ScopeId {
/// The root ScopeId.
///
/// This scope will last for the entire duration of your app, making it convenient for long-lived state
/// that is created dynamically somewhere down the component tree.
///
/// # Example
///
/// ```rust, ignore
/// use dioxus_signals::*;
/// let my_persistent_state = Signal::new_in_scope(ScopeId::ROOT, String::new());
/// ```
pub const ROOT: ScopeId = ScopeId(0);
}

/// A component's state separate from its props.
///
/// This struct exists to provide a common interface for all scopes without relying on generics.
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/virtual_dom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ impl VirtualDom {
);

// Unlike react, we provide a default error boundary that just renders the error as a string
root.provide_context(Rc::new(ErrorBoundary::new(ScopeId(0))));
root.provide_context(Rc::new(ErrorBoundary::new(ScopeId::ROOT)));

// the root element is always given element ID 0 since it's the container for the entire tree
dom.elements.insert(ElementRef::none());
Expand All @@ -289,7 +289,7 @@ impl VirtualDom {
///
/// This scope has a ScopeId of 0 and is the root of the tree
pub fn base_scope(&self) -> &ScopeState {
self.get_scope(ScopeId(0)).unwrap()
self.get_scope(ScopeId::ROOT).unwrap()
}

/// Build the virtualdom with a global context inserted into the base scope
Expand Down Expand Up @@ -547,10 +547,10 @@ impl VirtualDom {
/// ```
pub fn rebuild(&mut self) -> Mutations {
let _runtime = RuntimeGuard::new(self.runtime.clone());
match unsafe { self.run_scope(ScopeId(0)).extend_lifetime_ref() } {
match unsafe { self.run_scope(ScopeId::ROOT).extend_lifetime_ref() } {
// Rebuilding implies we append the created elements to the root
RenderReturn::Ready(node) => {
let m = self.create_scope(ScopeId(0), node);
let m = self.create_scope(ScopeId::ROOT, node);
self.mutations.edits.push(Mutation::AppendChildren {
id: ElementId(0),
m,
Expand Down Expand Up @@ -663,6 +663,6 @@ impl VirtualDom {
impl Drop for VirtualDom {
fn drop(&mut self) {
// Simply drop this scope which drops all of its children
self.drop_scope(ScopeId(0), true);
self.drop_scope(ScopeId::ROOT, true);
}
}
8 changes: 4 additions & 4 deletions packages/core/tests/attr_cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn attrs_cycle() {
]
);

dom.mark_dirty(ScopeId(0));
dom.mark_dirty(ScopeId::ROOT);
assert_eq!(
dom.render_immediate().santize().edits,
[
Expand All @@ -56,7 +56,7 @@ fn attrs_cycle() {
]
);

dom.mark_dirty(ScopeId(0));
dom.mark_dirty(ScopeId::ROOT);
assert_eq!(
dom.render_immediate().santize().edits,
[
Expand All @@ -65,7 +65,7 @@ fn attrs_cycle() {
]
);

dom.mark_dirty(ScopeId(0));
dom.mark_dirty(ScopeId::ROOT);
assert_eq!(
dom.render_immediate().santize().edits,
[
Expand All @@ -88,7 +88,7 @@ fn attrs_cycle() {
);

// we take the node taken by attributes since we reused it
dom.mark_dirty(ScopeId(0));
dom.mark_dirty(ScopeId::ROOT);
assert_eq!(
dom.render_immediate().santize().edits,
[
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tests/bubble_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn bubbles_error() {
let _edits = dom.rebuild().santize();
}

dom.mark_dirty(ScopeId(0));
dom.mark_dirty(ScopeId::ROOT);

_ = dom.render_immediate();
}
6 changes: 3 additions & 3 deletions packages/core/tests/context_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ fn state_shares() {
]
);

dom.mark_dirty(ScopeId(0));
dom.mark_dirty(ScopeId::ROOT);
_ = dom.render_immediate();
assert_eq!(dom.base_scope().consume_context::<i32>().unwrap(), 1);

dom.mark_dirty(ScopeId(0));
dom.mark_dirty(ScopeId::ROOT);
_ = dom.render_immediate();
assert_eq!(dom.base_scope().consume_context::<i32>().unwrap(), 2);

Expand All @@ -41,7 +41,7 @@ fn state_shares() {
[SetText { value: "Value is 2", id: ElementId(1,) },]
);

dom.mark_dirty(ScopeId(0));
dom.mark_dirty(ScopeId::ROOT);
dom.mark_dirty(ScopeId(2));
let edits = dom.render_immediate();
assert_eq!(
Expand Down
6 changes: 3 additions & 3 deletions packages/core/tests/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn cycling_elements() {
);
}

dom.mark_dirty(ScopeId(0));
dom.mark_dirty(ScopeId::ROOT);
assert_eq!(
dom.render_immediate().santize().edits,
[
Expand All @@ -33,7 +33,7 @@ fn cycling_elements() {
);

// notice that the IDs cycle back to ElementId(1), preserving a minimal memory footprint
dom.mark_dirty(ScopeId(0));
dom.mark_dirty(ScopeId::ROOT);
assert_eq!(
dom.render_immediate().santize().edits,
[
Expand All @@ -42,7 +42,7 @@ fn cycling_elements() {
]
);

dom.mark_dirty(ScopeId(0));
dom.mark_dirty(ScopeId::ROOT);
assert_eq!(
dom.render_immediate().santize().edits,
[
Expand Down
6 changes: 3 additions & 3 deletions packages/core/tests/diff_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn component_swap() {
);
}

dom.mark_dirty(ScopeId(0));
dom.mark_dirty(ScopeId::ROOT);
assert_eq!(
dom.render_immediate().santize().edits,
[
Expand All @@ -84,7 +84,7 @@ fn component_swap() {
]
);

dom.mark_dirty(ScopeId(0));
dom.mark_dirty(ScopeId::ROOT);
assert_eq!(
dom.render_immediate().santize().edits,
[
Expand All @@ -93,7 +93,7 @@ fn component_swap() {
]
);

dom.mark_dirty(ScopeId(0));
dom.mark_dirty(ScopeId::ROOT);
assert_eq!(
dom.render_immediate().santize().edits,
[
Expand Down
14 changes: 7 additions & 7 deletions packages/core/tests/diff_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ fn text_diff() {
let mut vdom = VirtualDom::new(app);
_ = vdom.rebuild();

vdom.mark_dirty(ScopeId(0));
vdom.mark_dirty(ScopeId::ROOT);
assert_eq!(
vdom.render_immediate().edits,
[SetText { value: "hello 1", id: ElementId(2) }]
);

vdom.mark_dirty(ScopeId(0));
vdom.mark_dirty(ScopeId::ROOT);
assert_eq!(
vdom.render_immediate().edits,
[SetText { value: "hello 2", id: ElementId(2) }]
);

vdom.mark_dirty(ScopeId(0));
vdom.mark_dirty(ScopeId::ROOT);
assert_eq!(
vdom.render_immediate().edits,
[SetText { value: "hello 3", id: ElementId(2) }]
Expand All @@ -46,7 +46,7 @@ fn element_swap() {
let mut vdom = VirtualDom::new(app);
_ = vdom.rebuild();

vdom.mark_dirty(ScopeId(0));
vdom.mark_dirty(ScopeId::ROOT);
assert_eq!(
vdom.render_immediate().santize().edits,
[
Expand All @@ -55,7 +55,7 @@ fn element_swap() {
]
);

vdom.mark_dirty(ScopeId(0));
vdom.mark_dirty(ScopeId::ROOT);
assert_eq!(
vdom.render_immediate().santize().edits,
[
Expand All @@ -64,7 +64,7 @@ fn element_swap() {
]
);

vdom.mark_dirty(ScopeId(0));
vdom.mark_dirty(ScopeId::ROOT);
assert_eq!(
vdom.render_immediate().santize().edits,
[
Expand All @@ -73,7 +73,7 @@ fn element_swap() {
]
);

vdom.mark_dirty(ScopeId(0));
vdom.mark_dirty(ScopeId::ROOT);
assert_eq!(
vdom.render_immediate().santize().edits,
[
Expand Down
Loading

0 comments on commit 31f8bab

Please sign in to comment.