2022-12-26 16:15:39 -05:00
|
|
|
// Copyright 2022 Jeremy Wall (Jeremy@marzhilsltudios.com)
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// 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 super::*;
|
|
|
|
|
|
|
|
pub enum Msg {
|
|
|
|
UpdateOne(String),
|
|
|
|
UpdateTwo(i32),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq)]
|
|
|
|
pub struct FakeState {
|
|
|
|
value_one: String,
|
|
|
|
value_two: i32,
|
|
|
|
}
|
|
|
|
|
2022-12-26 17:24:14 -05:00
|
|
|
pub enum MultiMsg {
|
|
|
|
Foo(Msg),
|
|
|
|
Bar(Msg),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MultiState<'ctx, D>
|
|
|
|
where
|
|
|
|
D: MessageMapper<Msg, FakeState>,
|
|
|
|
{
|
|
|
|
foo: &'ctx Handler<'ctx, D, FakeState, Msg>,
|
|
|
|
bar: &'ctx Handler<'ctx, D, FakeState, Msg>,
|
|
|
|
}
|
|
|
|
|
2022-12-26 16:15:39 -05:00
|
|
|
pub struct StateMachine();
|
|
|
|
|
2022-12-26 16:37:51 -05:00
|
|
|
impl MessageMapper<Msg, FakeState> for StateMachine {
|
|
|
|
fn map(&self, msg: Msg, original: &ReadSignal<FakeState>) -> FakeState {
|
2022-12-26 16:15:39 -05:00
|
|
|
match msg {
|
|
|
|
Msg::UpdateOne(val) => {
|
|
|
|
let mut new_state = original.get().as_ref().clone();
|
|
|
|
new_state.value_one = val;
|
|
|
|
new_state
|
|
|
|
}
|
|
|
|
Msg::UpdateTwo(val) => {
|
|
|
|
let mut new_state = original.get().as_ref().clone();
|
|
|
|
new_state.value_two = val;
|
|
|
|
new_state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! with_scope {
|
|
|
|
($cx:ident, $( $body:tt )* ) => {{
|
|
|
|
use sycamore::prelude::create_scope_immediate;
|
|
|
|
create_scope_immediate(|$cx| {
|
|
|
|
$( $body )*
|
|
|
|
});
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_state_effect_flow() {
|
|
|
|
with_scope! {cx,
|
|
|
|
let state = FakeState {
|
|
|
|
value_one: "foo".to_owned(),
|
|
|
|
value_two: 0,
|
|
|
|
};
|
|
|
|
|
2022-12-26 16:37:51 -05:00
|
|
|
let handler = Handler::new(cx, state, StateMachine());
|
2022-12-26 16:15:39 -05:00
|
|
|
|
|
|
|
create_child_scope(cx, |cx| {
|
2022-12-26 16:37:51 -05:00
|
|
|
let form_val = create_signal(cx, handler.read_signal().get_untracked().value_one.clone());
|
2022-12-26 16:15:39 -05:00
|
|
|
|
2022-12-26 16:49:25 -05:00
|
|
|
handler.bind_trigger(cx, form_val, |val| Msg::UpdateOne((*val).clone()));
|
2022-12-26 16:15:39 -05:00
|
|
|
|
|
|
|
form_val.set("bar".to_owned());
|
|
|
|
|
2022-12-26 16:37:51 -05:00
|
|
|
assert_eq!(handler.read_signal().get_untracked().value_one, "bar".to_owned());
|
2022-12-26 16:15:39 -05:00
|
|
|
|
|
|
|
create_child_scope(cx, |cx| {
|
|
|
|
let form_val = create_signal(cx, 0);
|
|
|
|
|
2022-12-26 16:49:25 -05:00
|
|
|
handler.bind_trigger(cx, form_val, |val| Msg::UpdateTwo(*val));
|
2022-12-26 16:15:39 -05:00
|
|
|
form_val.set(1);
|
2022-12-26 16:37:51 -05:00
|
|
|
assert_eq!(handler.read_signal().get_untracked().value_two, 1);
|
2022-12-26 16:15:39 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|