fix some ownership issues

This commit is contained in:
Evan Almloff 2023-08-08 13:18:15 -07:00
parent d6089bbd35
commit ba7a663eb6
4 changed files with 20 additions and 22 deletions

View file

@ -5,7 +5,7 @@ use crate::{
};
use rustc_hash::FxHashSet;
use std::{
any::{Any, TypeId},
any::Any,
cell::{Cell, RefCell},
fmt::Debug,
future::Future,
@ -25,7 +25,7 @@ pub struct ScopeContext {
pub(crate) height: u32,
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) spawned_tasks: RefCell<FxHashSet<TaskId>>,
@ -99,9 +99,7 @@ impl ScopeContext {
self.shared_contexts
.borrow()
.iter()
.find(|(k, _)| *k == TypeId::of::<T>())
.map(|(_, v)| v)?
.downcast_ref::<T>()
.find_map(|any| any.downcast_ref::<T>())
.cloned()
}
@ -121,7 +119,7 @@ impl ScopeContext {
.shared_contexts
.borrow()
.iter()
.find_map(|(_, any)| any.downcast_ref::<T>())
.find_map(|any| any.downcast_ref::<T>())
{
return Some(shared.clone());
}
@ -159,14 +157,14 @@ impl ScopeContext {
// If the context exists, swap it out for the new value
for ctx in contexts.iter_mut() {
// 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());
return value;
}
}
// Else, just push it
contexts.push((TypeId::of::<T>(), Box::new(value.clone())));
contexts.push(Box::new(value.clone()));
value
}

View file

@ -1,16 +1,16 @@
use core::{self, fmt::Debug};
use std::cell::RefCell;
use std::fmt::{self, Formatter};
use std::marker::PhantomData;
use std::rc::Rc;
//
use dioxus_core::prelude::*;
use crate::dependency::Dep;
use crate::use_signal;
use crate::{dependency::Dependency, CopyValue};
use crate::{use_signal, Signal};
#[derive(Default, Clone, Copy)]
#[derive(Default, Clone)]
pub(crate) struct EffectStack {
pub(crate) effects: CopyValue<Vec<Effect>>,
pub(crate) effects: Rc<RefCell<Vec<Effect>>>,
}
pub(crate) fn get_effect_stack() -> EffectStack {
@ -65,7 +65,7 @@ impl Debug for Effect {
impl Effect {
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.
@ -85,11 +85,11 @@ impl Effect {
pub fn try_run(&self) {
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();
{
get_effect_stack().effects.write().pop();
get_effect_stack().effects.borrow_mut().pop();
}
}
}

View file

@ -3,8 +3,8 @@ use std::cell::{Ref, RefMut};
use std::rc::Rc;
use dioxus_core::prelude::{
consume_context, consume_context_from_scope, current_scope_id, provide_context_to_scope,
provide_root_context,
consume_context, consume_context_from_scope, current_scope_id, provide_context,
provide_context_to_scope, provide_root_context,
};
use dioxus_core::ScopeId;
@ -25,7 +25,7 @@ fn current_owner() -> Rc<Owner> {
Some(rt) => rt,
None => {
let owner = Rc::new(current_store().owner());
provide_root_context(owner).expect("in a virtual dom")
provide_context(owner).expect("in a virtual dom")
}
}
}

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 {
subscribers: Default::default(),
@ -87,7 +87,7 @@ pub fn selector<R: PartialEq>(mut f: impl FnMut() -> R + 'static) -> ReadOnlySig
value: f(),
});
{
get_effect_stack().effects.write().pop();
// get_effect_stack().effects.borrow_mut().pop();
}
effect.callback.value.set(Box::new(move || {