Skip to content

Instantly share code, notes, and snippets.

@cetra3
Created August 11, 2019 12:46
Show Gist options
  • Save cetra3/9fff16acb32b2d3d27404ef67778b340 to your computer and use it in GitHub Desktop.
Save cetra3/9fff16acb32b2d3d27404ef67778b340 to your computer and use it in GitHub Desktop.
use std::sync::RwLock;
use std::sync::RwLockReadGuard;
#[allow(dead_code)]
pub struct Store<T, U> {
name: String,
state: RwLock<T>,
listeners: RwLock<Vec<fn(&T, &U)>>,
reducer: fn(&mut T, &U),
}
#[allow(dead_code)]
impl<T, U> Store<T, U> {
pub fn create_store<S: Into<String>>(
name: S,
reducer: fn(&mut T, &U),
initial_state: T,
) -> Store<T, U> {
Store {
name: name.into(),
state: RwLock::new(initial_state),
listeners: RwLock::new(Vec::new()),
reducer,
}
}
pub fn subscribe(&self, listener: fn(&T, &U)) {
let mut listeners = self.listeners.write().expect("Could not write subscriber");
listeners.push(listener);
}
pub fn get_state(&self) -> RwLockReadGuard<T> {
if let Err(_) = self.state.try_read() {
warn!("Get State Called for `{}`, but would block", self.name);
}
self.state.read().expect("Could not get state")
}
pub fn dispatch(&self, action: U) {
(self.reducer)(
&mut self.state.write().expect("Could not write state"),
&action,
);
for listener in &*self.listeners.read().expect("Could not notify listeners") {
listener(&self.get_state(), &action)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment