container_muxer: filters done
use crate::state_types::*;
+use enclose::*;
use serde_derive::*;
-use std::ops::Deref;
-use std::collections::BTreeMap;
use std::cell::RefCell;
+use std::collections::BTreeMap;
+use std::ops::Deref;
use std::rc::Rc;
#[derive(Debug, Clone, Serialize)]
@@ -22,7 +23,10 @@ pub struct ContainerMuxer<U> {
chain: Chain,
filters: Rc<RefCell<BTreeMap<U, ActionLoad>>>,
}
-impl<T> ContainerMuxer<T> where T: 'static + Clone + Ord {
+impl<T> ContainerMuxer<T>
+where
+ T: 'static + Clone + Ord,
+{
pub fn new<U: 'static + Deref<Target = ContainerInterface>>(
middlewares: Vec<Box<Handler>>,
containers: Vec<(T, U)>,
@@ -31,23 +35,39 @@ impl<T> ContainerMuxer<T> where T: 'static + Clone + Ord {
let filters = Rc::new(RefCell::new(BTreeMap::new()));
let chain = Chain::new(
middlewares,
- Box::new(move |action| {
+ Box::new(enclose!((filters) move |action| {
cb(Event::Action(action));
for (id, container) in containers.iter() {
+ // If we've set a filter for this container ID,
+ // we will only allow Load/LoadWithCtx that match it
+ match action {
+ Action::Load(x) | Action::LoadWithCtx(_, x) => {
+ match filters.borrow().get(id) {
+ Some(y) if x != y => continue,
+ _ => (),
+ }
+ }
+ _ => (),
+ }
let has_new_state = container.dispatch(action);
if has_new_state {
cb(Event::NewState(id));
}
}
- }),
+ })),
);
ContainerMuxer { chain, filters }
}
pub fn dispatch(&self, action: &Action) {
+ if let Action::Load(_) = action {
+ self.filters.borrow_mut().clear();
+ }
self.chain.dispatch(action)
}
pub fn dispatch_load_to(&self, id: &T, action_load: &ActionLoad) {
- self.filters.borrow_mut().insert(id.to_owned(), action_load.to_owned());
- unimplemented!();
+ self.filters
+ .borrow_mut()
+ .insert(id.to_owned(), action_load.to_owned());
+ self.chain.dispatch(&Action::Load(action_load.to_owned()));
}
}
