Skip to content

Commit

Permalink
fix: prevented global state caching on engine-side
Browse files Browse the repository at this point in the history
This was causing capsules that use global state in the same context as a
template to activate unreachable code.

Fixes #280.
  • Loading branch information
arctic-hen7 committed Apr 29, 2023
1 parent 2708508 commit 23e6deb
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions packages/perseus/src/reactor/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl<G: Html> Reactor<G> {
held_state
} else {
let global_state_ty = self.global_state.0.borrow();
dbg!(&global_state_ty);

This comment has been minimized.

Copy link
@Miroito

Miroito May 3, 2023

Contributor

Did you mean to keep this dbg! call?
From the docs at https://doc.rust-lang.org/std/macro.dbg.html I see that it will also print on release builds which might not be intended?

This comment has been minimized.

Copy link
@arctic-hen7

arctic-hen7 May 10, 2023

Author Member

Thank you! All fixed!

// We'll get the server-given global state
if let GlobalStateType::Server(server_state) = &*global_state_ty {
// Fall back to the state we were given, first
Expand All @@ -65,10 +66,15 @@ impl<G: Html> Reactor<G> {
.into_concrete()
.map_err(|err| ClientInvariantError::InvalidState { source: err })?;
let rx = unrx.make_rx();
// Set that as the new active global state
drop(global_state_ty);
let mut active_global_state = self.global_state.0.borrow_mut();
*active_global_state = GlobalStateType::Loaded(Box::new(rx.clone()));
// On the engine-side, do not set this as the active state, because that
// would compromise any capsules trying to access this (see #280)
#[cfg(client)]
{
// Set that as the new active global state
drop(global_state_ty);
let mut active_global_state = self.global_state.0.borrow_mut();
*active_global_state = GlobalStateType::Loaded(Box::new(rx.clone()));
}

rx
} else {
Expand Down

0 comments on commit 23e6deb

Please sign in to comment.