Skip to main content

Signal

Struct Signal 

Source
pub struct Signal<T> { /* private fields */ }
Expand description

A reactive signal that notifies listeners when its value changes.

§Bridging signals to the UI thread (async feature)

Signal is UI-thread-only (Rc internals, not Send) and listeners run synchronously inside Signal::set/Signal::update. To drive widgets from a signal, bridge the listener into the bounded UI command queue instead of touching widgets directly:

  1. Create a UiProxy with Ui::task_proxy before wiring the listener.
  2. Subscribe once on the UI thread; the listener converts the new value into proxy posts (set_text, set_value, set_checked, set_enabled, set_visible, progress). A UiProxy is Send and bounded, so the same wiring works when the signal is updated on the UI thread and the commands are applied on the next event-loop turn.
  3. Drain the queue from the event loop: Ui::wait_event, Ui::poll_event, Ui::dispatch_events, and run_until_closed apply posted commands in FIFO order on the UI thread before returning backend events.

Posts that arrive after teardown, or when the bounded queue is full, report TaskSendError instead of blocking or panicking, so a listener can degrade gracefully. Background threads cannot hold a Signal: send raw values through the proxy (or a channel) and update the signal on the UI thread, where its listeners run.

use native_gui_mock::MockBackend;
use native_gui_core::state::Signal;
use native_gui_core::{Ui, WidgetKind};

let mut ui = Ui::new(MockBackend::default());
let label = ui.create(WidgetKind::Label, Some("0"))?;
let proxy = ui.task_proxy();

let counter = Signal::new(0i32);
counter.subscribe(move |value| {
    let _ = proxy.set_text(label, value.to_string());
});

counter.set(1);
// The queue drains on the next event-loop turn (here: a dispatch pass).
ui.dispatch_events(|_| {})?;
assert_eq!(
    ui.widget(label).and_then(|spec| spec.text.clone()),
    Some("1".to_owned())
);

Implementations§

Source§

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

Source

pub fn new(initial: T) -> Self

Creates a new reactive signal with an initial value.

Source

pub fn get(&self) -> T

Gets the current value of the signal.

Source

pub fn set(&self, new_value: T)

Sets a new value for the signal, notifying listeners if the value changed.

Source

pub fn update(&self, mutate: impl FnOnce(&mut T))

Mutates the value in place and notifies listeners once if it changed.

This is the main entry point for the observable store: views subscribe with Signal::subscribe and rebuild the affected widgets when the value they depend on changes.

Source

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

Registers a callback to be invoked when the value changes.

Trait Implementations§

Source§

impl<T: Clone> Clone for Signal<T>

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

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Signal<T>

§

impl<T> !Send for Signal<T>

§

impl<T> !Sync for Signal<T>

§

impl<T> !UnwindSafe for Signal<T>

§

impl<T> Forget for Signal<T>

§

impl<T> Freeze for Signal<T>
where *const RcInner<RefCell<SignalInner<T>>> is !null: Freeze, PhantomData<RcInner<RefCell<SignalInner<T>>>>: Freeze,

§

impl<T> Move for Signal<T>

§

impl<T> Unpin for Signal<T>
where Rc<RefCell<SignalInner<T>>>: Unpin,

§

impl<T> UnsafeUnpin for Signal<T>
where *const RcInner<RefCell<SignalInner<T>>> is !null: UnsafeUnpin, PhantomData<RcInner<RefCell<SignalInner<T>>>>: UnsafeUnpin,

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