mirror of
https://github.com/hexedtech/codemp-nvim.git
synced 2024-11-22 23:44:55 +01:00
fix: one factory per buffer, create on attach
This commit is contained in:
parent
1eec71f3b2
commit
3827ab066d
2 changed files with 81 additions and 95 deletions
|
@ -104,40 +104,19 @@ impl Handler for NeovimHandler {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
"sync" => {
|
|
||||||
if args.len() < 1 {
|
|
||||||
return Err(Value::from("no path given"));
|
|
||||||
}
|
|
||||||
let path = default_empty_str(&args, 0);
|
|
||||||
|
|
||||||
let mut c = self.client.clone();
|
|
||||||
match c.sync(path).await {
|
|
||||||
Err(e) => Err(Value::from(format!("could not sync: {}", e))),
|
|
||||||
Ok(content) => match nvim.get_current_buf().await {
|
|
||||||
Err(e) => return Err(Value::from(format!("could not get current buffer: {}", e))),
|
|
||||||
Ok(b) => {
|
|
||||||
let lines : Vec<String> = content.split("\n").map(|x| x.to_string()).collect();
|
|
||||||
match b.set_lines(0, -1, false, lines).await {
|
|
||||||
Err(e) => Err(Value::from(format!("failed sync: {}", e))),
|
|
||||||
Ok(()) => Ok(Value::from("synched")),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
"attach" => {
|
"attach" => {
|
||||||
if args.len() < 1 {
|
if args.len() < 1 {
|
||||||
return Err(Value::from("no path given"));
|
return Err(Value::from("no path given"));
|
||||||
}
|
}
|
||||||
let path = default_empty_str(&args, 0);
|
let path = default_empty_str(&args, 0);
|
||||||
let buf = match nvim.get_current_buf().await {
|
let buffer = match nvim.get_current_buf().await {
|
||||||
Ok(b) => b,
|
Ok(b) => b,
|
||||||
Err(e) => return Err(Value::from(format!("could not get current buffer: {}", e))),
|
Err(e) => return Err(Value::from(format!("could not get current buffer: {}", e))),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut c = self.client.clone();
|
let mut c = self.client.clone();
|
||||||
|
|
||||||
|
let buf = buffer.clone();
|
||||||
match c.attach(path, move |x| {
|
match c.attach(path, move |x| {
|
||||||
let lines : Vec<String> = x.split("\n").map(|x| x.to_string()).collect();
|
let lines : Vec<String> = x.split("\n").map(|x| x.to_string()).collect();
|
||||||
let b = buf.clone();
|
let b = buf.clone();
|
||||||
|
@ -147,8 +126,14 @@ impl Handler for NeovimHandler {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}).await {
|
}).await {
|
||||||
Ok(()) => Ok(Value::from("spawned worker")),
|
|
||||||
Err(e) => Err(Value::from(format!("could not attach to stream: {}", e))),
|
Err(e) => Err(Value::from(format!("could not attach to stream: {}", e))),
|
||||||
|
Ok(content) => {
|
||||||
|
let lines : Vec<String> = content.split("\n").map(|x| x.to_string()).collect();
|
||||||
|
if let Err(e) = buffer.set_lines(0, -1, false, lines).await {
|
||||||
|
error!("could not update buffer: {}", e);
|
||||||
|
}
|
||||||
|
Ok(Value::from("spawned worker"))
|
||||||
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/// TODO better name for this file
|
/// TODO better name for this file
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::{sync::{Arc, RwLock}, collections::BTreeMap};
|
||||||
use tracing::{error, warn};
|
use tracing::{error, warn};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
@ -10,12 +10,14 @@ use crate::{
|
||||||
tonic::{transport::Channel, Status, Streaming},
|
tonic::{transport::Channel, Status, Streaming},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub type FactoryStore = Arc<RwLock<BTreeMap<String, Arc<AsyncFactory>>>>;
|
||||||
|
|
||||||
impl From::<BufferClient<Channel>> for CodempClient {
|
impl From::<BufferClient<Channel>> for CodempClient {
|
||||||
fn from(x: BufferClient<Channel>) -> CodempClient {
|
fn from(x: BufferClient<Channel>) -> CodempClient {
|
||||||
CodempClient {
|
CodempClient {
|
||||||
id: Uuid::new_v4(),
|
id: Uuid::new_v4(),
|
||||||
client:x,
|
client:x,
|
||||||
factory: Arc::new(AsyncFactory::new(None)),
|
factories: Arc::new(RwLock::new(BTreeMap::new())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,85 +26,85 @@ impl From::<BufferClient<Channel>> for CodempClient {
|
||||||
pub struct CodempClient {
|
pub struct CodempClient {
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
client: BufferClient<Channel>,
|
client: BufferClient<Channel>,
|
||||||
factory: Arc<AsyncFactory>,
|
factories: FactoryStore,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CodempClient {
|
impl CodempClient {
|
||||||
pub async fn create(&mut self, path: String, content: Option<String>) -> Result<bool, Status> {
|
fn get_factory(&self, path: &String) -> Result<Arc<AsyncFactory>, Status> {
|
||||||
Ok(
|
match self.factories.read().unwrap().get(path) {
|
||||||
self.client.create(
|
Some(f) => Ok(f.clone()),
|
||||||
BufferPayload {
|
None => Err(Status::not_found("no active buffer for given path")),
|
||||||
path,
|
|
||||||
content,
|
|
||||||
user: self.id.to_string(),
|
|
||||||
}
|
}
|
||||||
)
|
}
|
||||||
.await?
|
|
||||||
.into_inner()
|
pub fn add_factory(&self, path: String, factory:Arc<AsyncFactory>) {
|
||||||
.accepted
|
self.factories.write().unwrap().insert(path, factory);
|
||||||
)
|
}
|
||||||
|
|
||||||
|
pub async fn create(&mut self, path: String, content: Option<String>) -> Result<bool, Status> {
|
||||||
|
let req = BufferPayload {
|
||||||
|
path: path.clone(),
|
||||||
|
content: content.clone(),
|
||||||
|
user: self.id.to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let res = self.client.create(req).await?.into_inner();
|
||||||
|
|
||||||
|
Ok(res.accepted)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn insert(&mut self, path: String, txt: String, pos: u64) -> Result<bool, Status> {
|
pub async fn insert(&mut self, path: String, txt: String, pos: u64) -> Result<bool, Status> {
|
||||||
match self.factory.insert(txt, pos).await {
|
let factory = self.get_factory(&path)?;
|
||||||
|
match factory.insert(txt, pos).await {
|
||||||
|
Err(e) => Err(Status::internal(format!("invalid operation: {}", e))),
|
||||||
Ok(op) => {
|
Ok(op) => {
|
||||||
Ok(
|
let req = OperationRequest {
|
||||||
self.client.edit(
|
|
||||||
OperationRequest {
|
|
||||||
path,
|
path,
|
||||||
hash: "".into(),
|
hash: "".into(),
|
||||||
opseq: serde_json::to_string(&op).unwrap(),
|
|
||||||
user: self.id.to_string(),
|
user: self.id.to_string(),
|
||||||
}
|
opseq: serde_json::to_string(&op)
|
||||||
)
|
.map_err(|_| Status::invalid_argument("could not serialize opseq"))?,
|
||||||
.await?
|
};
|
||||||
.into_inner()
|
let res = self.client.edit(req).await?.into_inner();
|
||||||
.accepted
|
Ok(res.accepted)
|
||||||
)
|
|
||||||
},
|
},
|
||||||
Err(e) => Err(Status::internal(format!("invalid operation: {}", e))),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete(&mut self, path: String, pos: u64, count: u64) -> Result<bool, Status> {
|
pub async fn delete(&mut self, path: String, pos: u64, count: u64) -> Result<bool, Status> {
|
||||||
match self.factory.delete(pos, count).await {
|
let factory = self.get_factory(&path)?;
|
||||||
|
match factory.delete(pos, count).await {
|
||||||
|
Err(e) => Err(Status::internal(format!("invalid operation: {}", e))),
|
||||||
Ok(op) => {
|
Ok(op) => {
|
||||||
Ok(
|
let req = OperationRequest {
|
||||||
self.client.edit(
|
|
||||||
OperationRequest {
|
|
||||||
path,
|
path,
|
||||||
hash: "".into(),
|
hash: "".into(),
|
||||||
opseq: serde_json::to_string(&op).unwrap(),
|
|
||||||
user: self.id.to_string(),
|
user: self.id.to_string(),
|
||||||
}
|
opseq: serde_json::to_string(&op)
|
||||||
)
|
.map_err(|_| Status::invalid_argument("could not serialize opseq"))?,
|
||||||
.await?
|
};
|
||||||
.into_inner()
|
let res = self.client.edit(req).await?.into_inner();
|
||||||
.accepted
|
Ok(res.accepted)
|
||||||
)
|
|
||||||
},
|
},
|
||||||
Err(e) => Err(Status::internal(format!("invalid operation: {}", e))),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn attach<F : Fn(String) -> () + Send + 'static>(&mut self, path: String, callback: F) -> Result<(), Status> {
|
pub async fn attach<F>(&mut self, path: String, callback: F) -> Result<String, Status>
|
||||||
let stream = self.client.attach(
|
where F : Fn(String) -> () + Send + 'static {
|
||||||
BufferPayload {
|
let content = self.sync(path.clone()).await?;
|
||||||
|
let factory = Arc::new(AsyncFactory::new(Some(content.clone())));
|
||||||
|
self.add_factory(path.clone(), factory.clone());
|
||||||
|
let req = BufferPayload {
|
||||||
path,
|
path,
|
||||||
content: None,
|
content: None,
|
||||||
user: self.id.to_string(),
|
user: self.id.to_string(),
|
||||||
}
|
};
|
||||||
)
|
let stream = self.client.attach(req).await?.into_inner();
|
||||||
.await?
|
|
||||||
.into_inner();
|
|
||||||
|
|
||||||
let factory = self.factory.clone();
|
|
||||||
tokio::spawn(async move { Self::worker(stream, factory, callback).await } );
|
tokio::spawn(async move { Self::worker(stream, factory, callback).await } );
|
||||||
|
Ok(content)
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn sync(&mut self, path: String) -> Result<String, Status> {
|
async fn sync(&mut self, path: String) -> Result<String, Status> {
|
||||||
let res = self.client.sync(
|
let res = self.client.sync(
|
||||||
BufferPayload {
|
BufferPayload {
|
||||||
path, content: None, user: self.id.to_string(),
|
path, content: None, user: self.id.to_string(),
|
||||||
|
@ -111,21 +113,20 @@ impl CodempClient {
|
||||||
Ok(res.into_inner().content.unwrap_or("".into()))
|
Ok(res.into_inner().content.unwrap_or("".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn worker<F : Fn(String) -> ()>(mut stream: Streaming<RawOp>, factory: Arc<AsyncFactory>, callback: F) {
|
async fn worker<F>(mut stream: Streaming<RawOp>, factory: Arc<AsyncFactory>, callback: F)
|
||||||
|
where F : Fn(String) -> () {
|
||||||
loop {
|
loop {
|
||||||
match stream.message().await {
|
match stream.message().await {
|
||||||
Err(e) => break error!("error receiving change: {}", e),
|
Err(e) => break error!("error receiving change: {}", e),
|
||||||
Ok(v) => match v {
|
Ok(v) => match v {
|
||||||
None => break warn!("stream closed"),
|
None => break warn!("stream closed"),
|
||||||
Some(operation) => {
|
Some(operation) => match serde_json::from_str(&operation.opseq) {
|
||||||
match serde_json::from_str(&operation.opseq) {
|
|
||||||
Err(e) => break error!("could not deserialize opseq: {}", e),
|
Err(e) => break error!("could not deserialize opseq: {}", e),
|
||||||
Ok(op) => match factory.process(op).await {
|
Ok(op) => match factory.process(op).await {
|
||||||
Err(e) => break error!("desynched: {}", e),
|
Err(e) => break error!("desynched: {}", e),
|
||||||
Ok(x) => callback(x),
|
Ok(x) => callback(x),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue