use anyhow::Context;
use super::store::Store;
use super::view::View;
pub struct Dispatcher<S, V> {
store: S,
view: V,
}
impl<S, V> Dispatcher<S, V> {
pub fn new(store: S, view: V) -> Self {
Dispatcher { store, view }
}
}
impl<S, V> Dispatcher<S, V>
where
S: Store,
V: View<S>,
{
pub fn dispatch(&mut self, action: S::Action) -> anyhow::Result<()> {
self.store
.handle(action)
.context("failed to handle the action")?;
self.view.on_update(&self.store).context(
"failed to update the view with the latest data from the store",
)
}
pub fn get_store(&self) -> &S {
&self.store
}
}