Skip to main content

ComponentContext

Struct ComponentContext 

Source
pub struct ComponentContext {
    pub id: String,
    pub name: String,
    pub mounting: bool,
    pub request_metadata: Map<String, Value>,
    pub effects: Map<String, Value>,
    pub memo: Map<String, Value>,
    pub request: Option<Arc<Request>>,
    pub streamed: Vec<Value>,
}
Expand description

The context one lifecycle hook or action receives.

Fields§

§id: String

The component’s instance ID.

§name: String

The component’s registered name, such as counter.

§mounting: bool

Whether this render is the component’s first.

§request_metadata: Map<String, Value>

Metadata the browser sent with the call, such as island or async.

§effects: Map<String, Value>

Effects the component adds during this request.

§memo: Map<String, Value>

Memo entries the component adds during this request.

§request: Option<Arc<Request>>

The browser’s request, so an action can read the signed-in user, run a policy, or use a service without the framework cloning the request per component.

§streamed: Vec<Value>

The frames an action streamed, in the order it wrote them.

Implementations§

Source§

impl ComponentContext

Source

pub fn mounting( id: impl Into<String>, name: impl Into<String>, request: Option<Arc<Request>>, ) -> Self

A context for a component that is mounting, in a request that has request.

Source

pub fn updating( id: impl Into<String>, name: impl Into<String>, request: Option<Arc<Request>>, ) -> Self

A context for a component that is updating.

Source

pub async fn validate(&mut self, component: &dyn Component) -> Result<()>

Runs the component’s declared rules against its current state.

Every failing field’s first message is recorded in the snapshot’s memo.errors, which the browser’s $errors reads, and the whole set comes back in LivewireError::ValidationFailed so the caller can answer without an HTTP failure.

§Errors

Returns LivewireError::ValidationFailed when a rule rejects a field, and the framework’s own error when a rule could not run at all, such as a check that needs the database.

Source

pub fn add_error( &mut self, field: impl Into<String>, message: impl Into<String>, )

Records a validation error for field, which the browser reads from the snapshot’s memo.errors and shows through $errors.

Source

pub fn stream(&mut self, name: impl Into<String>, content: impl Into<String>)

Sends content to the element marked wire:stream="name", as the action runs.

The browser appends it as the frame arrives, so a long action can show progress; the ordinary response follows the frames.

Source

pub fn add_effect(&mut self, name: &str, value: Value)

Adds value under name, replacing any value already there.

Source

pub fn xjs(&mut self, expression: impl Into<String>)

Queues a JavaScript expression the browser evaluates once the answer morphed.

The client reads effects.xjs as a list of {expression, params} entries, so a second call appends instead of replacing what the first one asked for.

Source

pub fn push_effect(&mut self, name: &str, value: Value)

Adds value to the list under name, keeping a list that is already there.

Source

pub fn add_memo(&mut self, key: &str, value: Value)

Adds a memo entry.

Source

pub fn download( &mut self, name: impl Into<String>, content: impl AsRef<[u8]>, content_type: impl Into<String>, )

Sends content to the browser as a file, instead of replacing the component’s markup.

The response carries the file base64-encoded in the download effect, which the runtime turns into a save dialog; the component renders nothing.

Source

pub fn request(&self) -> Option<&Request>

The browser’s request, or None when the component rendered outside a request.

Source

pub async fn user<U: Authenticatable>(&self) -> Result<Option<U>>

The signed-in user of the browser’s request.

§Errors

Fails when the component rendered outside a request.

Trait Implementations§

Source§

impl Clone for ComponentContext

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ComponentContext

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 T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where 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, N> NotifiedBy<T> for N
where T: Notification<N>,

Source§

fn __routes(&self) -> Vec<Route>

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more