2023-04-19 04:18:22 +02:00
|
|
|
use std::{sync::Mutex, collections::VecDeque};
|
2023-04-17 14:56:00 +02:00
|
|
|
|
|
|
|
use operational_transform::{OperationSeq, OTError};
|
2023-04-19 04:18:22 +02:00
|
|
|
use tokio::sync::watch;
|
2023-04-17 14:56:00 +02:00
|
|
|
|
|
|
|
use crate::operation::factory::OperationFactory;
|
|
|
|
|
|
|
|
|
|
|
|
#[tonic::async_trait]
|
2023-04-19 04:18:22 +02:00
|
|
|
pub trait OperationProcessor : OperationFactory {
|
2023-04-17 14:56:00 +02:00
|
|
|
async fn apply(&self, op: OperationSeq) -> Result<String, OTError>;
|
|
|
|
async fn process(&self, op: OperationSeq) -> Result<String, OTError>;
|
|
|
|
|
|
|
|
async fn poll(&self) -> Option<OperationSeq>;
|
|
|
|
async fn ack(&self) -> Option<OperationSeq>;
|
2023-04-19 04:18:22 +02:00
|
|
|
async fn wait(&self);
|
2023-04-17 14:56:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub struct OperationController {
|
|
|
|
text: Mutex<String>,
|
|
|
|
queue: Mutex<VecDeque<OperationSeq>>,
|
|
|
|
last: Mutex<watch::Receiver<OperationSeq>>,
|
|
|
|
notifier: watch::Sender<OperationSeq>,
|
2023-04-19 04:18:22 +02:00
|
|
|
changed: Mutex<watch::Receiver<()>>,
|
|
|
|
changed_notifier: watch::Sender<()>,
|
2023-04-17 14:56:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl OperationController {
|
|
|
|
pub fn new(content: String) -> Self {
|
|
|
|
let (tx, rx) = watch::channel(OperationSeq::default());
|
2023-04-19 04:18:22 +02:00
|
|
|
let (done, wait) = watch::channel(());
|
2023-04-17 14:56:00 +02:00
|
|
|
OperationController {
|
|
|
|
text: Mutex::new(content),
|
|
|
|
queue: Mutex::new(VecDeque::new()),
|
|
|
|
last: Mutex::new(rx),
|
|
|
|
notifier: tx,
|
2023-04-19 04:18:22 +02:00
|
|
|
changed: Mutex::new(wait),
|
|
|
|
changed_notifier: done,
|
2023-04-17 14:56:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OperationFactory for OperationController {
|
|
|
|
fn content(&self) -> String {
|
|
|
|
self.text.lock().unwrap().clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tonic::async_trait]
|
|
|
|
impl OperationProcessor for OperationController {
|
|
|
|
async fn apply(&self, op: OperationSeq) -> Result<String, OTError> {
|
|
|
|
let txt = self.content();
|
|
|
|
let res = op.apply(&txt)?;
|
|
|
|
*self.text.lock().unwrap() = res.clone();
|
|
|
|
self.queue.lock().unwrap().push_back(op.clone());
|
2023-04-19 04:18:22 +02:00
|
|
|
self.notifier.send(op);
|
2023-04-17 14:56:00 +02:00
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2023-04-19 04:18:22 +02:00
|
|
|
async fn wait(&self) {
|
|
|
|
let mut blocker = self.changed.lock().unwrap().clone();
|
|
|
|
blocker.changed().await;
|
|
|
|
blocker.changed().await;
|
|
|
|
}
|
|
|
|
|
2023-04-17 14:56:00 +02:00
|
|
|
async fn process(&self, mut op: OperationSeq) -> Result<String, OTError> {
|
|
|
|
let mut queue = self.queue.lock().unwrap();
|
|
|
|
for el in queue.iter_mut() {
|
|
|
|
(op, *el) = op.transform(el)?;
|
|
|
|
}
|
|
|
|
let txt = self.content();
|
|
|
|
let res = op.apply(&txt)?;
|
|
|
|
*self.text.lock().unwrap() = res.clone();
|
2023-04-19 04:18:22 +02:00
|
|
|
self.changed_notifier.send(());
|
2023-04-17 14:56:00 +02:00
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn poll(&self) -> Option<OperationSeq> {
|
|
|
|
let len = self.queue.lock().unwrap().len();
|
|
|
|
if len <= 0 {
|
|
|
|
let mut recv = self.last.lock().unwrap().clone();
|
2023-04-19 04:18:22 +02:00
|
|
|
// TODO this is not 100% reliable
|
|
|
|
recv.changed().await; // acknowledge current state
|
|
|
|
recv.changed().await; // wait for a change in state
|
2023-04-17 14:56:00 +02:00
|
|
|
}
|
|
|
|
Some(self.queue.lock().unwrap().get(0)?.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn ack(&self) -> Option<OperationSeq> {
|
|
|
|
self.queue.lock().unwrap().pop_front()
|
|
|
|
}
|
|
|
|
}
|