yew-api-hook 0.3.0

Use asynchronous api requests in conjunction with yew's suspense feature
Documentation
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#[cfg(feature = "cache")]
use crate::CachableRequest;
use crate::Request;

use yew::prelude::*;
use yew::suspense::SuspensionResult;
#[cfg(feature = "cache")]
use yewdux::prelude::use_store_value;

/// Use API Options
///
/// You may specify dependencies which force the request to be reevaluated
/// and a handler which is called every time a request is ran
#[derive(Clone, Debug)]
pub struct Options<R, D>
where
    R: Request + 'static,
    D: Clone + PartialEq + 'static,
{
    pub deps: Option<D>,
    pub handler: Option<Callback<Result<R::Output, R::Error>, ()>>,
}

impl<R, D> Default for Options<R, D>
where
    R: Request + 'static,
    D: Clone + PartialEq + 'static,
{
    fn default() -> Self {
        Self {
            deps: None,
            handler: None,
        }
    }
}

/// The basic api hook which requests data on mount and preserves its
/// data through out the component lifetime
#[hook]
pub fn use_api<R: Request + 'static>(request: R) -> SuspensionResult<Result<R::Output, R::Error>> {
    use_api_with_options::<R, ()>(request, Default::default())
}

/// The basic api hook which requests data on mount and preserves its
/// data through out the component lifetime.
///
/// Reruns the request once the dependencies update
#[hook]
pub fn use_api_with_options<R: Request + 'static, D: Clone + PartialEq + 'static>(
    request: R,
    options: Options<R, D>,
) -> SuspensionResult<Result<R::Output, R::Error>> {
    let deps = (request, options.deps);

    let result = inner::use_future_with_deps(
        |deps| async move {
            let result = deps.0.run().await;

            if let Some(ref handler) = options.handler {
                handler.emit(result.to_owned());
            }

            if let Ok(ref data) = result {
                R::store(data.to_owned());
            }

            result
        },
        deps,
    )?;

    Ok((*result).to_owned())
}

/// A lazy api response which you can trigger through the `run` callback
pub struct LazyResponse<R: Request + 'static> {
    pub run: Callback<(), ()>,
    pub data: Option<SuspensionResult<Result<R::Output, R::Error>>>,
}

/// Useful when not wanting to run a request on mount, e.g. for a logout button
/// You may run the request multiple times through multiple emits of the callback
#[hook]
pub fn use_api_lazy<R: Request + 'static>(request: R) -> LazyResponse<R> {
    use_api_lazy_with_options::<R, ()>(request, Default::default())
}

/// Useful when not wanting to run a request on mount, e.g. for a logout button
/// You may run the request multiple times through multiple emits of the callback
#[hook]
pub fn use_api_lazy_with_options<R: Request + 'static, D: Clone + PartialEq + 'static>(
    request: R,
    options: Options<R, D>,
) -> LazyResponse<R> {
    let DynLazyResponse { run, data } = use_api_dynamic_with_options::<R, D>(options);

    let run = Callback::from(move |_| {
        run.emit(request.clone());
    });

    LazyResponse { run, data }
}

pub struct DynLazyResponse<R: Request + 'static> {
    pub run: Callback<R, ()>,
    pub data: Option<SuspensionResult<Result<R::Output, R::Error>>>,
}

/// Useful when not wanting to run a request on mount, e.g. for a logout button
/// You may run the request multiple times through multiple emits of the callback
///
/// By using the dynamic hook you can build the request with its parameters at runtime
#[hook]
pub fn use_api_dynamic<R: Request + 'static>() -> DynLazyResponse<R> {
    use_api_dynamic_with_options::<R, ()>(Default::default())
}

/// Useful when not wanting to run a request on mount, e.g. for a logout button
/// You may run the request multiple times through multiple emits of the callback
///
/// By using the dynamic hook you can build the request with its parameters at runtime
#[hook]
pub fn use_api_dynamic_with_options<R: Request + 'static, D: Clone + PartialEq + 'static>(
    options: Options<R, D>,
) -> DynLazyResponse<R> {
    let request = use_state(|| Option::<R>::None);

    let deps = ((*request).clone(), options.deps);

    let (run, result) = inner::use_future_callback(
        |request| async move {
            let Some(ref request) = request.0 else {
                return None;
            };

            let result = request.run().await;

            if let Some(ref handler) = options.handler {
                handler.emit(result.to_owned());
            }

            if let Ok(ref data) = result {
                R::store(data.to_owned());
            }

            Some(result)
        },
        deps,
    );

    let run = Callback::from(move |r| {
        request.set(Some(r));
        run.emit(());
    });

    if let Some(Ok(false)) = result.as_ref().map(|o| o.as_ref().map(|sr| sr.is_some())) {
        return DynLazyResponse { run, data: None };
    }

    let data = result.map(|res| res.map(|res| (*res).clone().unwrap()));

    DynLazyResponse { run, data }
}

