Merge branch 'master' into issue-1179
This commit is contained in:
38 files changed
+1681
-1655
No files matched your search
@@ -6,7 +6,7 @@ use crate::{
|
||||
use std::{
|
||||
any::{Any, TypeId},
|
||||
backtrace::Backtrace,
|
||||
cell::RefCell,
|
||||
cell::{Cell, RefCell},
|
||||
error::Error,
|
||||
fmt::{Debug, Display},
|
||||
rc::Rc,
|
||||
@@ -472,8 +472,8 @@ pub fn ErrorBoundary<'a>(cx: Scope<'a, ErrorBoundaryProps<'a>>) -> Element {
|
||||
attr_paths: &[],
|
||||
};
|
||||
VNode {
|
||||
parent: Default::default(),
|
||||
stable_id: Default::default(),
|
||||
parent: Cell::new(None),
|
||||
stable_id: Cell::new(None),
|
||||
key: None,
|
||||
template: std::cell::Cell::new(TEMPLATE),
|
||||
root_ids: bumpalo::collections::Vec::with_capacity_in(1usize, __cx.bump()).into(),
|
||||
|
||||
@@ -87,6 +87,16 @@ impl Runtime {
|
||||
self.scope_stack.borrow().last().copied()
|
||||
}
|
||||
|
||||
/// Call this function with the current scope set to the given scope
|
||||
///
|
||||
/// Useful in a limited number of scenarios, not public.
|
||||
pub(crate) fn with_scope<O>(&self, id: ScopeId, f: impl FnOnce() -> O) -> O {
|
||||
self.scope_stack.borrow_mut().push(id);
|
||||
let o = f();
|
||||
self.scope_stack.borrow_mut().pop();
|
||||
o
|
||||
}
|
||||
|
||||
/// Get the context for any scope given its ID
|
||||
///
|
||||
/// This is useful for inserting or removing contexts from a scope, or rendering out its root node
|
||||
@@ -137,6 +147,17 @@ impl RuntimeGuard {
|
||||
push_runtime(runtime.clone());
|
||||
Self(runtime)
|
||||
}
|
||||
|
||||
/// Run a function with a given runtime and scope in context
|
||||
pub fn with<O>(runtime: Rc<Runtime>, scope: Option<ScopeId>, f: impl FnOnce() -> O) -> O {
|
||||
let guard = Self::new(runtime.clone());
|
||||
let o = match scope {
|
||||
Some(scope) => Runtime::with_scope(&runtime, scope, f),
|
||||
None => f(),
|
||||
};
|
||||
drop(guard);
|
||||
o
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for RuntimeGuard {
|
||||
|
||||
@@ -318,11 +318,16 @@ pub fn spawn(fut: impl Future<Output = ()> + 'static) {
|
||||
with_current_scope(|cx| cx.spawn(fut));
|
||||
}
|
||||
|
||||
/// Spawn a future on a component given its [`ScopeId`].
|
||||
pub fn spawn_at(fut: impl Future<Output = ()> + 'static, scope_id: ScopeId) -> Option<TaskId> {
|
||||
with_runtime(|rt| rt.get_context(scope_id).unwrap().push_future(fut))
|
||||
}
|
||||
|
||||
/// Spawn a future that Dioxus won't clean up when this component is unmounted
|
||||
///
|
||||
/// This is good for tasks that need to be run after the component has been dropped.
|
||||
pub fn spawn_forever(fut: impl Future<Output = ()> + 'static) -> Option<TaskId> {
|
||||
with_current_scope(|cx| cx.spawn_forever(fut))
|
||||
spawn_at(fut, ScopeId(0))
|
||||
}
|
||||
|
||||
/// Informs the scheduler that this task is no longer needed and should be removed.
|
||||
|
||||
Reference in new issue
Block a user