Effect

Struct Effect 

Source
pub struct Effect<Event: EventTrait>(/* private fields */);
Expand description

Declarative description of events to be processed.

Effects allow you to describe asynchronous or deferred work that will produce events. They are returned from MvuLogic::init and MvuLogic::update with the new model state.

§Example

use oxide_mvu::Effect;

#[derive(Clone)]
enum Event {
    LoadData,
    DataLoaded(String),
}

// Trigger a follow-up event
let effect = Effect::just(Event::LoadData);

// Combine multiple effects
let effect = Effect::batch(vec![
    Effect::just(Event::LoadData),
    Effect::just(Event::DataLoaded("cached".to_string())),
]);

// No side effects
let effect: Effect<Event> = Effect::none();

Implementations§

Source§

impl<Event: EventTrait> Effect<Event>

Source

pub fn execute( self, emitter: &Emitter<Event>, ) -> Pin<Box<dyn Future<Output = ()> + Send>>

Execute the effect, consuming it and returning a future.

The returned future will be spawned on your async runtime using the provided spawner.

Source

pub fn just(event: Event) -> Self

Create an effect that just emits a single event.

Useful for triggering immediate follow-up events.

§Example
use oxide_mvu::Effect;

#[derive(Clone)]
enum Event { Refresh }

let effect = Effect::just(Event::Refresh);
Source

pub fn none() -> Self

Create an empty effect.

Prefer this when semantically indicating “no side effects”.

§Example
use oxide_mvu::Effect;

#[derive(Clone)]
enum Event { Increment }

let effect: Effect<Event> = Effect::none();
Source

pub fn batch(effects: Vec<Effect<Event>>) -> Self

Combine multiple effects into a single effect.

All events from all effects will be queued for processing.

§Example
use oxide_mvu::Effect;

#[derive(Clone)]
enum Event { A, B, C }

let combined = Effect::batch(vec![
    Effect::just(Event::A),
    Effect::just(Event::B),
    Effect::just(Event::C),
]);
Source

pub fn from_async<F, Fut>(f: F) -> Self
where F: FnOnce(Emitter<Event>) -> Fut + Send + 'static, Fut: Future<Output = ()> + Send + 'static,

Create an effect from an async function using a runtime-agnostic spawner.

This allows you to use async/await syntax with any async runtime (tokio, async-std, smol, etc.) by providing a spawner function that knows how to execute futures on your chosen runtime.

The async function receives a cloned Emitter that can be used to emit events when the async work completes.

§Arguments
  • spawner - A function that spawns the future on your async runtime
  • f - An async function that receives an Emitter and returns a Future
§Example with tokio
use oxide_mvu::Effect;
use std::time::Duration;

#[derive(Clone)]
enum Event {
    FetchData,
    DataLoaded(String),
    DataFailed(String),
}

async fn fetch_from_api() -> Result<String, String> {
    // Await some async operation...
    Ok("data from API".to_string())
}

let effect = Effect::from_async(
    |emitter| async move {
        match fetch_from_api().await {
            Ok(data) => emitter.emit(Event::DataLoaded(data)).await,
            Err(err) => emitter.emit(Event::DataFailed(err)).await,
        }
    }
);
§Example with async-std
use oxide_mvu::Effect;

#[derive(Clone)]
enum Event { TimerAlert }

let await_timer_effect = Effect::from_async(
    |emitter| async move {
        // Await timer
        emitter.emit(Event::TimerAlert).await;
    }
);

Auto Trait Implementations§

§

impl<Event> Freeze for Effect<Event>

§

impl<Event> !RefUnwindSafe for Effect<Event>

§

impl<Event> Send for Effect<Event>

§

impl<Event> !Sync for Effect<Event>

§

impl<Event> Unpin for Effect<Event>

§

impl<Event> !UnwindSafe for Effect<Event>

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