pub fn use_future_notion_runner<'hook, T>(
) -> impl 'hook + Hook<Output = Rc<dyn Fn(T::Input)>>
where T: FutureNotion + 'static + 'hook,
Expand description

A hook to create a function that when called, runs a FutureNotion with provided input.

A FutureNotion is created by applying a #[future_notion(NotionName)] attribute to an async function.

When a future notion is run, it will be applied twice with a notion type Deferred<T>. The first time is before it starts with a variant Pending and the second time is when it completes with variant Complete.

If the notion read any other states using the BounceStates argument, it will subscribe to the states, when any state changes, an Outdated variant will be dispatched.

Note

If you are trying to interact with a backend API, it is recommended to use the Query API instead.

Example


#[derive(PartialEq)]
struct User {
    id: u64,
    username: String,
}

#[future_notion(FetchUser)]
async fn fetch_user(id: &u64) -> User {
    // fetch user here...

    User { id: *id, username: "username".into() }
}

#[derive(PartialEq, Default, Atom)]
#[bounce(with_notion(Deferred<FetchUser>))]  // A future notion with type `T` will be applied as `Deferred<T>`.
struct UserState {
    inner: Option<Rc<User>>,
}

// Each time a future notion is run, it will be applied twice.
impl WithNotion<Deferred<FetchUser>> for UserState {
    fn apply(self: Rc<Self>, notion: Rc<Deferred<FetchUser>>) -> Rc<Self> {
        match notion.output() {
            Some(m) => Self { inner: Some(m) }.into(),
            None => self,
        }
    }
}

let load_user = use_future_notion_runner::<FetchUser>();
load_user(1);

Note

When used in function components and hooks, this hook is equivalent to:

pub fn use_future_notion_runner<T>() -> Rc<dyn Fn(T::Input)>
where
    T: FutureNotion + 'static,
{
    /* implementation omitted */
}