mirror of
https://github.com/zaphar/sycamore-state.git
synced 2025-07-22 20:29:50 -04:00
Add rust doc to the crate
This commit is contained in:
parent
f939f52a2b
commit
cad0b2ecb7
15
src/lib.rs
15
src/lib.rs
@ -15,10 +15,16 @@ use std::{marker::PhantomData, rc::Rc};
|
||||
|
||||
use sycamore::prelude::*;
|
||||
|
||||
/// Trait that maps a message and an original state value to a new value.
|
||||
/// Implementors of this trait can implement all of their state management
|
||||
/// logic in one place.
|
||||
pub trait MessageMapper<Msg, Out> {
|
||||
fn map(&self, msg: Msg, original: &ReadSignal<Out>) -> Out;
|
||||
}
|
||||
|
||||
/// Provides the necessary wiring for a centralized state handling
|
||||
/// mechanism. The API guides you along handling the lifetimes properly
|
||||
/// as well as registering all the state management logic in one place.
|
||||
pub struct Handler<'ctx, D, T, Msg>
|
||||
where
|
||||
D: MessageMapper<Msg, T>,
|
||||
@ -32,6 +38,9 @@ impl<'ctx, D, T, Msg> Handler<'ctx, D, T, Msg>
|
||||
where
|
||||
D: MessageMapper<Msg, T>,
|
||||
{
|
||||
/// Constructs a new Handler with a lifetime anchored to the provided Scope.
|
||||
/// You will usually construct this in your top level scope and then
|
||||
/// pass the handlers down into your components.
|
||||
pub fn new(cx: Scope<'ctx>, initial: T, dispatcher: D) -> &'ctx Self {
|
||||
let signal = create_signal(cx, initial);
|
||||
let dispatcher = create_ref(cx, dispatcher);
|
||||
@ -49,10 +58,13 @@ where
|
||||
self.signal.set(self.dispatcher.map(msg, self.signal))
|
||||
}
|
||||
|
||||
/// Provides a ReadSignal handle for the contained Signal implementation.
|
||||
pub fn read_signal(&'ctx self) -> &'ctx ReadSignal<T> {
|
||||
self.signal
|
||||
}
|
||||
|
||||
/// Binds a triggering signal and associated message mapping function as
|
||||
/// a state update for this Handler instance.
|
||||
pub fn bind_trigger<F, Val>(
|
||||
&'ctx self,
|
||||
cx: Scope<'ctx>,
|
||||
@ -64,6 +76,9 @@ where
|
||||
create_effect(cx, move || self.dispatch(message_fn(trigger.get())));
|
||||
}
|
||||
|
||||
/// Helper method to get a memoized value derived from the contained
|
||||
/// state for this Handler. The provided handler only notifies subscribers
|
||||
/// If the state has actually been updated.
|
||||
pub fn get_selector<F, Val>(
|
||||
&'ctx self,
|
||||
cx: Scope<'ctx>,
|
||||
|
Loading…
x
Reference in New Issue
Block a user