fix some ownership issues

This commit is contained in:
Evan Almloff committed 2023-08-08 13:18:15 -07:00
1 parent d6089bbd35
commit ba7a663eb6
4 files changed
+20 -22

No files matched your search

+6 -8
View File
@@ -5,7 +5,7 @@ use crate::{
}; };
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use std::{ use std::{
any::{Any, TypeId}, any::Any,
cell::{Cell, RefCell}, cell::{Cell, RefCell},
fmt::Debug, fmt::Debug,
future::Future, future::Future,
@@ -25,7 +25,7 @@ pub struct ScopeContext {
pub(crate) height: u32, pub(crate) height: u32,
pub(crate) suspended: Cell<bool>, pub(crate) suspended: Cell<bool>,
pub(crate) shared_contexts: RefCell<Vec<(TypeId, Box<dyn Any>)>>, pub(crate) shared_contexts: RefCell<Vec<Box<dyn Any>>>,
pub(crate) tasks: Rc<Scheduler>, pub(crate) tasks: Rc<Scheduler>,
pub(crate) spawned_tasks: RefCell<FxHashSet<TaskId>>, pub(crate) spawned_tasks: RefCell<FxHashSet<TaskId>>,
@@ -99,9 +99,7 @@ impl ScopeContext {
self.shared_contexts self.shared_contexts
.borrow() .borrow()
.iter() .iter()
.find(|(k, _)| *k == TypeId::of::<T>()) .find_map(|any| any.downcast_ref::<T>())
.map(|(_, v)| v)?
.downcast_ref::<T>()
.cloned() .cloned()
} }
@@ -121,7 +119,7 @@ impl ScopeContext {
.shared_contexts .shared_contexts
.borrow() .borrow()
.iter() .iter()
.find_map(|(_, any)| any.downcast_ref::<T>()) .find_map(|any| any.downcast_ref::<T>())
{ {
return Some(shared.clone()); return Some(shared.clone());
} }
@@ -159,14 +157,14 @@ impl ScopeContext {
// If the context exists, swap it out for the new value // If the context exists, swap it out for the new value
for ctx in contexts.iter_mut() { for ctx in contexts.iter_mut() {
// Swap the ptr directly // Swap the ptr directly
if let Some(ctx) = ctx.1.downcast_mut::<T>() { if let Some(ctx) = ctx.downcast_mut::<T>() {
std::mem::swap(ctx, &mut value.clone()); std::mem::swap(ctx, &mut value.clone());
return value; return value;
} }
} }
// Else, just push it // Else, just push it
contexts.push((TypeId::of::<T>(), Box::new(value.clone()))); contexts.push(Box::new(value.clone()));
value value
} }
+9 -9
View File
@@ -1,16 +1,16 @@
use core::{self, fmt::Debug}; use core::{self, fmt::Debug};
use std::cell::RefCell;
use std::fmt::{self, Formatter}; use std::fmt::{self, Formatter};
use std::marker::PhantomData; use std::rc::Rc;
//
use dioxus_core::prelude::*; use dioxus_core::prelude::*;
use crate::dependency::Dep; use crate::use_signal;
use crate::{dependency::Dependency, CopyValue}; use crate::{dependency::Dependency, CopyValue};
use crate::{use_signal, Signal};
#[derive(Default, Clone, Copy)] #[derive(Default, Clone)]
pub(crate) struct EffectStack { pub(crate) struct EffectStack {
pub(crate) effects: CopyValue<Vec<Effect>>, pub(crate) effects: Rc<RefCell<Vec<Effect>>>,
} }
pub(crate) fn get_effect_stack() -> EffectStack { pub(crate) fn get_effect_stack() -> EffectStack {
@@ -65,7 +65,7 @@ impl Debug for Effect {
impl Effect { impl Effect {
pub(crate) fn current() -> Option<Self> { pub(crate) fn current() -> Option<Self> {
get_effect_stack().effects.read().last().copied() get_effect_stack().effects.borrow().last().copied()
} }
/// Create a new effect. The effect will be run immediately and whenever any signal it reads changes. /// Create a new effect. The effect will be run immediately and whenever any signal it reads changes.
@@ -85,11 +85,11 @@ impl Effect {
pub fn try_run(&self) { pub fn try_run(&self) {
if let Some(mut callback) = self.callback.try_write() { if let Some(mut callback) = self.callback.try_write() {
{ {
get_effect_stack().effects.write().push(*self); get_effect_stack().effects.borrow_mut().push(*self);
} }
callback(); callback();
{ {
get_effect_stack().effects.write().pop(); get_effect_stack().effects.borrow_mut().pop();
} }
} }
} }
+3 -3
View File
@@ -3,8 +3,8 @@ use std::cell::{Ref, RefMut};
use std::rc::Rc; use std::rc::Rc;
use dioxus_core::prelude::{ use dioxus_core::prelude::{
consume_context, consume_context_from_scope, current_scope_id, provide_context_to_scope, consume_context, consume_context_from_scope, current_scope_id, provide_context,
provide_root_context, provide_context_to_scope, provide_root_context,
}; };
use dioxus_core::ScopeId; use dioxus_core::ScopeId;
@@ -25,7 +25,7 @@ fn current_owner() -> Rc<Owner> {
Some(rt) => rt, Some(rt) => rt,
None => { None => {
let owner = Rc::new(current_store().owner()); let owner = Rc::new(current_store().owner());
provide_root_context(owner).expect("in a virtual dom") provide_context(owner).expect("in a virtual dom")
} }
} }
} }
+2 -2
View File
@@ -78,7 +78,7 @@ pub fn selector<R: PartialEq>(mut f: impl FnMut() -> R + 'static) -> ReadOnlySig
}; };
{ {
get_effect_stack().effects.write().push(effect); get_effect_stack().effects.borrow_mut().push(effect);
} }
state.inner.value.set(SignalData { state.inner.value.set(SignalData {
subscribers: Default::default(), subscribers: Default::default(),
@@ -87,7 +87,7 @@ pub fn selector<R: PartialEq>(mut f: impl FnMut() -> R + 'static) -> ReadOnlySig
value: f(), value: f(),
}); });
{ {
get_effect_stack().effects.write().pop(); // get_effect_stack().effects.borrow_mut().pop();
} }
effect.callback.value.set(Box::new(move || { effect.callback.value.set(Box::new(move || {