Struct perseus::reactor::Reactor

source ·
pub struct Reactor<G: Html> {
    pub router_state: RouterState,
    /* private fields */
}
Expand description

The core of Perseus’ browser-side systems. This forms a central point for all the Perseus state and rendering logic to operate from. In your own code, this will always be available in the Sycamore context system.

Note that this is also used on the engine-side for rendering.

Fields§

§router_state: RouterState

The router state.

Implementations§

source§

impl Reactor<DomNode>

source

pub fn handle_critical_error( cx: Scope<'_>, err: ClientError, error_views: &ErrorViews<DomNode> )

Creates the infrastructure necessary to handle a critical error, and then displays it. This is intended for use if the reactor cannot be instantiated, and it takes the app-level context to verify this.

Panics

This will panic if given a scope in which a reactor exists.

Visibility

This is broadly part of Perseus implementation details, and is exposed only for those foregoing #[perseus::main] or #[perseus::browser_main] to build their own custom browser-side entrypoint (do not do this unless you really need to).

source

pub fn handle_panic( panic_info: &PanicInfo<'_>, handler: Arc<dyn Fn(Scope<'_>, ClientError, ErrorContext, ErrorPosition) -> (View<SsrNode>, View<DomNode>) + Send + Sync> )

Creates the infrastructure necessary to handle a panic, and then displays an error created by the user’s ErrorViews. This function will only panic if certain fundamental functions of the web APIs are not defined, in which case no error message could ever be displayed to the user anyway.

A handler is manually provided to this, because the ErrorViews are typically not thread-safe once extracted from PerseusApp.

Visibility

Under absolutely no circumstances should this function ever be called outside a Perseus panic handler set in the entrypoint! It is exposed for custom entrypoints only.

source§

impl<G: Html> Reactor<G>

source

pub fn get_global_state<'a, I>(&self, cx: Scope<'a>) -> &'a Iwhere I: MakeUnrx + AnyFreeze + Clone, I::Unrx: MakeRx<Rx = I>,

Gets the global state. Note that this can only be used for reactive global state, since Perseus always expects your global state to be reactive.

Panics

This will panic if the app has no global state. If you don’t know whether or not there is global state, use .try_global_state() instead.

source

pub fn try_get_global_state<'a, I>( &self, cx: Scope<'a> ) -> Result<Option<&'a I>, ClientError>where I: MakeUnrx + AnyFreeze + Clone, I::Unrx: MakeRx<Rx = I>,

The underlying logic for .get_global_state(), except this will return None if the app does not have global state.

This will return an error if the state from the server was found to be invalid.

source§

impl<G: Html> Reactor<G>

source

pub fn thaw( &self, new_frozen_app: &str, thaw_prefs: ThawPrefs ) -> Result<(), ClientError>

Commands Perseus to ‘thaw’ the app from the given frozen state. You’ll also need to provide preferences for thawing, which allow you to control how different pages should prioritize frozen state over existing (or active) state. Once you call this, assume that any following logic will not run, as this may navigate to a different route in your app. How you get the frozen state to supply to this is up to you.

If the app has already been thawed from a previous frozen state, any state used from that will be considered active for this thawing.

This will return an error if the frozen state provided is invalid. However, if the frozen state for an individual page is invalid, it will be silently ignored in favor of either the active state or the server-provided state.

Note that any existing frozen app will be overriden by this.

If the app was last frozen while on an error page, this will not attempt to change the current route.

source

pub fn preload<'a, 'b: 'a>(&'b self, cx: Scope<'a>, url: &str)

Preloads the given URL from the server and caches it, preventing future network requests to fetch that page. Localization will be handled automatically.

This function automatically defers the asynchronous preloading work to a browser future for convenience. If you would like to access the underlying future, use .try_preload() instead.

To preload a widget, you must prefix its path with __capsule/.

Panics

This function will panic if any errors occur in preloading, such as the route being not found, or not localized. If the path you’re preloading is not hardcoded, use .try_preload() instead.

source

pub fn route_preload<'a, 'b: 'a>(&'b self, cx: Scope<'a>, url: &str)

Preloads the given URL from the server and caches it for the current route, preventing future network requests to fetch that page. On a route transition, this will be removed. Localization will be handled automatically.

WARNING: the route preloading system is under heavy construction at present!

This function automatically defers the asynchronous preloading work to a browser future for convenience. If you would like to access the underlying future, use .try_route_preload() instead.

To preload a widget, you must prefix its path with __capsule/.

Panics

This function will panic if any errors occur in preloading, such as the route being not found, or not localized. If the path you’re preloading is not hardcoded, use .try_route_preload() instead.

source

pub async fn try_preload(&self, url: &str) -> Result<(), ClientError>

A version of .preload() that returns a future that can resolve to an error. If the path you’re preloading is not hardcoded, you should use this. Localization will be handled automatically.

To preload a widget, you must prefix its path with __capsule/.

source

pub async fn try_route_preload(&self, url: &str) -> Result<(), ClientError>

A version of .route_preload() that returns a future that can resolve to an error. If the path you’re preloading is not hardcoded, you should use this. Localization will be handled automatically.

To preload a widget, you must prefix its path with __capsule/.

source§

impl<G: Html> Reactor<G>

source

pub fn register_no_state(&self, url: &PathMaybeWithLocale, is_widget: bool)

Registers a page/widget as definitely taking no state, which allows it to be cached fully, preventing unnecessary network requests. Any future attempt to set state will lead to errors (with exceptions for HSR).

source§

impl<G: Html> Reactor<G>

source

pub fn from_cx(cx: Scope<'_>) -> &Self

Gets a Reactor out of the given Sycamore scope’s context.

You should never need to worry about this function panicking, since your code will only ever run if a reactor is present.

source

pub fn try_get_translator(&self) -> Option<Translator>

Gets the currently active translator.

On the browser-side, this will return None under some error conditions, or before the initial load.

On the engine-side, this will return None under certain error conditions.

source

pub fn try_get_translator(&self) -> Option<Translator>

Gets the currently active translator.

On the browser-side, this will return None under some error conditions, or before the initial load.

On the engine-side, this will return None under certain error conditions.

source

pub fn get_translator(&self) -> Translator

Gets the currently active translator. Under some conditions, this will panic: .try_get_translator() is available as a non-panicking alternative.

Panics

Panics if used before the initial load on the browser, when there isn’t a translator yet, or if used on the engine-side when a translator is not available (which will be inside certain error views). Note that an engine-side panic would occur as the server is serving a request, which will lead to the request not being fulfilled.

Trait Implementations§

source§

impl<G: Debug + Html> Debug for Reactor<G>

source§

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

Formats the value using the given formatter. Read more
source§

impl<G: Html> Freeze for Reactor<G>

source§

fn freeze(&self) -> String

‘Freezes’ the reactive struct by making it unreactive and converting it to a String.
source§

impl<G: Html, M: MutableStore, T: TranslationsManager> TryFrom<PerseusAppBase<G, M, T>> for Reactor<G>

§

type Error = ClientError

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

fn try_from(app: PerseusAppBase<G, M, T>) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<G> !RefUnwindSafe for Reactor<G>

§

impl<G> !Send for Reactor<G>

§

impl<G> !Sync for Reactor<G>

§

impl<G> Unpin for Reactor<G>

§

impl<G> !UnwindSafe for Reactor<G>

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> AnyFreeze for Twhere T: Any + Freeze,

source§

fn as_any(&self) -> &(dyn Any + 'static)

Gives &dyn Any to enable downcasting.
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, 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.