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

yew_agent/
utils.rs

1use std::rc::Rc;
2use std::sync::atomic::{AtomicUsize, Ordering};
3
4use yew::Reducible;
5
6/// Gets a unique worker id
7pub(crate) fn get_next_id() -> usize {
8    static CTR: AtomicUsize = AtomicUsize::new(0);
9
10    CTR.fetch_add(1, Ordering::SeqCst)
11}
12
13#[derive(Default, PartialEq)]
14pub(crate) struct BridgeIdState {
15    pub inner: usize,
16}
17
18impl Reducible for BridgeIdState {
19    type Action = ();
20
21    fn reduce(self: Rc<Self>, _: Self::Action) -> Rc<Self> {
22        Self {
23            inner: self.inner + 1,
24        }
25        .into()
26    }
27}
28
29pub(crate) enum OutputsAction<T> {
30    Push(Rc<T>),
31    Close,
32    Reset,
33}
34
35pub(crate) struct OutputsState<T> {
36    pub ctr: usize,
37    pub inner: Vec<Rc<T>>,
38    pub closed: bool,
39}
40
41impl<T> Clone for OutputsState<T> {
42    fn clone(&self) -> Self {
43        Self {
44            ctr: self.ctr,
45            inner: self.inner.clone(),
46            closed: self.closed,
47        }
48    }
49}
50
51impl<T> Reducible for OutputsState<T> {
52    type Action = OutputsAction<T>;
53
54    fn reduce(mut self: Rc<Self>, action: Self::Action) -> Rc<Self> {
55        {
56            let this = Rc::make_mut(&mut self);
57            this.ctr += 1;
58
59            match action {
60                OutputsAction::Push(m) => this.inner.push(m),
61                OutputsAction::Close => {
62                    this.closed = true;
63                }
64                OutputsAction::Reset => {
65                    this.closed = false;
66                    this.inner = Vec::new();
67                }
68            }
69        }
70
71        self
72    }
73}
74
75impl<T> Default for OutputsState<T> {
76    fn default() -> Self {
77        Self {
78            ctr: 0,
79            inner: Vec::new(),
80            closed: false,
81        }
82    }
83}