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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
#![doc = include_str!("README.md")]
use std::cell::RefCell;
use std::collections::VecDeque;
use std::iter::from_fn;
use std::marker::PhantomData as Marker;
use std::rc::Weak;
use std::time::{Duration, Instant};
use futures::stream::{iter, once};
use futures::{Future, Stream, StreamExt};
pub(crate) use delay::Delay;
pub(crate) use task::Executor;
#[doc(hidden)]
pub use task::Task;
mod delay;
pub(crate) mod scheduler;
mod task;
/// `Effects` are used within `Reducer`s to propagate `Action`s as side-effects of performing other `Action`s.
///
/// `Effects` are also [`Scheduler`]s — able to apply modifiers to when (and how often) `Action`s. are sent.
///
/// See [the module level documentation](self) for more.
pub trait Effects: Clone + Scheduler<Action = <Self as Effects>::Action> {
/// The `Action` type sent by this `Effects`.
type Action;
/// An effect that sends an [`Action`][`Self::Action`] through
/// the `Store`’s [`Reducer`][`crate::Reducer`].
#[doc(alias = "send")]
fn action(&self, action: impl Into<<Self as Effects>::Action>);
/// A [`Task`] represents asynchronous work that will then [`send`][`crate::Store::send`]
/// zero or more [`Action`][`Self::Action`]s back into the `Store`’s [`Reducer`][`crate::Reducer`]
/// as it runs.
///
/// Use this method if you need to ability to [`cancel`][Task::cancel] the task
/// while it is running. Otherwise [`future`][Effects::future] or [`stream`][Effects::stream]
/// should be preferred.
fn task<S: Stream<Item = <Self as Effects>::Action> + 'static>(&self, stream: S) -> Task;
/// An effect that runs a [`Future`][`std::future`] and, if it returns an
/// [`Action`][`Self::Action`], sends it through the `Store`’s [`Reducer`][`crate::Reducer`].
#[inline(always)]
fn future<F: Future<Output = Option<<Self as Effects>::Action>> + 'static>(&self, future: F)
where
<Self as Effects>::Action: 'static,
{
let stream = once(future).filter_map(|action| async move { action });
self.task(stream).detach()
}
/// An effect that runs a [`Stream`](https://docs.rs/futures/latest/futures/stream/index.html)
/// and sends every [`Action`][`Self::Action`] it returns through the `Store`’s
/// [`Reducer`][`crate::Reducer`].
#[inline(always)]
fn stream<S: Stream<Item = <Self as Effects>::Action> + 'static>(&self, stream: S) {
self.task(stream).detach()
}
/// Scopes the `Effects` down to one that sends child actions.
///
/// For example, the inner loop of the [`RecursiveReducer`] macro is,
/// effectively, just calling
///
/// ```rust ignore
/// if let Ok(action) = action.clone().try_into() {
/// reduce(&mut self.child_reducer, action, effects.scope());
/// }
/// ```
/// on each child-reducer.
///
/// [`RecursiveReducer`]: crate::derive_macros
#[inline(always)]
fn scope<ChildAction>(&self) -> Scoped<Self, ChildAction>
where
<Self as Effects>::Action: From<ChildAction>,
{
Scoped(self.clone(), Marker)
}
}
/// [`Effects`] are also `Scheduler`s — able to apply modifiers to when (and how often) `Action`s. are sent.
pub trait Scheduler {
/// The `Action` sends scheduled by this `Scheduler`.
type Action;
#[doc(hidden)]
fn now(&self) -> Instant {
Instant::now()
}
#[doc(hidden)]
fn schedule(
&self,
action: Self::Action,
after: impl IntoIterator<Item = Delay> + 'static,
) -> Task
where
Self::Action: Clone + 'static;
/// Sends the `Action` after `duration`.
fn after(&self, duration: Duration, action: Self::Action) -> Task
where
Self::Action: Clone + 'static,
{
let instant = self.now() + duration;
self.at(instant, action)
}
/// Sends the `Action` at `instant`.
fn at(&self, instant: Instant, action: Self::Action) -> Task
where
Self::Action: Clone + 'static,
{
let mut task = self.schedule(action, [Delay::new(instant)]);
task.when = Some(instant);
task
}
/// Sends the `Action` every `interval`.
fn every(&self, interval: Interval, action: Self::Action) -> Task
where
Self::Action: Clone + 'static,
{
let (mut n, duration) = match interval {
Interval::Leading(duration) => (0, duration), // 0 × delay => no initial delay
Interval::Trailing(duration) => (1, duration),
};
let start = self.now();
self.schedule(
action,
from_fn(move || {
let instant = start.checked_add(duration.checked_mul(n)?)?;
n = n.checked_add(1)?;
Some(Delay::new(instant))
}),
)
}
/// An effect that coalesces repeated attempts to send [`Action`][`Effects::Action`]s
/// through the `Store`’s [`Reducer`][`crate::Reducer`] into a singe send.
/// Once `timeout` has elapsed with no further `Action`s being attempted,
/// the last `Action` will be sent.
///
/// The `debounce` function will automatically update the information
/// stored in `previous` as it runs. The `Task` debounced by this call
/// will be the _previous_ task for the next call, if any.
fn debounce(&self, action: Self::Action, previous: &mut Option<Task>, interval: Interval)
where
Self::Action: Clone + 'static,
{
let task = match interval {
Interval::Trailing(timeout) => self.after(timeout, action),
Interval::Leading(timeout) => {
let now = self.now();
match previous.as_ref().and_then(|task| task.when).as_ref() {
None => self.at(now, action),
Some(then) => {
if now <= *then + timeout {
return; // A leading debounce DROPS subsequent actions within the interval
}
self.at(now, action)
}
}
}
};
*previous = Some(task);
}
/// An effect that sends an [`Action`][`Effects::Action`] through the `Store`’s
/// [`Reducer`][`crate::Reducer`] if at least one `interval` of time has passed
/// since `previous` was sent. Otherwise, all subsequent actions but the last
/// are dropped until that time; which resets the countdown until the next
/// debounced action can be sent.
///
/// The `throttle` function will automatically update the information
/// stored in `previous` as it runs. The `Task` throttled by this call
/// will be the _previous_ task for the next call, if any.
fn throttle(&self, action: Self::Action, previous: &mut Option<Task>, interval: Interval)
where
Self::Action: Clone + 'static,
{
let now = self.now();
let timeout = interval.duration();
let when = match previous.take().and_then(|task| task.when) {
Some(when) if when > now => when, // previous was not yet sent — replace it
Some(when) if when + timeout > now => when + timeout, // previous was sent recently
_ => match interval {
Interval::Leading(_) => now,
Interval::Trailing(_) => now + timeout,
},
};
let task = self.at(when, action);
*previous = Some(task);
}
}
/// When a [`Scheduler`] uses a repeating interval, that interval can begin immediately, a `Leading`
/// interval, or it may begin after the first delay, a `Trailing` interval.
pub enum Interval {
/// The first `Action` should be sent immediately.
Leading(Duration),
/// The first `Action` should not be send until after the `Duration` has passed.
Trailing(Duration),
}
impl Interval {
pub fn duration(&self) -> Duration {
match self {
Interval::Leading(duration) => *duration,
Interval::Trailing(duration) => *duration,
}
}
}
/// An `Effects` that scopes its `Action`s to one that sends child actions.
///
/// This `struct` is created by the [`scope`] method on [`Effects`]. See its
/// documentation for more.
///
/// [`scope`]: Effects::scope
pub struct Scoped<Parent, Child>(Parent, Marker<Child>);
// Using `#[derive(Clone)]` adds a `Clone` requirement to all `Action`s
impl<Parent: Clone, Child> Clone for Scoped<Parent, Child> {
#[inline(always)]
fn clone(&self) -> Self {
Scoped(self.0.clone(), Marker)
}
}
impl<Parent, Child> Effects for Scoped<Parent, Child>
where
Parent: Effects,
<Parent as Effects>::Action: Clone + From<Child> + 'static,
Child: 'static,
{
type Action = Child;
#[inline(always)]
fn action(&self, action: impl Into<<Self as Effects>::Action>) {
self.0.action(action.into());
}
#[inline(always)]
fn task<S: Stream<Item = Child> + 'static>(&self, stream: S) -> Task {
self.0.task(stream.map(|action| action.into()))
}
}
#[doc(hidden)]
impl<Parent, Child> Scheduler for Scoped<Parent, Child>
where
Parent: Effects,
<Parent as Effects>::Action: From<Child> + Clone + 'static,
{
type Action = Child;
#[inline(always)]
fn schedule(
&self,
action: Self::Action,
after: impl IntoIterator<Item = Delay> + 'static,
) -> Task
where
Self::Action: Clone + 'static,
{
self.0.schedule(action.into(), after)
}
}
#[doc(hidden)]
// `Parent` for `Effects::scope` tuples
impl<Action: 'static> Effects for Weak<RefCell<VecDeque<Action>>> {
type Action = Action;
fn action(&self, action: impl Into<Action>) {
if let Some(actions) = self.upgrade() {
actions.borrow_mut().push_back(action.into())
}
}
fn task<S: Stream<Item = Action> + 'static>(&self, stream: S) -> Task {
Task::new(stream)
}
}
#[doc(hidden)]
impl<Action: 'static> Scheduler for Weak<RefCell<VecDeque<Action>>> {
type Action = Action;
fn schedule(&self, action: Action, delays: impl IntoIterator<Item = Delay> + 'static) -> Task
where
Action: Clone + 'static,
{
self.task(iter(delays).then(move |delay| {
let action = action.clone();
async move {
delay.await;
action.clone()
}
}))
}
}