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
use std::thread::{JoinHandle, Thread};
use channel::Sender;
use crate::Reducer;
pub(crate) mod channel;
mod runtime;
pub(crate) mod testing;
#[doc = include_str!("README.md")]
pub struct Store<State: Reducer> {
sender: Sender<Result<<State as Reducer>::Action, Thread>>,
handle: JoinHandle<<State as Reducer>::Output>,
}
impl<State: Reducer> Store<State> {
/// Creates a new `Store` with `state` as its initial state.
///
/// If `State` is not [`Send`], then [`new`][`Store::new`] or [`default`][`Store::default`]
/// can be used instead.
pub fn with_initial(state: State) -> Self
where
State: Send + 'static,
<State as Reducer>::Action: Send,
<State as Reducer>::Output: Send + From<State>,
{
Store::runtime(|| state)
}
/// Creates a new `Store` with its initial state generated by a function.
///
/// Useful if `State` is not [`Send`], but the arguments used to construct it are.
pub fn new<F>(with: F) -> Self
where
F: (FnOnce() -> State) + Send + 'static,
<State as Reducer>::Action: Send + 'static,
<State as Reducer>::Output: Send + From<State> + 'static,
{
Store::runtime(with)
}
/// Calls the `Store`’s [`Reducer`][`crate::Reducer`] with `action`.
///
/// Takes an [`Into<Action>`] so that both child and parent `Action`s may be sent easily.
pub fn send(&self, action: impl Into<<State as Reducer>::Action>) {
self.sender.send(Ok(action.into()))
}
/// Stops the `Store`’s runtime and returns its current `state` value.
///
/// # Note
/// Care should be exercised when using this method in applications that utilize
/// asynchronous [`Effects`][`crate::effects::Effects`]. `into_inner` makes a “best effort”
/// to wait until any pending tasks are completed but it is not guaranteed.
pub fn into_inner(self) -> <State as Reducer>::Output {
self.sender.send(Err(std::thread::current()));
std::thread::park(); // waiting for any async tasks to finish up
drop(self.sender); // ends the runtime’s (outer) while-let
std::thread::yield_now(); // give it time to shut down
self.handle.join().unwrap()
}
}
impl<State: Reducer> Default for Store<State>
where
State: Default,
<State as Reducer>::Action: Send + 'static,
<State as Reducer>::Output: Send + From<State> + 'static,
{
/// Creates a new `Store` with a default initial state.
fn default() -> Self {
Store::new(|| State::default())
}
}