/// Use the locally cached data instead of running the api request if possible
#[cfg(feature = "cache")]
#[hook]
pub fn use_cachable_api<R: Request + CachableRequest + 'static>(
    request: R,
) -> SuspensionResult<Result<R::Output, R::Error>> {
    use_cachable_api_with_options::<R, ()>(request, Default::default())
}

/// Use the locally cached data instead of running the api request if possible
#[cfg(feature = "cache")]
#[hook]
pub fn use_cachable_api_with_options<
    R: Request + CachableRequest + 'static,
    D: Clone + PartialEq + 'static,
>(
    request: R,
    options: Options<R, D>,
) -> SuspensionResult<Result<R::Output, R::Error>> {
    let store = use_store_value::<R::Store>();
    let deps = (request, options.deps);
    let result = inner::use_future_with_deps(
        |deps| async move {
            if let Some(cache) = deps.0.load(store) {
                return Ok(cache);
            }

            let result = deps.0.run().await;

            if let Some(ref handler) = options.handler {
                handler.emit(result.to_owned());
            }

            if let Ok(ref data) = result {
                R::store(data.to_owned());
            }

            result
        },
        deps,
    )?;

    Ok((*result).to_owned())
}

/// Use the locally cached data instead of running the api request if possible
/// Only returns a result once the callback was emitted
#[cfg(feature = "cache")]
#[hook]
pub fn use_cachable_api_lazy<R: Request + CachableRequest + 'static>(
    request: R,
) -> LazyResponse<R> {
    use_cachable_api_lazy_with_options::<R, ()>(request, Default::default())
}

/// Use the locally cached data instead of running the api request if possible
/// Only returns a result once the callback was emitted
#[cfg(feature = "cache")]
#[hook]
pub fn use_cachable_api_lazy_with_options<
    R: Request + CachableRequest + 'static,
    D: Clone + PartialEq + 'static,
>(
    request: R,
    options: Options<R, D>,
) -> LazyResponse<R> {
    let DynLazyResponse { run, data } = use_cachable_api_dynamic_with_options::<R, D>(options);

    let run = Callback::from(move |_| {
        run.emit(request.clone());
    });

    LazyResponse { run, data }
}

#[cfg(feature = "cache")]
#[hook]
pub fn use_cachable_api_dynamic<R: Request + CachableRequest + 'static>() -> DynLazyResponse<R> {
    use_cachable_api_dynamic_with_options::<R, ()>(Default::default())
}

/// Useful when not wanting to run a request on mount, e.g. for a logout button
/// You may run the request multiple times through multiple emits of the callback
///
/// By using the dynamic hook you can build the request with its parameters at runtime
#[cfg(feature = "cache")]
#[hook]
pub fn use_cachable_api_dynamic_with_options<
    R: Request + CachableRequest + 'static,
    D: Clone + PartialEq + 'static,
>(
    options: Options<R, D>,
) -> DynLazyResponse<R> {
    let store = use_store_value::<R::Store>();
    let request = use_state(|| Option::<R>::None);

    let deps = (request.clone(), options.deps);

    let (run, result) = inner::use_future_callback(
        |deps| async move {
            let Some(ref request) = *(deps.0) else {
                return None;
            };

            if let Some(cache) = request.load(store) {
                return Some(Ok(cache));
            }

            let result = request.run().await;

            if let Some(ref handler) = options.handler {
                handler.emit(result.to_owned());
            }

            if let Ok(ref data) = result {
                R::store(data.to_owned());
            }

            Some(result)
        },
        deps,
    );

    let run = Callback::from(move |r| {
        request.set(Some(r));
        run.emit(());
    });

    if let Some(Ok(false)) = result.as_ref().map(|o| o.as_ref().map(|sr| sr.is_some())) {
        return DynLazyResponse { run, data: None };
    }

    let data = result.map(|res| res.map(|res| (*res).clone().unwrap()));

    DynLazyResponse { run, data }
}

/// from yew@next
mod inner {
    use std::borrow::Borrow;
    use std::cell::Cell;
    use std::fmt;
    use std::future::Future;
    use std::ops::Deref;
    use std::rc::Rc;

    use yew::prelude::*;
    use yew::suspense::{Suspension, SuspensionResult};

    pub struct UseFutureHandle<O> {
        inner: UseStateHandle<Option<O>>,
    }

    impl<O> Deref for UseFutureHandle<O> {
        type Target = O;

