This is unreleased documentation for Yew Next version.
For up-to-date documentation, see the latest version on docs.rs.

yew_agent/oneshot/
hooks.rs

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
use yew::prelude::*;

use super::provider::OneshotProviderState;
use super::Oneshot;

/// Hook handle for [`use_oneshot_runner`]
#[derive(Debug)]
pub struct UseOneshotRunnerHandle<T>
where
    T: Oneshot + 'static,
{
    state: OneshotProviderState<T>,
}

impl<T> UseOneshotRunnerHandle<T>
where
    T: Oneshot + 'static,
{
    /// Runs an oneshot agent.
    pub async fn run(&self, input: T::Input) -> T::Output {
        self.state.create_bridge().run(input).await
    }
}

impl<T> Clone for UseOneshotRunnerHandle<T>
where
    T: Oneshot + 'static,
{
    fn clone(&self) -> Self {
        Self {
            state: self.state.clone(),
        }
    }
}

impl<T> PartialEq for UseOneshotRunnerHandle<T>
where
    T: Oneshot,
{
    fn eq(&self, rhs: &Self) -> bool {
        self.state == rhs.state
    }
}

/// A hook to create a runner to an oneshot agent.
#[hook]
pub fn use_oneshot_runner<T>() -> UseOneshotRunnerHandle<T>
where
    T: Oneshot + 'static,
{
    let state = use_context::<OneshotProviderState<T>>().expect("failed to find worker context");

    UseOneshotRunnerHandle { state }
}