Skip to content

Commit

Permalink
middleware in seperate file
Browse files Browse the repository at this point in the history
  • Loading branch information
Elmer Bulthuis committed Dec 3, 2019
1 parent 62cd4c8 commit fef826c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
2 changes: 2 additions & 0 deletions examples/light/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
extern crate reduxr;

mod actions;
mod middlewares;
mod selectors;
mod states;
mod stores;

pub use actions::*;
pub use middlewares::*;
pub use selectors::*;
pub use states::*;
pub use stores::*;
Expand Down
26 changes: 26 additions & 0 deletions examples/light/middlewares.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
extern crate reduxr;

use super::*;
use reduxr::Dispatch;
use std::sync::Mutex;

pub fn create_switch_middleware() -> impl FnOnce(Dispatch<State, Action>) -> Dispatch<State, Action>
{
let mutex = Mutex::new(());

|next| {
Box::new(move |store, action| {
next(store, action);

if let Action::Switch = action {
let _lock = mutex.lock();
let state = store.get_state();
if state.select_power() {
store.dispatch(Action::TurnOff);
} else {
store.dispatch(Action::TurnOn);
}
}
})
}
}
20 changes: 1 addition & 19 deletions examples/light/stores.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,9 @@ extern crate reduxr;

use super::*;
use reduxr::Store;
use std::sync::Mutex;

pub fn create_store() -> Store<State, Action> {
let store: Store<State, _> = Store::default();
store.add_middleware(|next| {
let mutex = Mutex::new(());

Box::new(move |store, action| {
next(store, action);

if let Action::Switch = action {
let _lock = mutex.lock();
let state = store.get_state();
if state.select_power() {
store.dispatch(Action::TurnOff);
} else {
store.dispatch(Action::TurnOn);
}
}
})
})
Store::default().add_middleware(create_switch_middleware())
}

#[cfg(test)]
Expand Down
12 changes: 6 additions & 6 deletions src/store.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::*;
use std::sync::RwLock;

type Dispatcher<State, Action> = Box<dyn Send + Sync + Fn(&Store<State, Action>, Action)>;
pub type Dispatch<State, Action> = Box<dyn Send + Sync + Fn(&Store<State, Action>, Action)>;

/// A redux store. Dispatching actions on the store will make the action pass through the
/// middleware and finally the state will be reduced via the `Reduce` trait.
///
/// All middleware may return a value that is eventually returned from the dispatch function.
pub struct Store<State, Action> {
state_lock: RwLock<State>,
dispatcher: Dispatcher<State, Action>,
dispatch_handler: Dispatch<State, Action>,
}

impl<State, Action> Store<State, Action>
Expand All @@ -20,7 +20,7 @@ where
pub fn new(state: State) -> Self {
Store {
state_lock: RwLock::new(state),
dispatcher: Box::new(|store, action| {
dispatch_handler: Box::new(|store, action| {
let mut state = store.state_lock.write().unwrap();
*state = state.clone().reduce(action);
}),
Expand All @@ -29,15 +29,15 @@ where

pub fn add_middleware(
mut self,
middleware: impl FnOnce(Dispatcher<State, Action>) -> Dispatcher<State, Action>,
middleware: impl FnOnce(Dispatch<State, Action>) -> Dispatch<State, Action>,
) -> Self {
self.dispatcher = middleware(self.dispatcher);
self.dispatch_handler = middleware(self.dispatch_handler);
self
}

/// Dispatch action through the middleware and eventualle reduce state with it!
pub fn dispatch(&self, action: Action) {
(self.dispatcher)(self, action);
(self.dispatch_handler)(self, action);
}

/// Get a clone of the current state
Expand Down

0 comments on commit fef826c

Please sign in to comment.