        fn deref(&self) -> &Self::Target {
            self.inner.as_ref().unwrap()
        }
    }

    impl<T: fmt::Debug> fmt::Debug for UseFutureHandle<T> {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            f.debug_struct("UseFutureHandle")
                .field("value", &format!("{:?}", self.inner))
                .finish()
        }
    }

    #[hook]
    pub fn use_future<F, T, O>(init_f: F) -> SuspensionResult<UseFutureHandle<O>>
    where
        F: FnOnce() -> T,
        T: Future<Output = O> + 'static,
        O: 'static,
    {
        use_future_with_deps(move |_| init_f(), ())
    }

    #[hook]
    pub fn use_future_with_deps<F, D, T, O>(f: F, deps: D) -> SuspensionResult<UseFutureHandle<O>>
    where
        F: FnOnce(Rc<D>) -> T,
        T: Future<Output = O> + 'static,
        O: 'static,
        D: PartialEq + 'static,
    {
        let output = use_state(|| None);
        // We only commit a result if it comes from the latest spawned future. Otherwise, this
        // might trigger pointless updates or even override newer state.
        let latest_id = use_state(|| Cell::new(0u32));

        let suspension = {
            let output = output.clone();

            use_memo_base(
                move |deps| {
                    let self_id = latest_id.get().wrapping_add(1);
                    // As long as less than 2**32 futures are in flight wrapping_add is fine
                    (*latest_id).set(self_id);
                    let deps = Rc::new(deps);
                    let task = f(deps.clone());
                    let suspension = Suspension::from_future(async move {
                        let result = task.await;
                        if latest_id.get() == self_id {
                            output.set(Some(result));
                        }
                    });
                    (suspension, deps)
                },
                deps,
            )
        };

        if suspension.resumed() {
            Ok(UseFutureHandle { inner: output })
        } else {
            Err((*suspension).clone())
        }
    }

    #[hook]
    pub fn use_future_callback<F, D, T, O>(
        f: F,
        deps: D,
    ) -> (
        Callback<(), ()>,
        Option<SuspensionResult<UseFutureHandle<O>>>,
    )
    where
        F: FnOnce(Rc<D>) -> T,
        T: Future<Output = O> + 'static,
        O: 'static,
        D: Clone + PartialEq + 'static,
    {
        let execution = use_state(|| false);
        let execute: Callback<(), ()> = {
            let execution = execution.clone();
            use_callback(move |_, _| execution.set(true), ())
        };

        let output = use_state(|| None);
        // We only commit a result if it comes from the latest spawned future. Otherwise, this
        // might trigger pointless updates or even override newer state.
        let latest_id = use_state(|| Cell::new(0u32));

        let suspension = {
            let output = output.clone();

            let deps = (deps, execution.clone());
            use_memo_base(
                move |deps| {
                    if !(*execution) {
                        return (None, deps);
                    }

                    let self_id = latest_id.get().wrapping_add(1);
                    // As long as less than 2**32 futures are in flight wrapping_add is fine
                    (*latest_id).set(self_id);
                    let task = f(Rc::new(deps.0.clone()));
                    let suspension = Suspension::from_future(async move {
                        let result = task.await;

                        if latest_id.get() == self_id {
                            output.set(Some(result));
                        }
                        execution.set(false);
                    });
                    (Some(suspension), (deps.0.to_owned(), deps.1))
                },
                deps,
            )
        };

        if let Some(ref suspension) = *suspension {
            if suspension.resumed() {
                return (execute, Some(Ok(UseFutureHandle { inner: output })));
            } else {
                return (execute, Some(Err(suspension.clone())));
            }
        }

        if output.is_some() {
            return (execute, Some(Ok(UseFutureHandle { inner: output })));
        }

        (execute, None)
    }

    #[hook]
    pub(crate) fn use_memo_base<T, F, D, K>(f: F, deps: D) -> Rc<T>
    where
        T: 'static,
        F: FnOnce(D) -> (T, K),
        K: 'static + Borrow<D>,
        D: PartialEq,
    {
        struct MemoState<T, K> {
            memo_key: K,
            result: Rc<T>,
        }
        let state = use_mut_ref(|| -> Option<MemoState<T, K>> { None });

        let mut state = state.borrow_mut();
        match &*state {
            Some(existing) if existing.memo_key.borrow() != &deps => {
                // Drop old state if it's outdated
                *state = None;
            }
            _ => {}
        };
        let state = state.get_or_insert_with(|| {
            let (result, memo_key) = f(deps);
            let result = Rc::new(result);
            MemoState { result, memo_key }
        });
        state.result.clone()
    }
}