2个发布版本
0.0.2 | 2023年5月8日 |
---|---|
0.0.1 | 2023年5月8日 |
#893 in WebAssembly
每月 22次下载
21KB
238 行
状态管理推导宏
概述
sycamore-state 是一个用于利用 sycamore 的响应式原语进行更好的状态管理的实用库
此crate的主要功能是 State
推导宏和 Rc/Ref 集合信号类型
目前,为了生命周期管理,此crate广泛使用 sycamore::reactive::create_signal_unsafe
,如果您认为可能存在不安全的错误,请随时打开一个 issue
当前功能
- 泛型状态支持
- 生命周期支持
计划功能
- 更好的编译错误
- 闭包绑定支持
- 派生状态支持
- 自动上下文提供的宏
使用方法
#[derive(Debug, State, Clone)]
#[state(clone, eq, debug)] // avaliable derive macros are: (clone, debug, eq, ord)
pub struct MyState<'a> {
pub field_1: String,
pub field_2: u32,
#[state]
pub field_3: MyInnerState<'a>,
#[state]
#[collection]
pub state_collection: Vec<MyInnerState<'a>>
}
#[derive(Debug, State, Clone)]
#[state(clone, eq, debug)]
pub struct MyInnerState<'a> {
pub field_1: i64,
#[collection]
pub collection: Vec<&'a str>
}
let ref_state = RefMyState::new(cx, MyState {
field_1: "my_string".into(),
field_2: 5,
field_3: MyInnerState {
field_1: 20,
collection: vec!["my", "string", "collection"],
},
state_collection: Default::default()
});
生成的结构体
pub struct RcMyState<'a> {
pub field_1: RcSignal<String>,
pub field_2: RcSignal<u32>,
pub field_3: RcSignal<RcMyInnerState<'a>>,
pub state_collection: RcCollectionSignal<RcMyInnerState<'a>>,
}
pub struct RcMyInnerState<'a> {
pub field_1: RcSignal<i64>,
pub collection: RcCollectionSignal<&'a str>,
}
pub struct RefMyState<'a, 'stateful> {
pub field_1: &'stateful Signal<String>,
pub field_2: &'stateful Signal<u32>,
pub field_3: &'stateful Signal<RefMyInnerState<'a, 'stateful>>,
pub state_collection: RefCollectionSignal<'stateful, RefMyInnerState<'a, 'stateful>>,
}
pub struct RefMyInnerState<'a, 'stateful> {
pub field_1: &'stateful Signal<i64>,
pub collection: RefCollectionSignal<'stateful, &'a str>,
}
依赖关系
~3.5MB
~62K SLoC