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

source

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.

source

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.

source

pub fn get_head(&self, url: &PathMaybeWithLocale) -> Option<String>

Gets the document metadata registered for a URL, if it exists.

source

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.

source

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.

source

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.

source

pub fn contains(&self, url: &PathMaybeWithLocale) -> PssContains

Checks if the state contains an entry for the given URL.

source

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.

source

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).

source

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.)

source

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

source

pub fn freeze_to_hash_map(&self) -> HashMap<PathMaybeWithLocale, String>

Freezes the component entries into a new HashMap of Strings 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

source§

fn clone(&self) -> PageStateStore

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for PageStateStore

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.