Round 1 of next_hydration_key()

This commit is contained in:
Greg Johnston 2022-12-14 20:38:37 -05:00
parent 218c4d3c90
commit c463579faa
2 changed files with 12 additions and 7 deletions

View file

@ -201,6 +201,7 @@ pub(crate) struct Runtime {
pub scope_contexts: RefCell<SparseSecondaryMap<ScopeId, HashMap<TypeId, Box<dyn Any>>>>,
#[allow(clippy::type_complexity)]
pub scope_cleanups: RefCell<SparseSecondaryMap<ScopeId, Vec<Box<dyn FnOnce()>>>>,
pub scope_node_ids: RefCell<SecondaryMap<ScopeId, usize>>,
pub signals: RefCell<SlotMap<SignalId, Rc<RefCell<dyn Any>>>>,
pub signal_subscribers: RefCell<SecondaryMap<SignalId, RefCell<HashSet<EffectId>>>>,
pub effects: RefCell<SlotMap<EffectId, Rc<dyn AnyEffect>>>,

View file

@ -325,14 +325,18 @@ impl Scope {
})
}
/// Sets the latest hydration key.
pub fn set_hydration_key(&self, id: usize) {
with_runtime(self.runtime, |runtime| runtime.set_hydration_key(id))
}
/// Gets the latest hydration key.
pub fn get_hydration_key(&self) -> Option<usize> {
with_runtime(self.runtime, |runtime| runtime.get_hydration_key())
pub fn next_hydration_key(&self) -> Option<String> {
with_runtime(self.runtime, |runtime| {
let mut ids = runtime.scope_node_ids.borrow_mut();
if let Some(entry) = ids.entry(self.id) {
let current_key = entry.or_default();
*current_key += 1;
Some(format!("{:?}-{}", self.id, current_key))
} else {
None
}
})
}
/// The current key for an HTML fragment created by server-rendering a `<Suspense/>` component.