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>
impl Reactor<DomNode>
sourcepub fn handle_critical_error(
cx: Scope<'_>,
err: ClientError,
error_views: &ErrorViews<DomNode>
)
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).
sourcepub fn handle_panic(
panic_info: &PanicInfo<'_>,
handler: Arc<dyn Fn(Scope<'_>, ClientError, ErrorContext, ErrorPosition) -> (View<SsrNode>, View<DomNode>) + Send + Sync>
)
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>
impl<G: Html> Reactor<G>
sourcepub fn get_global_state<'a, I>(&self, cx: Scope<'a>) -> &'a Iwhere
I: MakeUnrx + AnyFreeze + Clone,
I::Unrx: MakeRx<Rx = I>,
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.
sourcepub 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>,
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>
impl<G: Html> Reactor<G>
sourcepub fn thaw(
&self,
new_frozen_app: &str,
thaw_prefs: ThawPrefs
) -> Result<(), ClientError>
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.
sourcepub fn preload<'a, 'b: 'a>(&'b self, cx: Scope<'a>, url: &str)
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.
sourcepub fn route_preload<'a, 'b: 'a>(&'b self, cx: Scope<'a>, url: &str)
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.
sourcepub async fn try_preload(&self, url: &str) -> Result<(), ClientError>
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/
.
sourcepub async fn try_route_preload(&self, url: &str) -> Result<(), ClientError>
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>
impl<G: Html> Reactor<G>
sourcepub fn register_no_state(&self, url: &PathMaybeWithLocale, is_widget: bool)
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>
impl<G: Html> Reactor<G>
sourcepub fn from_cx(cx: Scope<'_>) -> &Self
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.
sourcepub fn try_get_translator(&self) -> Option<Translator>
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.
sourcepub fn try_get_translator(&self) -> Option<Translator>
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.
sourcepub fn get_translator(&self) -> Translator
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.