Struct perseus::state::PageStateStore
source · pub struct PageStateStore { /* private fields */ }
Expand description
A container for page state in Perseus. This is designed as a context store, in which one of each type can be stored. Therefore, it acts very similarly to Sycamore’s context system, though it’s specifically designed for each page to store one reactive properties object. In theory, you could interact with this entirely independently of Perseus’ state interface, though this isn’t recommended.
Paths in this store will have their locales prepended if the app uses i18n.
Implementations§
source§impl PageStateStore
impl PageStateStore
sourcepub fn new(max_size: usize) -> Self
pub fn new(max_size: usize) -> Self
Creates a new, empty page state store with the given maximum size. After this number of pages have been entered, the oldest ones will have their states eliminated. Note that individual pages can be marked for keeping or can be manually removed to circumvent these mechanisms.
sourcepub fn get_state<T: AnyFreeze + Clone>(
&self,
url: &PathMaybeWithLocale
) -> Option<T>
pub fn get_state<T: AnyFreeze + Clone>( &self, url: &PathMaybeWithLocale ) -> Option<T>
Gets an element out of the state by its type and URL. If the element
stored for the given URL doesn’t match the provided type, None
will be
returned.
This will NOT return any document metadata, if any exists.
sourcepub fn get_head(&self, url: &PathMaybeWithLocale) -> Option<String>
pub fn get_head(&self, url: &PathMaybeWithLocale) -> Option<String>
Gets the document metadata registered for a URL, if it exists.
sourcepub fn add_state<T: AnyFreeze + Clone>(
&self,
url: &PathMaybeWithLocale,
val: T,
is_widget: bool
) -> Result<(), ClientError>
pub fn add_state<T: AnyFreeze + Clone>( &self, url: &PathMaybeWithLocale, val: T, is_widget: bool ) -> Result<(), ClientError>
Adds page state to the entry in the store with the given URL, creating it if it doesn’t exist. Any state previously set for the item will be overridden, but any document metadata will be preserved.
This will be added to the end of the order
property, and any previous
entries of it in that list will be removed, unless it is specified to
be a widget.
If there’s already an entry for the given URL that has been marked as
not accepting state, this will return an error, and the entry will
not be added. When this is called for HSR purposes, this should be taken
with a grain of salt, as documented on .set_state()
for PssEntry
.
sourcepub fn add_head(&self, url: &PathMaybeWithLocale, head: String, is_widget: bool)
pub fn add_head(&self, url: &PathMaybeWithLocale, head: String, is_widget: bool)
Adds document metadata to the entry in the store for the given URL, creating it if it doesn’t exist.
This will be added to the end of the order
property, and any previous
entries of it in that list will be removed.
This will accept widgets adding empty heads, since they do still need to be registered.
sourcepub fn set_state_never(&self, url: &PathMaybeWithLocale, is_widget: bool)
pub fn set_state_never(&self, url: &PathMaybeWithLocale, is_widget: bool)
Sets the given entry as not being able to take any state. Any future attempt to register state for it will lead to silent failures and/or panics.
sourcepub fn contains(&self, url: &PathMaybeWithLocale) -> PssContains
pub fn contains(&self, url: &PathMaybeWithLocale) -> PssContains
Checks if the state contains an entry for the given URL.
sourcepub fn get_preloaded(
&self,
url: &PathMaybeWithLocale
) -> Option<PageDataPartial>
pub fn get_preloaded( &self, url: &PathMaybeWithLocale ) -> Option<PageDataPartial>
Gets a preloaded page. This will search both the globally and route-specifically preloaded pages.
Note that this will delete the preloaded page from the preload cache, since it’s expected to be parsed and rendered immediately. It should also have its head entered in the PSS.
sourcepub fn cycle_route_preloaded(&self, keep_urls: &[&PathMaybeWithLocale])
pub fn cycle_route_preloaded(&self, keep_urls: &[&PathMaybeWithLocale])
Clears all the routes that were preloaded for the last route, keeping only those listed (this should be used to make sure we don’t have to double-preload things).
sourcepub fn force_keep(&self, url: &PathMaybeWithLocale)
pub fn force_keep(&self, url: &PathMaybeWithLocale)
Forces the store to keep a certain page. This will prevent it from being evicted from the store, regardless of how many other pages are entered after it.
Warning: in the vast majority of cases, your use-case for this will be far better served by the global state system! (If you use this with mutable state, you are quite likely to shoot yourself in the foot.)
sourcepub fn force_remove(&self, url: &PathMaybeWithLocale) -> Option<PssEntry>
pub fn force_remove(&self, url: &PathMaybeWithLocale) -> Option<PssEntry>
Forcibly removes a page from the store. Generally, you should never need to use this function, but it’s provided for completeness. This could be used for preventing a certain page from being frozen, if necessary. Note that calling this in development will cause HSR to not work (since it relies on the state freezing system).
This returns the page’s state, if it was found.
Note: this will safely preserve the invariants of the store (as opposed to manual removal).
source§impl PageStateStore
impl PageStateStore
sourcepub fn freeze_to_hash_map(&self) -> HashMap<PathMaybeWithLocale, String>
pub fn freeze_to_hash_map(&self) -> HashMap<PathMaybeWithLocale, String>
Freezes the component entries into a new HashMap
of String
s to avoid
extra layers of deserialization. This does NOT include document
metadata, which will be re-requested from the server. (There is no
point in freezing that, since it can’t be unique for the user’s page
interactions, as it’s added directly as the server sends it.)
Note that the typed path system uses transparent serialization, and has no additional storage cost.
Trait Implementations§
source§impl Clone for PageStateStore
impl Clone for PageStateStore
source§fn clone(&self) -> PageStateStore
fn clone(&self) -> PageStateStore
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more