Skip to content

Commit

Permalink
Action handler can send another action
Browse files Browse the repository at this point in the history
  • Loading branch information
wtholliday committed Jun 9, 2023
1 parent f08684c commit c4d0331
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/modifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ pub trait Modifiers: View + Sized {
}

/// Handle an action from a child view.
fn handle<A: 'static, F: Fn(&mut Context, &A) + 'static>(
fn handle<A: 'static, A2: 'static, F: Fn(&mut Context, &A) -> A2 + 'static>(
self,
handler: F,
) -> Handle<Self, F, A> {
) -> Handle<Self, F, A, A2> {
Handle::new(self, handler)
}

Expand Down
17 changes: 10 additions & 7 deletions src/views/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,34 @@ use crate::*;
use std::any::Any;

/// Struct for an action handler.
pub struct Handle<V, F, A> {
pub struct Handle<V, F, A, A2> {
child: V,
func: F,
phantom_action: std::marker::PhantomData<A>,
phantom_action2: std::marker::PhantomData<A2>,
}

impl<V, F, A> Handle<V, F, A>
impl<V, F, A, A2> Handle<V, F, A, A2>
where
V: View,
F: Fn(&mut Context, &A) + 'static,
F: Fn(&mut Context, &A) -> A2 + 'static,
{
pub fn new(v: V, f: F) -> Self {
Self {
child: v,
func: f,
phantom_action: Default::default(),
phantom_action2: Default::default(),
}
}
}

impl<V, F, A> View for Handle<V, F, A>
impl<V, F, A, A2> View for Handle<V, F, A, A2>
where
V: View,
F: Fn(&mut Context, &A) + 'static,
F: Fn(&mut Context, &A) -> A2 + 'static,
A: 'static,
A2: 'static,
{
fn process(
&self,
Expand All @@ -41,7 +44,7 @@ where

for action in child_actions {
if let Some(a) = action.downcast_ref::<A>() {
(self.func)(cx, a);
actions.push(Box::new((self.func)(cx, a)));
} else {
actions.push(action);
}
Expand Down Expand Up @@ -78,4 +81,4 @@ where
}
}

impl<V, F, A> private::Sealed for Handle<V, F, A> {}
impl<V, F, A, A2> private::Sealed for Handle<V, F, A, A2> {}

0 comments on commit c4d0331

Please sign in to comment.