Skip to main content

fusor_async/
browser.rs

1//! Browser executor bindings: `read` and `resource` run loads with `spawn_local`.
2use crate::{AsyncValue, CancellationToken, Resource};
3use fusor::OwnerHandle;
4use std::future::Future;
5
6/// Declare a read participating in the nearest generated coherent region.
7pub fn read<K, T, E, F>(
8    owner: &OwnerHandle,
9    key: impl Fn() -> K + 'static,
10    load: impl Fn(K, CancellationToken) -> F + 'static,
11) -> AsyncValue<K, T, E>
12where
13    K: Clone + PartialEq + 'static,
14    T: 'static,
15    E: std::fmt::Display + 'static,
16    F: Future<Output = Result<T, E>> + 'static,
17{
18    AsyncValue::new(owner, key, load, wasm_bindgen_futures::spawn_local)
19}
20
21/// Create a [`Resource`] whose loads run with `wasm_bindgen_futures::spawn_local`.
22pub fn resource<K, T, E, F>(
23    owner: &OwnerHandle,
24    key: impl Fn() -> Option<K> + 'static,
25    load: impl Fn(K, CancellationToken) -> F + 'static,
26) -> Resource<K, T, E>
27where
28    K: Clone + PartialEq + 'static,
29    T: 'static,
30    E: 'static,
31    F: Future<Output = Result<T, E>> + 'static,
32{
33    Resource::new(owner, key, load, wasm_bindgen_futures::spawn_local)
34}