yew_oauth2/components/context/
agent.rs

1use crate::agent::{self, Client};
2use std::ops::{Deref, DerefMut};
3use std::sync::atomic::{AtomicUsize, Ordering};
4use yew::hook;
5
6/// A wrapper for the [`agent::Agent`].
7///
8/// Required as Yew has some requirements for the type of a context, like [`PartialEq`].
9#[derive(Clone, Debug)]
10pub struct Agent<C: Client>(agent::Agent<C>, usize);
11
12static COUNTER: AtomicUsize = AtomicUsize::new(0);
13
14impl<C: Client> Agent<C> {
15    pub fn new(agent: agent::Agent<C>) -> Self {
16        let id = COUNTER.fetch_add(1, Ordering::AcqRel);
17
18        Self(agent, id)
19    }
20}
21
22impl<C: Client> PartialEq for Agent<C> {
23    fn eq(&self, other: &Self) -> bool {
24        self.1.eq(&other.1)
25    }
26}
27
28impl<C: Client> Deref for Agent<C> {
29    type Target = agent::Agent<C>;
30
31    fn deref(&self) -> &Self::Target {
32        &self.0
33    }
34}
35
36impl<C: Client> DerefMut for Agent<C> {
37    fn deref_mut(&mut self) -> &mut Self::Target {
38        &mut self.0
39    }
40}
41
42/// Get the authentication agent.
43#[hook]
44pub fn use_auth_agent<C>() -> Option<Agent<C>>
45where
46    C: Client,
47{
48    yew::prelude::use_context()
49}