create a way to hoist ownership to a different scope
This commit is contained in:
1 parent
49001c59d3
commit
dce418140a
4 files changed
+53
-6
No files matched your search
@@ -70,6 +70,10 @@ impl<T: 'static> Signal<T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn origin_scope(&self) -> ScopeId {
|
||||
self.inner.origin_scope()
|
||||
}
|
||||
|
||||
pub fn read(&self) -> Ref<T> {
|
||||
let inner = self.inner.read();
|
||||
if let Some(current_scope_id) = current_scope_id() {
|
||||
|
||||
@@ -2,7 +2,11 @@ use std::cell::{Ref, RefMut};
|
||||
|
||||
use std::rc::Rc;
|
||||
|
||||
use dioxus_core::prelude::{consume_context, provide_root_context};
|
||||
use dioxus_core::prelude::{
|
||||
consume_context, consume_context_from_scope, current_scope_id, provide_context_to_scope,
|
||||
provide_root_context,
|
||||
};
|
||||
use dioxus_core::ScopeId;
|
||||
|
||||
use dioxus_copy::{CopyHandle, Owner, Store};
|
||||
|
||||
@@ -26,8 +30,19 @@ fn current_owner() -> Rc<Owner> {
|
||||
}
|
||||
}
|
||||
|
||||
fn owner_in_scope(scope: ScopeId) -> Rc<Owner> {
|
||||
match consume_context_from_scope(scope) {
|
||||
Some(rt) => rt,
|
||||
None => {
|
||||
let owner = Rc::new(current_store().owner());
|
||||
provide_context_to_scope(scope, owner).expect("in a virtual dom")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CopyValue<T: 'static> {
|
||||
pub value: CopyHandle<T>,
|
||||
origin_scope: ScopeId,
|
||||
}
|
||||
|
||||
impl<T: 'static> CopyValue<T> {
|
||||
@@ -36,9 +51,23 @@ impl<T: 'static> CopyValue<T> {
|
||||
|
||||
Self {
|
||||
value: owner.insert(value),
|
||||
origin_scope: current_scope_id().expect("in a virtual dom"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_in_scope(value: T, scope: ScopeId) -> Self {
|
||||
let owner = owner_in_scope(scope);
|
||||
|
||||
Self {
|
||||
value: owner.insert(value),
|
||||
origin_scope: scope,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn origin_scope(&self) -> ScopeId {
|
||||
self.origin_scope
|
||||
}
|
||||
|
||||
pub fn try_read(&self) -> Option<Ref<'_, T>> {
|
||||
self.value.try_read()
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user