fix: use typeid of atoms for their unique ID (#558)

This commit is contained in:
Jon Kelley authored and GitHub committed 2022-09-17 10:50:05 -07:00
1 parent 540e785d8b
commit bf9901a6cd
4 files changed
+38 -11

No files matched your search

+21 -3
View File
@@ -1,11 +1,21 @@
use std::{any::Any, cell::RefCell, collections::HashMap, rc::Rc, sync::Arc};
use std::{
any::{Any, TypeId},
cell::RefCell,
collections::HashMap,
rc::Rc,
sync::Arc,
};
use dioxus_core::ScopeId;
use im_rc::HashSet;
use crate::Readable;
pub type AtomId = *const ();
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AtomId {
pub(crate) ptr: *const (),
pub(crate) type_id: TypeId,
}
pub struct AtomRoot {
pub atoms: RefCell<HashMap<AtomId, Slot>>,
@@ -44,7 +54,15 @@ impl AtomRoot {
// initialize the value if it's not already initialized
if let Some(slot) = atoms.get_mut(&f.unique_id()) {
slot.subscribers.insert(scope);
slot.value.clone().downcast().unwrap()
match slot.value.clone().downcast() {
Ok(res) => res,
Err(e) => panic!(
"Downcasting atom failed: {:?}. Has typeid of {:?} but needs typeid of {:?}",
f.unique_id(),
e.type_id(),
TypeId::of::<V>()
),
}
} else {
let value = Rc::new(f.init());
let mut subscribers = HashSet::new();