use crate::boundary::{Action, Gesture, codec};
use crate::wire::link::Landed;
use crate::wire::post::{Post, Ticket};
use std::collections::BTreeMap;
use std::path::PathBuf;
#[derive(Default)]
pub struct Acts {
post: Post,
roots: BTreeMap<Ticket, Vec<PathBuf>>,
}
impl crate::AppModel {
pub fn adopt_post(&mut self, post: Post) {
self.acts.post = post;
}
pub fn post_act(&mut self, action: &Action) -> Ticket {
let roots = self.act_roots(action);
let envelope = codec::encode(&Gesture::Act(action.clone()));
let ticket = self.acts.post.send(&envelope);
self.acts.roots.insert(ticket, roots);
ticket
}
pub fn act_receipt(&mut self, ticket: Ticket) -> Option<Landed> {
self.acts.post.receipt(ticket)
}
pub(super) fn settle_acts(&mut self) {
for ticket in self.acts.post.settle() {
if let Some(roots) = self.acts.roots.remove(&ticket) {
self.mark_dirty(roots);
}
}
}
fn act_roots(&self, action: &Action) -> Vec<PathBuf> {
let substrate = action
.project()
.and_then(|name| self.derivation().project_path(&name).ok())
.unwrap_or_else(|| self.roots.yog_state.clone());
let workspace = action
.workspace()
.and_then(|name| self.derivation().ws_path(&name).ok());
std::iter::once(substrate).chain(workspace).collect()
}
}
#[cfg(test)]
mod tests;