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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
use ;
use crate::;
use ;
use ;
use ;
/// Per-read settings for [`use_cached_with`] and [`use_cached_key_with`].
type Fetch<T> = ;
/// A cached read's current answer, returned by [`use_cached`].
/// `Copy`, like a signal.
/// Reads a server function through the client cache. **This is the hook to
/// use for data a screen shows when it opens.**
///
/// ```ignore
/// let media = use_cached(get_media, (id.clone(),));
/// let bookmarks = use_cached(get_bookmarks, ());
/// let reviews = use_cached(get_media_reviews, (id.clone(), 0, 21));
///
/// match &*media.read() {
/// Some(Ok(media)) => rsx! { MediaView { media: media.clone() } },
/// Some(Err(_)) => rsx! { ErrorState {} },
/// None => rsx! { Loading {} },
/// }
/// ```
///
/// Pass the function itself and its arguments as a tuple: the pair is the
/// cache key, so the key always matches what is fetched. When the
/// arguments change between renders (new props), it reads the new key.
///
/// A read is answered, in order, from
///
/// 1. memory, for anything fetched this session, then
/// 2. the persistent store, for anything fetched in an earlier session
/// (see [`set_cache_owner`](crate::set_cache_owner)), so a cold start shows the last
/// known data at once, then
/// 3. the server, every time the screen opens, unless the same read
/// finished within [`CacheConfig::share_for`](crate::CacheConfig::share_for). Its
/// answer replaces what was showing.
///
/// A mounted screen refetches after [`invalidate_cached`](crate::invalidate_cached) and
/// when the app regains focus (see [`CacheConfig`](crate::CacheConfig)).
///
/// # When not to use it
///
/// - **Reads that change with every keystroke** (search boxes, pickers):
/// use `use_resource`. Every value is thrown away a moment later.
/// - **Handlers that must see current server state before acting**: call
/// the server function directly. A cache answers with what it last saw.
/// - **Closures.** `use_cached(|| get_media(id), ())` panics: see
/// [`CacheableFn`]. For a read that is not one server function call, use
/// [`use_cached_key`] with a named key.
///
/// On the server (the `server` feature) nothing is cached: every render
/// fetches.
/// [`use_cached`] with per-read [`CacheOptions`].
/// Reads through the client cache under a key you name. Use it for a read
/// that is not one server function call: several calls combined, or data
/// from somewhere else. Prefer [`use_cached`] otherwise.
///
/// `key` must name everything `fetch` depends on. A changed key is what
/// makes it refetch, and two reads with the same key share one entry.
///
/// ```ignore
/// let shelves = use_cached_key(CacheKey::new("home_shelves").with(media_type), move || async move {
/// let (trending, new) = futures::join!(get_trending(media_type), get_new_releases(media_type));
/// Ok(Shelves { trending: trending?, new: new? })
/// });
/// ```
///
/// Invalidate it with [`invalidate_cached_name`](crate::invalidate_cached_name) or
/// [`invalidate_cached_key`](crate::invalidate_cached_key).
/// [`use_cached_key`] with per-read [`CacheOptions`].
/// Sets `data` only when `value` differs, so an unchanged refetch doesn't
/// re-render the screen.