runtime.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. use std::cell::{Cell, Ref, RefCell};
  2. use crate::{innerlude::Scheduler, scope_context::ScopeContext, scopes::ScopeId};
  3. use std::rc::Rc;
  4. thread_local! {
  5. static RUNTIMES: RefCell<Vec<Rc<Runtime>>> = RefCell::new(vec![]);
  6. }
  7. /// Pushes a new scope onto the stack
  8. pub(crate) fn push_runtime(runtime: Rc<Runtime>) {
  9. RUNTIMES.with(|stack| stack.borrow_mut().push(runtime));
  10. }
  11. /// Pops a scope off the stack
  12. pub(crate) fn pop_runtime() {
  13. RUNTIMES.with(|stack| stack.borrow_mut().pop());
  14. }
  15. /// Runs a function with the current runtime
  16. pub(crate) fn with_runtime<F, R>(f: F) -> Option<R>
  17. where
  18. F: FnOnce(&Runtime) -> R,
  19. {
  20. RUNTIMES.with(|stack| {
  21. let stack = stack.borrow();
  22. stack.last().map(|r| f(r))
  23. })
  24. }
  25. /// Runs a function with the current scope
  26. pub(crate) fn with_current_scope<F, R>(f: F) -> Option<R>
  27. where
  28. F: FnOnce(&ScopeContext) -> R,
  29. {
  30. with_runtime(|runtime| {
  31. runtime
  32. .current_scope_id()
  33. .and_then(|scope| runtime.get_context(scope).map(|sc| f(&sc)))
  34. })
  35. .flatten()
  36. }
  37. /// A global runtime that is shared across all scopes that provides the async runtime and context API
  38. pub struct Runtime {
  39. pub(crate) scope_contexts: RefCell<Vec<Option<ScopeContext>>>,
  40. pub(crate) scheduler: Rc<Scheduler>,
  41. // We use this to track the current scope
  42. pub(crate) scope_stack: RefCell<Vec<ScopeId>>,
  43. pub(crate) rendering: Cell<bool>,
  44. }
  45. impl Runtime {
  46. pub(crate) fn new(scheduler: Rc<Scheduler>) -> Rc<Self> {
  47. Rc::new(Self {
  48. scheduler,
  49. scope_contexts: Default::default(),
  50. scope_stack: Default::default(),
  51. rendering: Cell::new(true),
  52. })
  53. }
  54. /// Get the current runtime
  55. pub fn current() -> Option<Rc<Self>> {
  56. RUNTIMES.with(|stack| stack.borrow().last().cloned())
  57. }
  58. /// Create a scope context. This slab is synchronized with the scope slab.
  59. pub(crate) fn create_context_at(&self, id: ScopeId, context: ScopeContext) {
  60. let mut contexts = self.scope_contexts.borrow_mut();
  61. if contexts.len() <= id.0 {
  62. contexts.resize_with(id.0 + 1, Default::default);
  63. }
  64. contexts[id.0] = Some(context);
  65. }
  66. pub(crate) fn remove_context(&self, id: ScopeId) {
  67. self.scope_contexts.borrow_mut()[id.0] = None;
  68. }
  69. /// Get the current scope id
  70. pub(crate) fn current_scope_id(&self) -> Option<ScopeId> {
  71. self.scope_stack.borrow().last().copied()
  72. }
  73. /// Get the context for any scope given its ID
  74. ///
  75. /// This is useful for inserting or removing contexts from a scope, or rendering out its root node
  76. pub(crate) fn get_context(&self, id: ScopeId) -> Option<Ref<'_, ScopeContext>> {
  77. Ref::filter_map(self.scope_contexts.borrow(), |contexts| {
  78. contexts.get(id.0).and_then(|f| f.as_ref())
  79. })
  80. .ok()
  81. }
  82. }
  83. /// A guard for a new runtime. This must be used to override the current runtime when importing components from a dynamic library that has it's own runtime.
  84. ///
  85. /// ```rust
  86. /// use dioxus::prelude::*;
  87. ///
  88. /// fn main() {
  89. /// let virtual_dom = VirtualDom::new(app);
  90. /// }
  91. ///
  92. /// fn app(cx: Scope) -> Element {
  93. /// render!{ Component { runtime: Runtime::current().unwrap() } }
  94. /// }
  95. ///
  96. /// // In a dynamic library
  97. /// #[derive(Props)]
  98. /// struct ComponentProps {
  99. /// runtime: std::rc::Rc<Runtime>,
  100. /// }
  101. ///
  102. /// impl PartialEq for ComponentProps {
  103. /// fn eq(&self, _other: &Self) -> bool {
  104. /// true
  105. /// }
  106. /// }
  107. ///
  108. /// fn Component(cx: Scope<ComponentProps>) -> Element {
  109. /// cx.use_hook(|| RuntimeGuard::new(cx.props.runtime.clone()));
  110. ///
  111. /// render! { div {} }
  112. /// }
  113. /// ```
  114. pub struct RuntimeGuard(Rc<Runtime>);
  115. impl RuntimeGuard {
  116. /// Create a new runtime guard that sets the current Dioxus runtime. The runtime will be reset when the guard is dropped
  117. pub fn new(runtime: Rc<Runtime>) -> Self {
  118. push_runtime(runtime.clone());
  119. Self(runtime)
  120. }
  121. }
  122. impl Drop for RuntimeGuard {
  123. fn drop(&mut self) {
  124. pop_runtime();
  125. }
  126. }