1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//! The Elm Architecture for [`leptos`].
//!
//! This crate is a particular strategy for state management
//! in [`leptos`]. It follows the Elm architecture, but not
//! strictly so, which allows mixing and matching with other state
//! management approaches.
//!
//! First, let's look at an example.
//!
//! # Example
//! ```rust
//! use leptos::*;
//! use leptos_tea::Cmd;
//!
//! #[derive(Default, leptos_tea::Model)]
//! struct CounterModel {
//! counter: usize,
//! }
//!
//! #[derive(Default)]
//! enum Msg {
//! Increment,
//! Decrement,
//! #[default]
//! Init,
//! }
//!
//! fn update(model: UpdateCounterModel, msg: &Msg, _: Cmd<Msg>) {
//! match msg {
//! Msg::Increment => model.counter.update(|c| *c += 1),
//! Msg::Decrement => model.counter.update(|c| *c -= 1),
//! Msg::Init => {}
//! }
//! }
//!
//! #[component]
//! fn Counter(cx: Scope) -> impl IntoView {
//! let (model, msg_dispatcher) = CounterModel::default().init(cx, update);
//!
//! view! { cx,
//! <h1>{model.counter}</h1>
//! <button on:click=move |_| msg_dispatcher(Msg::Decrement)>"-"</button>
//! <button on:click=move |_| msg_dispatcher(Msg::Increment)>"+"</button>
//! }
//! }
//! ```
//!
//! In the above example, we're annotating `CounterModel` with
//! `leptos_tea::Model`, which will derive a few important things:
//!
//! ```rust
//! # use leptos::*;
//! # use leptos_tea::Cmd;
//!
//! // Original struct, stays as-is
//! struct CounterModel {
//! counter: usize,
//! }
//!
//! // Model passed to the update function
//! struct UpdateCounterModel {
//! counter: RwSignal<bool>,
//! }
//!
//! // model passed to the component when you call `.init()`
//! struct ViewCounterModel {
//! counter: ReadSignal<bool>,
//! }
//!
//! impl CounterModel {
//! // Initializes everything and starts listening for messages.
//! // Msg::default() will be send to the update function when
//! // called
//! fn init<Msg: Default + 'static>(
//! self,
//! cx: Scope,
//! update_fn: impl Fn(UpdateCounterModel, &Msg, Cmd<Msg>),
//! ) -> (ViewCounterModel, SignalSetter<Msg>) {
//! /* ... */
//! # todo!()
//! }
//! }
//! ```
//!
//! You first need to create your `CounterModel`, however you'd like.
//! In this case, we're using `Default`. Then you call `.init()`,
//! which will return a tuple containing the read-only model, as well
//! as a `SignalSetter`, which allows you to do `msg_dispatcher(Msg::Blah)`
//! on nightly, or `msg_dispatcher.set(Msg::Blah)` on stable.
//!
//! And that's how this crate and state management approach works.
//!
//! # Model nesting
//!
//! Models can be nested inside one another like thus:
//!
//! ```rust
//! #[derive(leptos_tea::Model)]
//! struct Model {
//! #[model]
//! inner_model: InnerModel,
//! }
//!
//! #[derive(leptos_tea::Model)]
//! struct InnerModel(/* ... */);
//! ```
//!
//! **Important Node**: Although this _can_ be done, it is not
//! recommended, because it leads to nested `.update()`/`.with`
//! calls for each level of nesting. Instead, try and break out each
//! nested model into it's own independent model, view, update. Nevertheless,
//! sometimes this isn't desired or worth it, so the option is there in case
//! you need it.
//!
//! # Limitations
//!
//! `leptos_tea::Model` currently only supports tuple and field structs.
//! Support will be added soon.
use FutureExt;
use *;
pub use *;
use SmallVec;
use ;
type CmdFut<Msg> = ;
/// Command manager that allows dispatching messages and running
/// asynchronous operations.