Skip to main content

Signal

Struct Signal 

Source
pub struct Signal<T>
where T: Clone + PartialEq,
{ /* private fields */ }
Expand description

A reactive signal handle.

Allows reading, writing, and subscribing to changes. Implements Copy for ergonomic use; all copies share the same underlying state.

SAFETY: The inner pointer is allocated via Box::leak and lives for the entire program. This is safe in single-threaded WASM contexts where no concurrent access can occur.

Implementations§

Source§

impl<T> Signal<T>
where T: Clone + PartialEq,

Implementation of reactive signal operations.

Source

pub fn new(value: T) -> Self

Creates a new Signal with the given initial value.

The inner state is allocated via Box::leak and lives for the remainder of the program. This is safe in single-threaded WASM contexts where no concurrent access can occur.

§Arguments
  • T - The initial value of the signal.
§Returns
  • Self - A handle to the newly created reactive signal.
Source

pub fn get(&self) -> T

Returns the current value of the signal.

§Returns
  • T - The current value of the signal.
Source

pub fn try_get(&self) -> Option<T>

Attempts to return the current value of the signal without panicking.

§Returns
  • Some(T) - The current value if the borrow succeeds.
  • None - If the inner value is already mutably borrowed.
Source

pub fn subscribe<F>(&self, callback: F)
where F: FnMut() + 'static,

Subscribes a callback to be invoked when the signal changes.

§Arguments
  • FnMut() + 'static - The callback to invoke when the signal changes.
Source

pub fn replace_subscribe<F>(&self, callback: F)
where F: FnMut() + 'static,

Replaces all listeners with a single new callback.

Unlike subscribe, which appends a listener, this method clears any existing listeners first and then adds the new one. This prevents listener accumulation across re-renders: each signal is guaranteed to have at most one active listener at any time, eliminating cascading set() calls that would otherwise grow exponentially.

§Arguments
  • FnMut() + 'static - The callback to invoke when the signal changes.
Source

pub fn clear_listeners(&self)

Removes all subscribed listeners from this signal and marks it as inactive. After calling this method, subsequent set() and try_set() calls become complete no-ops: the value is not updated, no listeners are invoked, and schedule_signal_update() is not called. This is used during hook context cleanup when a match arm switch discards old signals, ensuring that stale setInterval closures referencing these signals become entirely harmless.

Source

pub fn set(&self, value: T)

Sets the value of the signal and notifies listeners.

If the signal has been marked as inactive (via clear_listeners()), this method is a complete no-op: the value is not updated, no listeners are invoked, and no global update is scheduled.

If the new value is equal to the current value, no update is performed and no listeners are notified. This prevents unnecessary re-renders and avoids cascading no-op updates through intermediate signal chains.

§Arguments
  • T - The new value to assign to the signal.
Source

pub fn set_silent(&self, value: T)

Sets the value of the signal and notifies listeners without scheduling a global DOM update dispatch.

This is identical to set except it does not call schedule_signal_update(), meaning no __euv_signal_update__ event will be dispatched. Use this for internal bookkeeping signals whose changes should not trigger DynamicNode re-renders.

If the new value is equal to the current value, no update is performed and no listeners are notified.

If the signal has been marked as inactive (via clear_listeners()), this method is a complete no-op.

§Arguments
  • T - The new value to assign to the signal.
Source

pub fn try_set(&self, value: T) -> bool

Attempts to set the value of the signal and notify listeners without panicking.

If the new value is equal to the current value, no update is performed.

§Arguments
  • T - The new value to assign to the signal.
§Returns
  • bool - true if the value was successfully updated and listeners were notified, false if unchanged or inactive.

Trait Implementations§

Source§

impl<T> AsNode for Signal<T>
where T: Clone + PartialEq + Display + 'static,

Converts a signal into a reactive text virtual node.

Source§

fn as_node(&self) -> Option<VirtualNode>

Converts this signal into a reactive text virtual node.

§Returns
  • Option<VirtualNode> - Always Some with a reactive text node.
Source§

impl<T> AsReactiveText for Signal<T>
where T: Clone + PartialEq + Display + 'static,

Converts a signal into a reactive text node with listener wiring.

Source§

fn as_reactive_text(&self) -> VirtualNode

Creates a reactive text node that auto-updates when the signal changes.

Internally creates a bridge Signal<String> that subscribes to the source signal and updates the text content on every change.

§Returns
  • VirtualNode - A text virtual node with reactive signal binding.
Source§

impl<T> Clone for Signal<T>
where T: Clone + PartialEq,

Clones the signal, sharing the same inner state.

Source§

fn clone(&self) -> Self

Returns a bitwise copy of this signal.

1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T> Deref for Signal<T>
where T: Clone + PartialEq,

Prevents direct dereference of a signal to enforce explicit API usage.

Source§

fn deref(&self) -> &Self::Target

Panics with a message directing the caller to use .get() instead.

Source§

type Target = T

The resulting type after dereferencing.
Source§

impl<T> DerefMut for Signal<T>
where T: Clone + PartialEq,

Prevents direct mutable dereference of a signal to enforce explicit API usage.

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Panics with a message directing the caller to use .set() instead.

Source§

impl<T> IntoNode for Signal<T>
where T: Clone + PartialEq + Display + 'static,

Converts a signal into a reactive text virtual node via IntoNode.

Source§

fn into_node(self) -> VirtualNode

Converts this signal into a reactive text virtual node.

§Returns
  • VirtualNode - A reactive text virtual node.
Source§

impl IntoReactiveString for Signal<String>

Converts a string signal into a reactive string by resolving its current value.

Source§

fn into_reactive_string(self) -> String

Resolves the current value of this signal.

§Returns
  • String - The current value of the signal.
Source§

impl IntoReactiveString for Signal<bool>

Converts a bool signal into a reactive string by resolving its current value.

Source§

fn into_reactive_string(self) -> String

Resolves the current value of this bool signal as a string.

§Returns
  • String - The current boolean value as "true" or "false".
Source§

impl IntoReactiveValue for Signal<String>

Converts a string signal into a reactive attribute value.

Source§

fn into_reactive_value(self) -> AttributeValue

Converts this string signal into an AttributeValue::Signal.

§Returns
  • AttributeValue - A signal-backed attribute value.
Source§

impl IntoReactiveValue for Signal<bool>

Converts a mutable bool signal into a reactive attribute value.

The signal is mapped to a Signal<String> that yields "true" or "false", enabling boolean attributes like checked to reactively update the DOM.

Source§

fn into_reactive_value(self) -> AttributeValue

Converts this bool signal into an AttributeValue via string mapping.

§Returns
  • AttributeValue - A signal-backed attribute value yielding "true" or "false".
Source§

impl<T> Copy for Signal<T>
where T: Clone + PartialEq,

Copies the signal, sharing the same inner state.

A Signal is just a raw pointer; copying it is a trivial bitwise copy.

Auto Trait Implementations§

§

impl<T> Freeze for Signal<T>

§

impl<T> !RefUnwindSafe for Signal<T>

§

impl<T> !Send for Signal<T>

§

impl<T> !Sync for Signal<T>

§

impl<T> Unpin for Signal<T>

§

impl<T> UnsafeUnpin for Signal<T>

§

impl<T> !UnwindSafe for Signal<T>

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<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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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 = 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 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<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more