HandlerRegistry

Struct HandlerRegistry 

Source
pub struct HandlerRegistry { /* private fields */ }
Expand description

Registry of event handlers

Implementations§

Source§

impl HandlerRegistry

Source

pub fn new() -> Self

Create a new empty handler registry

Source

pub fn register_simple<F>(&self, name: &str, handler: F)
where F: Fn(&mut dyn Any) + Send + Sync + 'static,

Register a simple handler

Source

pub fn register_with_value<F>(&self, name: &str, handler: F)
where F: Fn(&mut dyn Any, Box<dyn Any>) + Send + Sync + 'static,

Register a handler with a value parameter

Source

pub fn register_with_command<F>(&self, name: &str, handler: F)
where F: Fn(&mut dyn Any) -> Box<dyn Any> + Send + Sync + 'static,

Register a handler that returns a command

Source

pub fn get(&self, name: &str) -> Option<HandlerEntry>

Look up a handler by name

Source

pub fn dispatch( &self, handler_name: &str, model: &mut dyn Any, value: Option<String>, )

Dispatches a handler by name, executing it with the provided model and optional value.

This is a convenience method that combines get() and handler invocation.

§Arguments
  • handler_name - Name of the handler to dispatch
  • model - Mutable reference to the model (as &mut dyn Any)
  • value - Optional string value passed to WithValue handlers
§Example
use dampen_core::HandlerRegistry;

let registry = HandlerRegistry::new();
registry.register_simple("greet", |model| {
    let model = model.downcast_mut::<MyModel>().unwrap();
    model.count += 1;
});

let model = &mut MyModel { count: 0 } as &mut dyn std::any::Any;
registry.dispatch("greet", model, None);
Source

pub fn dispatch_with_command( &self, handler_name: &str, model: &mut dyn Any, value: Option<String>, ) -> Option<Box<dyn Any>>

Dispatches a handler by name and returns any command/task it produces.

This method is similar to dispatch() but returns the command/task from WithCommand handlers instead of discarding it. This is essential for integrating with the Elm/MVU pattern where handlers can return tasks.

§Arguments
  • handler_name - Name of the handler to dispatch
  • model - Mutable reference to the model (as &mut dyn Any)
  • value - Optional string value passed to WithValue handlers
§Returns
  • Some(Box<dyn Any>) - The command/task from a WithCommand handler
  • None - For Simple and WithValue handlers, or if handler not found
§Example
use dampen_core::HandlerRegistry;
use iced::Task;

let registry = HandlerRegistry::new();
registry.register_with_command("navigate", |model| {
    let model = model.downcast_mut::<MyModel>().unwrap();
    Box::new(Task::done(Message::SwitchView))
});

let model = &mut MyModel::default() as &mut dyn std::any::Any;
if let Some(boxed_task) = registry.dispatch_with_command("navigate", model, None) {
    if let Ok(task) = boxed_task.downcast::<Task<Message>>() {
        return *task;
    }
}
Source

pub fn contains(&self, name: &str) -> bool

Check if a handler exists

Trait Implementations§

Source§

impl Clone for HandlerRegistry

Source§

fn clone(&self) -> HandlerRegistry

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for HandlerRegistry

Source§

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

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

impl Default for HandlerRegistry

Source§

fn default() -> Self

Returns the “default value” for a type. 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<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 = 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.