mirror of
https://github.com/zaphar/sycamore-state.git
synced 2025-07-21 20:19:49 -04:00
Improve the type safety and ergonomics of bind_event.
* Add the triggering signal as a parameter. This ensures that We always call get on the signal to trigger the effect. * Rename the method to bind_trigger to better reflect what it is doing.
This commit is contained in:
parent
2349517a96
commit
3b220477d7
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
/target
|
||||
/Cargo.lock
|
||||
.jj/
|
14
src/lib.rs
14
src/lib.rs
@ -11,7 +11,7 @@
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
use std::marker::PhantomData;
|
||||
use std::{marker::PhantomData, rc::Rc};
|
||||
|
||||
use sycamore::prelude::*;
|
||||
|
||||
@ -53,11 +53,15 @@ where
|
||||
self.signal
|
||||
}
|
||||
|
||||
pub fn bind_event<F>(&'ctx self, cx: Scope<'ctx>, message_fn: F)
|
||||
where
|
||||
F: Fn() -> Msg + 'ctx,
|
||||
pub fn bind_trigger<F, Val>(
|
||||
&'ctx self,
|
||||
cx: Scope<'ctx>,
|
||||
trigger: &'ctx ReadSignal<Val>,
|
||||
message_fn: F,
|
||||
) where
|
||||
F: Fn(Rc<Val>) -> Msg + 'ctx,
|
||||
{
|
||||
create_effect(cx, move || self.dispatch(message_fn()));
|
||||
create_effect(cx, move || self.dispatch(message_fn(trigger.get())));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -65,7 +65,7 @@ fn test_state_effect_flow() {
|
||||
create_child_scope(cx, |cx| {
|
||||
let form_val = create_signal(cx, handler.read_signal().get_untracked().value_one.clone());
|
||||
|
||||
handler.bind_event(cx, || Msg::UpdateOne((*form_val.get()).clone()));
|
||||
handler.bind_trigger(cx, form_val, |val| Msg::UpdateOne((*val).clone()));
|
||||
|
||||
form_val.set("bar".to_owned());
|
||||
|
||||
@ -74,7 +74,7 @@ fn test_state_effect_flow() {
|
||||
create_child_scope(cx, |cx| {
|
||||
let form_val = create_signal(cx, 0);
|
||||
|
||||
handler.bind_event(cx, || Msg::UpdateTwo(*form_val.get()));
|
||||
handler.bind_trigger(cx, form_val, |val| Msg::UpdateTwo(*val));
|
||||
form_val.set(1);
|
||||
assert_eq!(handler.read_signal().get_untracked().value_two, 1);
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user