AsyncReducer

Struct AsyncReducer 

Source
pub struct AsyncReducer<Data: AsyncReducible> { /* private fields */ }
Expand description

A type that allows you to share information across your application easily with async operations.

Async reducers receive messages, update their state asynchronously, and notify their subscribers.

Unlike SharedState, this type doesn’t allow direct access to the internal data. Instead, it updates its state after receiving messages, similar to components. After the message is processed, all subscribers will be notified.

This is the async variant of Reducer, where the reduce method can perform asynchronous operations.

§Example

use relm4::{AsyncReducer, AsyncReducible};

struct CounterReducer(u8);

enum CounterInput {
    Increment,
    Decrement,
}

impl AsyncReducible for CounterReducer {
    type Input = CounterInput;

    async fn init() -> Self {
        Self(0)
    }

    async fn reduce(&mut self, input: Self::Input) -> bool {
        match input {
            CounterInput::Increment => {
                self.0 += 1;
            }
            CounterInput::Decrement =>  {
                self.0 -= 1;
            }
        }
        true
    }
}

// Create the reducer.
static REDUCER: AsyncReducer<CounterReducer> = AsyncReducer::new();

// Update the reducer.
REDUCER.emit(CounterInput::Increment);

// Create a channel and subscribe to changes.
let (sender, receiver) = relm4::channel();
REDUCER.subscribe(&sender, |data| data.0);

// Count up to 2.
REDUCER.emit(CounterInput::Increment);
assert_eq!(receiver.recv_sync().unwrap(), 2);

Implementations§

Source§

impl<Data> AsyncReducer<Data>
where Data: AsyncReducible + 'static,

Source

pub const fn new() -> Self

Create a new AsyncReducer variable.

The data will be initialized lazily on the first access.

Source

pub fn subscribe<Msg, F>(&self, sender: &Sender<Msg>, f: F)
where F: Fn(&Data) -> Msg + 'static + Send + Sync, Msg: Send + 'static,

Subscribe to an AsyncReducer. Any subscriber will be notified with a message every time you modify the reducer (by calling Self::emit()).

Source

pub fn subscribe_optional<Msg, F>(&self, sender: &Sender<Msg>, f: F)
where F: Fn(&Data) -> Option<Msg> + 'static + Send + Sync, Msg: Send + 'static,

An alternative version of subscribe() that only send a message if the closure returns Some.

Source

pub fn emit(&self, input: Data::Input)

Sends a message to the reducer to update its state asynchronously.

If the AsyncReducible::reduce() method returns true, all subscribers will be notified.

Trait Implementations§

Source§

impl<Data: Debug + AsyncReducible> Debug for AsyncReducer<Data>

Source§

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

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

impl<Data> Default for AsyncReducer<Data>
where Data: AsyncReducible + 'static,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<Data> !Freeze for AsyncReducer<Data>

§

impl<Data> RefUnwindSafe for AsyncReducer<Data>

§

impl<Data> Send for AsyncReducer<Data>

§

impl<Data> Sync for AsyncReducer<Data>

§

impl<Data> Unpin for AsyncReducer<Data>

§

impl<Data> UnwindSafe for AsyncReducer<Data>

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<C> AsyncPosition<()> for C

Source§

fn position(_index: usize)

Returns the position. 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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<C, I> Position<(), I> for C

Source§

fn position(&self, _index: &I)

Returns the position. 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<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