mirror of
https://github.com/hexedtech/codemp-nvim.git
synced 2024-11-22 15:34:53 +01:00
feat: state manager with message passing
Co-authored-by: f-tlm <f-tlm@users.noreply.github.com>
This commit is contained in:
parent
0151a9e1bd
commit
7272879180
2 changed files with 79 additions and 0 deletions
61
src/server/state.rs
Normal file
61
src/server/state.rs
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
|
||||||
|
use std::{collections::HashMap, sync::Arc};
|
||||||
|
use tokio::sync::{mpsc, watch};
|
||||||
|
|
||||||
|
use crate::workspace::Workspace;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum AlterState {
|
||||||
|
ADD {
|
||||||
|
key: String,
|
||||||
|
w: Workspace
|
||||||
|
},
|
||||||
|
REMOVE { key: String },
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct StateManager {
|
||||||
|
store: HashMap<String, Arc<Workspace>>,
|
||||||
|
rx: mpsc::Receiver<AlterState>,
|
||||||
|
tx: watch::Sender<HashMap<String, Arc<Workspace>>>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StateManager {
|
||||||
|
pub fn new(rx: mpsc::Receiver<AlterState>, tx: watch::Sender<HashMap<String, Arc<Workspace>>>) -> StateManager {
|
||||||
|
StateManager {
|
||||||
|
store: HashMap::new(),
|
||||||
|
rx,
|
||||||
|
tx
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn run(mut self) {
|
||||||
|
loop {
|
||||||
|
if let Some(event) = self.rx.recv().await {
|
||||||
|
match event {
|
||||||
|
AlterState::ADD { key, w } => {
|
||||||
|
self.store.insert(key, Arc::new(w)); // TODO put in hashmap
|
||||||
|
},
|
||||||
|
AlterState::REMOVE { key } => {
|
||||||
|
self.store.remove(&key);
|
||||||
|
},
|
||||||
|
}
|
||||||
|
self.tx.send(self.store.clone()).unwrap();
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn run_state_manager() -> (mpsc::Sender<AlterState>, watch::Receiver<HashMap<String, Arc<Workspace>>>) {
|
||||||
|
let (tx, rx) = mpsc::channel(32); // TODO quantify backpressure
|
||||||
|
let (watch_tx, watch_rx) = watch::channel(HashMap::new());
|
||||||
|
let state = StateManager::new(rx, watch_tx);
|
||||||
|
|
||||||
|
let _task = tokio::spawn(async move {
|
||||||
|
state.run().await;
|
||||||
|
});
|
||||||
|
|
||||||
|
return (tx, watch_rx);
|
||||||
|
}
|
18
src/server/workspace.rs
Normal file
18
src/server/workspace.rs
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
// Must be clonable, containing references to the actual state maybe? Or maybe give everyone an Arc, idk
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Workspace {
|
||||||
|
pub name: String,
|
||||||
|
pub content: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Workspace {
|
||||||
|
pub fn new(name: String, content: String) -> Self {
|
||||||
|
Workspace { name , content }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Workspace {
|
||||||
|
fn default() -> Self {
|
||||||
|
Workspace { name: "fuck you".to_string() , content: "too".to_string() }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue