Naming and api ergonomics

This commit is contained in:
Jeremy Wall 2022-12-26 16:37:51 -05:00
parent b282d07983
commit 2349517a96
2 changed files with 19 additions and 19 deletions

View File

@ -15,22 +15,22 @@ use std::marker::PhantomData;
use sycamore::prelude::*; use sycamore::prelude::*;
pub trait Dispatcher<Msg, Out> { pub trait MessageMapper<Msg, Out> {
fn apply(&self, msg: Msg, original: &ReadSignal<Out>) -> Out; fn map(&self, msg: Msg, original: &ReadSignal<Out>) -> Out;
} }
pub struct Reducer<'ctx, D, T, Msg> pub struct Handler<'ctx, D, T, Msg>
where where
D: Dispatcher<Msg, T>, D: MessageMapper<Msg, T>,
{ {
signal: &'ctx Signal<T>, signal: &'ctx Signal<T>,
dispatcher: &'ctx D, dispatcher: &'ctx D,
_phantom: PhantomData<Msg>, _phantom: PhantomData<Msg>,
} }
impl<'ctx, D, T, Msg> Reducer<'ctx, D, T, Msg> impl<'ctx, D, T, Msg> Handler<'ctx, D, T, Msg>
where where
D: Dispatcher<Msg, T>, D: MessageMapper<Msg, T>,
{ {
pub fn new(cx: Scope<'ctx>, initial: T, dispatcher: D) -> &'ctx Self { pub fn new(cx: Scope<'ctx>, initial: T, dispatcher: D) -> &'ctx Self {
let signal = create_signal(cx, initial); let signal = create_signal(cx, initial);
@ -45,19 +45,19 @@ where
) )
} }
pub fn dispatch(&self, msg: Msg) { fn dispatch(&self, msg: Msg) {
self.signal.set(self.dispatcher.apply(msg, self.signal)) self.signal.set(self.dispatcher.map(msg, self.signal))
} }
pub fn signal(&'ctx self) -> &'ctx ReadSignal<T> { pub fn read_signal(&'ctx self) -> &'ctx ReadSignal<T> {
self.signal self.signal
} }
pub fn bind<F>(&'ctx self, cx: Scope<'ctx>, f: F) pub fn bind_event<F>(&'ctx self, cx: Scope<'ctx>, message_fn: F)
where where
F: Fn() -> Msg + 'ctx, F: Fn() -> Msg + 'ctx,
{ {
create_effect(cx, move || self.dispatch(f())); create_effect(cx, move || self.dispatch(message_fn()));
} }
} }

View File

@ -26,8 +26,8 @@ pub struct FakeState {
pub struct StateMachine(); pub struct StateMachine();
impl Dispatcher<Msg, FakeState> for StateMachine { impl MessageMapper<Msg, FakeState> for StateMachine {
fn apply(&self, msg: Msg, original: &ReadSignal<FakeState>) -> FakeState { fn map(&self, msg: Msg, original: &ReadSignal<FakeState>) -> FakeState {
match msg { match msg {
Msg::UpdateOne(val) => { Msg::UpdateOne(val) => {
let mut new_state = original.get().as_ref().clone(); let mut new_state = original.get().as_ref().clone();
@ -60,23 +60,23 @@ fn test_state_effect_flow() {
value_two: 0, value_two: 0,
}; };
let reducer = Reducer::new(cx, state, StateMachine()); let handler = Handler::new(cx, state, StateMachine());
create_child_scope(cx, |cx| { create_child_scope(cx, |cx| {
let form_val = create_signal(cx, reducer.signal().get_untracked().value_one.clone()); let form_val = create_signal(cx, handler.read_signal().get_untracked().value_one.clone());
reducer.bind(cx, || Msg::UpdateOne((*form_val.get()).clone())); handler.bind_event(cx, || Msg::UpdateOne((*form_val.get()).clone()));
form_val.set("bar".to_owned()); form_val.set("bar".to_owned());
assert_eq!(reducer.signal().get_untracked().value_one, "bar".to_owned()); assert_eq!(handler.read_signal().get_untracked().value_one, "bar".to_owned());
create_child_scope(cx, |cx| { create_child_scope(cx, |cx| {
let form_val = create_signal(cx, 0); let form_val = create_signal(cx, 0);
reducer.bind(cx, || Msg::UpdateTwo(*form_val.get())); handler.bind_event(cx, || Msg::UpdateTwo(*form_val.get()));
form_val.set(1); form_val.set(1);
assert_eq!(reducer.signal().get_untracked().value_two, 1); assert_eq!(handler.read_signal().get_untracked().value_two, 1);
}); });
}); });
}; };