2 个不稳定版本
1.1.1 |
|
---|---|
1.0.1 |
|
0.3.0 | 2023 年 1 月 14 日 |
0.2.4 | 2022 年 9 月 24 日 |
#1025 在 Rust 模式
80KB
1.5K SLoC
Dioxus Shareables
dioxus-shareables
为 dioxus
组件之间共享结构添加了钩子。0.2.x 版本提供了三个接口
-
shareable!()
宏创建一个单个共享值。use dioxus::prelude::*; use dioxus_shareables::shareable; shareable!(Var: usize = 900); #[allow(non_snake_case)] pub fn Reader(cx: Scope) -> Element { let r = *Var.use_rw(&cx).read(); // this component will update when Var changes. // ... } #[allow(non_snake_case)] pub fn Writer(cx: Scope) -> Element { let w1 = Var.use_w(&cx); // this component writes to Var, but does not get updated when Var // changes // ... }
-
List
提供共享值数组。使用List<T>
而不是Vec<T>
允许仅使用一个或两个列表项的组件在它们使用的特定列表项更改时才进行更新。use dioxus::prelude::*; use dioxus_shareables::{shareable, List, ListEntry}; shareable!(Numbers: List<usize> = [3, 5, 7].into_iter().collect()); #[allow(non_snake_case)] fn IterateOverNumbers(cx: Scope) -> Element { let nums = Numbers.use_rw(&cx); // This component is updated when new items are added to or // removed from the list, but not when the individual list // items change. let w = nums.clone(); cx.render(rsx! { ul { nums.read().iter().map(|n| rsx! { ReadANumber { num: n } }) } }) } #[allow(non_snake_case)] #[inline_props] fn ReadANumber(cx: Scope, num: ListEntry<usize>) -> Element { let num = num.use_rw(&cx); // This component is updated when this specific entry in the // list is modified, but not when the others are. ... }
List
内部是一个Vec
,因此它实现的方法名称和行为来自Vec
。 -
shareable_struct!{}
宏提供具有封装不同行为的接口的共享struct
。想法是每个结构字段将存储在单独的全局中,并且仅在请求时加载。动作块描述了使用结构的方式,即它们需要哪种类型的访问(W
或RW
)到结构的字段。然后可以使用“动作”初始化结构,该动作描述了我们需要哪种类型的访问权限的字段。use dioxus::prelude::*; dioxus_shareables::shareable_struct! { pub struct Fuzzy { wuzzy: u8 = 17, was_a: u16 = 59, was_he: &'static str = "bear?", } action WAS impl pub WasTrait = W[was_a, was_he]; // declares a WAS action constant, as well an // equivalent trait. action INIT = W[wuzzy, was_a] RW[was_he]; // declares the INIT constant, but no // equivalent trait. } impl<A: FuzzyActions> Fuzzy<A> { pub fn method(&self) where A: WasTrait { let me = self.with_actions(WAS); // Pending updates to the rust trait system, we // have to typecast here to get a Fuzzy<WAS>. *me.was_he().write() = "bare!"; // We have access to was_he // self.wuzzy(); // but this would fail because we don't have access to wuzzy. // ... } } // ... fn component(cx: Scope) -> Element { let fuzzy = Fuzzy::use_(&cx, INIT); // This creates the hooks for the struct and initializes it // from the necessary globals. // ... fuzzy.method(); // This is ok, since the INIT action includes everything the WAS action does. // ... cx.render(rsx!{div{}}) }
关于版本号的说明。
如果您正在查看Crates.io上的版本,可能会注意到第一个发布版本是1.0。故事的简单版本是我刚开始与他人分享我的代码,并没有考虑到语义化版本控制(semver)。从那时起,我已经撤回了所有发布版本,以同步这个crate的版本号与最新的dioxus预发布版本。
依赖项
~2.4–8MB
~52K SLoC