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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#![doc = include_str!("../README.md")]

#[doc(hidden)]
#[path = "exports.rs"]
pub mod __private;
pub mod context;

use context::StateContext;
use futures_core::Stream;

use std::{
    ops::{Deref, DerefMut},
    pin::Pin,
    task::Poll,
};

/// Core trait
pub trait AsyncComponent {
    fn update_component(&mut self);
}

/// State trait
///
/// Returns output if state is updated
pub trait State {
    type Output;

    fn update(this: &mut Self) -> Option<Self::Output>;
}

/// Track change of value and signal to [`StateContext`].
/// This struct has no method and implements [`Deref`], [`DerefMut`].
/// When inner value is mutable dereferenced, it is marked changed and send signal.
/// This will also send signal when the cell is constructed or dropped.
#[derive(Debug)]
pub struct StateCell<T> {
    cx: StateContext,
    changed: bool,
    inner: T,
}

impl<T> StateCell<T> {
    /// Create new [`StateCell`] with [`StateContext`]
    pub fn new(cx: StateContext, inner: T) -> Self {
        cx.signal();

        Self {
            cx,
            changed: true,
            inner,
        }
    }

    /// Invalidate this [`StateCell`].
    /// Send signal to context.
    pub fn invalidate(this: &mut Self) {
        if !this.changed {
            this.changed = true;
        }

        this.cx.signal()
    }
}

impl<T> Deref for StateCell<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl<T> DerefMut for StateCell<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        StateCell::invalidate(self);

        &mut self.inner
    }
}

impl<T> State for StateCell<T> {
    type Output = ();

    fn update(this: &mut Self) -> Option<Self::Output> {
        if this.changed {
            this.changed = false;
            Some(())
        } else {
            None
        }
    }
}

impl<T> Drop for StateCell<T> {
    fn drop(&mut self) {
        self.cx.signal();
    }
}

/// State which polls inner stream
#[derive(Debug)]
pub struct StreamCell<T> {
    cx: StateContext,
    inner: T,
}

impl<T: Stream> StreamCell<T> {
    pub fn new(cx: StateContext, inner: T) -> Self {
        cx.signal();

        Self { cx, inner }
    }
}

impl<T: Stream + Unpin> State for StreamCell<T> {
    type Output = T::Item;

    fn update(this: &mut Self) -> Option<Self::Output> {
        match Pin::new(&mut this.inner).poll_next(&mut this.cx.task_context()) {
            Poll::Ready(Some(output)) => Some(output),
            _ => None,
        }
    }
}

impl<T> Drop for StreamCell<T> {
    fn drop(&mut self) {
        self.cx.signal();
    }
}