mirror of
https://github.com/hexedtech/codemp-vscode.git
synced 2024-11-25 00:44:48 +01:00
chore: cleaned up server and lib after split
This commit is contained in:
parent
9a1d84bc64
commit
74faca0f25
8 changed files with 128 additions and 113 deletions
|
@ -1,17 +1,16 @@
|
||||||
use std::{pin::Pin, sync::{Arc, RwLock}, collections::HashMap};
|
use std::{pin::Pin, sync::{Arc, RwLock}, collections::HashMap};
|
||||||
|
|
||||||
use tokio::sync::{mpsc, broadcast, oneshot};
|
use tokio::sync::{mpsc, oneshot};
|
||||||
use tonic::{Request, Response, Status};
|
use tonic::{Request, Response, Status};
|
||||||
|
|
||||||
use tokio_stream::{Stream, wrappers::ReceiverStream}; // TODO example used this?
|
use tokio_stream::{Stream, wrappers::ReceiverStream}; // TODO example used this?
|
||||||
|
|
||||||
use codemp::proto::{buffer_server::{Buffer, BufferServer}, RawOp, BufferPayload, BufferResponse, OperationRequest, Cursor};
|
use codemp::proto::{buffer_server::Buffer, RawOp, BufferPayload, BufferResponse, OperationRequest, BufferEditResponse, BufferCreateResponse};
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
|
|
||||||
use super::actor::{BufferHandle, BufferStore};
|
use super::actor::{BufferHandle, BufferStore};
|
||||||
|
|
||||||
type OperationStream = Pin<Box<dyn Stream<Item = Result<RawOp, Status>> + Send>>;
|
type OperationStream = Pin<Box<dyn Stream<Item = Result<RawOp, Status>> + Send>>;
|
||||||
type CursorStream = Pin<Box<dyn Stream<Item = Result<Cursor, Status>> + Send>>;
|
|
||||||
|
|
||||||
struct BufferMap {
|
struct BufferMap {
|
||||||
store: HashMap<String, BufferHandle>,
|
store: HashMap<String, BufferHandle>,
|
||||||
|
@ -34,15 +33,12 @@ impl BufferStore<String> for BufferMap {
|
||||||
|
|
||||||
pub struct BufferService {
|
pub struct BufferService {
|
||||||
map: Arc<RwLock<BufferMap>>,
|
map: Arc<RwLock<BufferMap>>,
|
||||||
cursor: broadcast::Sender<Cursor>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BufferService {
|
impl Default for BufferService {
|
||||||
#[allow(unused)]
|
fn default() -> BufferService {
|
||||||
fn get_buffer(&self, path: &String) -> Result<BufferHandle, Status> {
|
BufferService {
|
||||||
match self.map.read().unwrap().get(path) {
|
map: Arc::new(RwLock::new(HashMap::new().into())),
|
||||||
Some(buf) => Ok(buf.clone()),
|
|
||||||
None => Err(Status::not_found("no buffer for given path")),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,7 +46,6 @@ impl BufferService {
|
||||||
#[tonic::async_trait]
|
#[tonic::async_trait]
|
||||||
impl Buffer for BufferService {
|
impl Buffer for BufferService {
|
||||||
type AttachStream = OperationStream;
|
type AttachStream = OperationStream;
|
||||||
type ListenStream = CursorStream;
|
|
||||||
|
|
||||||
async fn attach(&self, req: Request<BufferPayload>) -> Result<Response<OperationStream>, Status> {
|
async fn attach(&self, req: Request<BufferPayload>) -> Result<Response<OperationStream>, Status> {
|
||||||
let request = req.into_inner();
|
let request = req.into_inner();
|
||||||
|
@ -73,29 +68,7 @@ impl Buffer for BufferService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn listen(&self, req: Request<BufferPayload>) -> Result<Response<CursorStream>, Status> {
|
async fn edit(&self, req:Request<OperationRequest>) -> Result<Response<BufferEditResponse>, Status> {
|
||||||
let mut sub = self.cursor.subscribe();
|
|
||||||
let myself = req.into_inner().user;
|
|
||||||
let (tx, rx) = mpsc::channel(128);
|
|
||||||
tokio::spawn(async move {
|
|
||||||
while let Ok(v) = sub.recv().await {
|
|
||||||
if v.user == myself { continue }
|
|
||||||
tx.send(Ok(v)).await.unwrap(); // TODO unnecessary channel?
|
|
||||||
}
|
|
||||||
});
|
|
||||||
let output_stream = ReceiverStream::new(rx);
|
|
||||||
info!("registered new subscriber to cursor updates");
|
|
||||||
Ok(Response::new(Box::pin(output_stream)))
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn moved(&self, req:Request<Cursor>) -> Result<Response<BufferResponse>, Status> {
|
|
||||||
match self.cursor.send(req.into_inner()) {
|
|
||||||
Ok(_) => Ok(Response::new(BufferResponse { accepted: true, content: None})),
|
|
||||||
Err(e) => Err(Status::internal(format!("could not broadcast cursor update: {}", e))),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn edit(&self, req:Request<OperationRequest>) -> Result<Response<BufferResponse>, Status> {
|
|
||||||
let request = req.into_inner();
|
let request = req.into_inner();
|
||||||
let tx = match self.map.read().unwrap().get(&request.path) {
|
let tx = match self.map.read().unwrap().get(&request.path) {
|
||||||
Some(handle) => {
|
Some(handle) => {
|
||||||
|
@ -110,19 +83,20 @@ impl Buffer for BufferService {
|
||||||
let (ack, status) = oneshot::channel();
|
let (ack, status) = oneshot::channel();
|
||||||
match tx.send((ack, request)).await {
|
match tx.send((ack, request)).await {
|
||||||
Err(e) => Err(Status::internal(format!("error sending edit to buffer actor: {}", e))),
|
Err(e) => Err(Status::internal(format!("error sending edit to buffer actor: {}", e))),
|
||||||
Ok(()) => Ok(Response::new(BufferResponse {
|
Ok(()) => {
|
||||||
accepted: status.await.unwrap_or(false),
|
match status.await {
|
||||||
content: None
|
Ok(_accepted) => Ok(Response::new(BufferEditResponse { })),
|
||||||
}))
|
Err(e) => Err(Status::internal(format!("error receiving edit result: {}", e))),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn create(&self, req:Request<BufferPayload>) -> Result<Response<BufferResponse>, Status> {
|
async fn create(&self, req:Request<BufferPayload>) -> Result<Response<BufferCreateResponse>, Status> {
|
||||||
let request = req.into_inner();
|
let request = req.into_inner();
|
||||||
let _handle = self.map.write().unwrap().handle(request.path, request.content);
|
let _handle = self.map.write().unwrap().handle(request.path, request.content);
|
||||||
info!("created new buffer");
|
info!("created new buffer");
|
||||||
let answ = BufferResponse { accepted: true, content: None };
|
Ok(Response::new(BufferCreateResponse { }))
|
||||||
Ok(Response::new(answ))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn sync(&self, req: Request<BufferPayload>) -> Result<Response<BufferResponse>, Status> {
|
async fn sync(&self, req: Request<BufferPayload>) -> Result<Response<BufferResponse>, Status> {
|
||||||
|
@ -131,23 +105,9 @@ impl Buffer for BufferService {
|
||||||
None => Err(Status::not_found("requested buffer does not exist")),
|
None => Err(Status::not_found("requested buffer does not exist")),
|
||||||
Some(buf) => {
|
Some(buf) => {
|
||||||
info!("synching buffer");
|
info!("synching buffer");
|
||||||
let answ = BufferResponse { accepted: true, content: Some(buf.content.borrow().clone()) };
|
let answ = BufferResponse { content: buf.content.borrow().clone() };
|
||||||
Ok(Response::new(answ))
|
Ok(Response::new(answ))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BufferService {
|
|
||||||
pub fn new() -> BufferService {
|
|
||||||
let (cur_tx, _cur_rx) = broadcast::channel(64); // TODO hardcoded capacity
|
|
||||||
BufferService {
|
|
||||||
map: Arc::new(RwLock::new(HashMap::new().into())),
|
|
||||||
cursor: cur_tx,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn server(self) -> BufferServer<BufferService> {
|
|
||||||
BufferServer::new(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
1
server/src/cursor/mod.rs
Normal file
1
server/src/cursor/mod.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub mod service;
|
52
server/src/cursor/service.rs
Normal file
52
server/src/cursor/service.rs
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
use std::pin::Pin;
|
||||||
|
|
||||||
|
use tokio::sync::{mpsc, broadcast};
|
||||||
|
use tonic::{Request, Response, Status};
|
||||||
|
|
||||||
|
use tokio_stream::{Stream, wrappers::ReceiverStream}; // TODO example used this?
|
||||||
|
|
||||||
|
use codemp::proto::{cursor_server::Cursor, UserIdentity, CursorPosition, MovedResponse};
|
||||||
|
use tracing::info;
|
||||||
|
|
||||||
|
type CursorStream = Pin<Box<dyn Stream<Item = Result<CursorPosition, Status>> + Send>>;
|
||||||
|
|
||||||
|
pub struct CursorService {
|
||||||
|
cursor: broadcast::Sender<CursorPosition>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tonic::async_trait]
|
||||||
|
impl Cursor for CursorService {
|
||||||
|
type ListenStream = CursorStream;
|
||||||
|
|
||||||
|
async fn listen(&self, req: Request<UserIdentity>) -> Result<Response<CursorStream>, Status> {
|
||||||
|
let mut sub = self.cursor.subscribe();
|
||||||
|
let myself = req.into_inner().id;
|
||||||
|
let (tx, rx) = mpsc::channel(128);
|
||||||
|
tokio::spawn(async move {
|
||||||
|
while let Ok(v) = sub.recv().await {
|
||||||
|
if v.user == myself { continue }
|
||||||
|
tx.send(Ok(v)).await.unwrap(); // TODO unnecessary channel?
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let output_stream = ReceiverStream::new(rx);
|
||||||
|
info!("registered new subscriber to cursor updates");
|
||||||
|
Ok(Response::new(Box::pin(output_stream)))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn moved(&self, req:Request<CursorPosition>) -> Result<Response<MovedResponse>, Status> {
|
||||||
|
match self.cursor.send(req.into_inner()) {
|
||||||
|
Ok(_) => Ok(Response::new(MovedResponse { })),
|
||||||
|
Err(e) => Err(Status::internal(format!("could not broadcast cursor update: {}", e))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for CursorService {
|
||||||
|
fn default() -> Self {
|
||||||
|
let (cur_tx, _cur_rx) = broadcast::channel(64); // TODO hardcoded capacity
|
||||||
|
// TODO don't drop receiver because sending event when there are no receivers throws an error
|
||||||
|
CursorService {
|
||||||
|
cursor: cur_tx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,12 +5,16 @@
|
||||||
//!
|
//!
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use codemp::proto::buffer_server::BufferServer;
|
||||||
|
use codemp::proto::cursor_server::CursorServer;
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
use tonic::transport::Server;
|
use tonic::transport::Server;
|
||||||
|
|
||||||
mod buffer;
|
mod buffer;
|
||||||
|
mod cursor;
|
||||||
|
|
||||||
use crate::buffer::service::BufferService;
|
use crate::buffer::service::BufferService;
|
||||||
|
use crate::cursor::service::CursorService;
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
struct CliArgs {
|
struct CliArgs {
|
||||||
|
@ -37,7 +41,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
info!("binding on {}", args.host);
|
info!("binding on {}", args.host);
|
||||||
|
|
||||||
Server::builder()
|
Server::builder()
|
||||||
.add_service(BufferService::new().server())
|
.add_service(BufferServer::new(BufferService::default()))
|
||||||
|
.add_service(CursorServer::new(CursorService::default()))
|
||||||
.serve(args.host.parse()?)
|
.serve(args.host.parse()?)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,6 @@ use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ControllerWorker,
|
ControllerWorker,
|
||||||
cursor::tracker::{CursorTracker, CursorTrackerWorker},
|
|
||||||
buffer::handle::{BufferHandle, OperationControllerEditor, OperationControllerWorker},
|
buffer::handle::{BufferHandle, OperationControllerEditor, OperationControllerWorker},
|
||||||
proto::{buffer_client::BufferClient, BufferPayload, RawOp, OperationRequest},
|
proto::{buffer_client::BufferClient, BufferPayload, RawOp, OperationRequest},
|
||||||
};
|
};
|
||||||
|
@ -28,39 +27,39 @@ impl BufferController {
|
||||||
|
|
||||||
pub fn id(&self) -> &str { &self.id }
|
pub fn id(&self) -> &str { &self.id }
|
||||||
|
|
||||||
pub async fn create(&mut self, path: &str, content: Option<&str>) -> Result<bool, Status> {
|
pub async fn create(&mut self, path: &str, content: Option<&str>) -> Result<(), Status> {
|
||||||
let req = BufferPayload {
|
let req = BufferPayload {
|
||||||
path: path.to_string(),
|
path: path.to_string(),
|
||||||
content: content.map(|x| x.to_string()),
|
content: content.map(|x| x.to_string()),
|
||||||
user: self.id.clone(),
|
user: self.id.clone(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let res = self.client.create(req).await?;
|
self.client.create(req).await?;
|
||||||
|
|
||||||
Ok(res.into_inner().accepted)
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn listen(&mut self) -> Result<CursorTracker, Status> {
|
// pub async fn listen(&mut self) -> Result<CursorTracker, Status> {
|
||||||
let req = BufferPayload {
|
// let req = BufferPayload {
|
||||||
path: "".into(),
|
// path: "".into(),
|
||||||
content: None,
|
// content: None,
|
||||||
user: self.id.clone(),
|
// user: self.id.clone(),
|
||||||
};
|
// };
|
||||||
|
|
||||||
let stream = self.client.listen(req).await?.into_inner();
|
// let stream = self.client.listen(req).await?.into_inner();
|
||||||
|
|
||||||
let controller = CursorTrackerWorker::new(self.id().to_string());
|
// let controller = CursorTrackerWorker::new(self.id().to_string());
|
||||||
let handle = controller.subscribe();
|
// let handle = controller.subscribe();
|
||||||
let client = self.client.clone();
|
// let client = self.client.clone();
|
||||||
|
|
||||||
tokio::spawn(async move {
|
// tokio::spawn(async move {
|
||||||
tracing::debug!("cursor worker started");
|
// tracing::debug!("cursor worker started");
|
||||||
controller.work(stream, client).await;
|
// controller.work(stream, client).await;
|
||||||
tracing::debug!("cursor worker stopped");
|
// tracing::debug!("cursor worker stopped");
|
||||||
});
|
// });
|
||||||
|
|
||||||
Ok(handle)
|
// Ok(handle)
|
||||||
}
|
// }
|
||||||
|
|
||||||
pub async fn attach(&mut self, path: &str) -> Result<BufferHandle, Status> {
|
pub async fn attach(&mut self, path: &str) -> Result<BufferHandle, Status> {
|
||||||
let req = BufferPayload {
|
let req = BufferPayload {
|
||||||
|
@ -72,8 +71,7 @@ impl BufferController {
|
||||||
let content = self.client.sync(req.clone())
|
let content = self.client.sync(req.clone())
|
||||||
.await?
|
.await?
|
||||||
.into_inner()
|
.into_inner()
|
||||||
.content
|
.content;
|
||||||
.unwrap_or("".into());
|
|
||||||
|
|
||||||
let stream = self.client.attach(req).await?.into_inner();
|
let stream = self.client.attach(req).await?.into_inner();
|
||||||
|
|
||||||
|
@ -100,7 +98,7 @@ impl OperationControllerEditor for (BufferController, Streaming<RawOp>) {
|
||||||
user: self.0.id().to_string(),
|
user: self.0.id().to_string(),
|
||||||
};
|
};
|
||||||
match self.0.client.edit(req).await {
|
match self.0.client.edit(req).await {
|
||||||
Ok(res) => res.into_inner().accepted,
|
Ok(_) => true,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
tracing::error!("error sending edit: {}", e);
|
tracing::error!("error sending edit: {}", e);
|
||||||
false
|
false
|
||||||
|
|
|
@ -1,25 +1,25 @@
|
||||||
pub mod tracker;
|
pub mod tracker;
|
||||||
|
|
||||||
use crate::proto::{Position, Cursor};
|
use crate::proto::{RowColumn, CursorPosition};
|
||||||
|
|
||||||
impl From::<Position> for (i32, i32) {
|
impl From::<RowColumn> for (i32, i32) {
|
||||||
fn from(pos: Position) -> (i32, i32) {
|
fn from(pos: RowColumn) -> (i32, i32) {
|
||||||
(pos.row, pos.col)
|
(pos.row, pos.col)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From::<(i32, i32)> for Position {
|
impl From::<(i32, i32)> for RowColumn {
|
||||||
fn from((row, col): (i32, i32)) -> Self {
|
fn from((row, col): (i32, i32)) -> Self {
|
||||||
Position { row, col }
|
RowColumn { row, col }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Cursor {
|
impl CursorPosition {
|
||||||
pub fn start(&self) -> Position {
|
pub fn start(&self) -> RowColumn {
|
||||||
self.start.clone().unwrap_or((0, 0).into())
|
self.start.clone().unwrap_or((0, 0).into())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn end(&self) -> Position {
|
pub fn end(&self) -> RowColumn {
|
||||||
self.end.clone().unwrap_or((0, 0).into())
|
self.end.clone().unwrap_or((0, 0).into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,17 +3,17 @@ use std::sync::Arc;
|
||||||
use tokio::sync::{mpsc, broadcast::{self, error::RecvError}, Mutex};
|
use tokio::sync::{mpsc, broadcast::{self, error::RecvError}, Mutex};
|
||||||
use tonic::{Streaming, transport::Channel};
|
use tonic::{Streaming, transport::Channel};
|
||||||
|
|
||||||
use crate::{proto::{Position, Cursor, buffer_client::BufferClient}, errors::IgnorableError, CodempError};
|
use crate::{proto::{RowColumn, CursorPosition, buffer_client::BufferClient}, errors::IgnorableError, CodempError};
|
||||||
|
|
||||||
pub struct CursorTracker {
|
pub struct CursorTracker {
|
||||||
uid: String,
|
uid: String,
|
||||||
op: mpsc::Sender<Cursor>,
|
op: mpsc::Sender<CursorPosition>,
|
||||||
stream: Mutex<broadcast::Receiver<Cursor>>,
|
stream: Mutex<broadcast::Receiver<CursorPosition>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CursorTracker {
|
impl CursorTracker {
|
||||||
pub async fn moved(&self, path: &str, start: Position, end: Position) -> Result<(), CodempError> {
|
pub async fn moved(&self, path: &str, start: RowColumn, end: RowColumn) -> Result<(), CodempError> {
|
||||||
Ok(self.op.send(Cursor {
|
Ok(self.op.send(CursorPosition {
|
||||||
user: self.uid.clone(),
|
user: self.uid.clone(),
|
||||||
buffer: path.to_string(),
|
buffer: path.to_string(),
|
||||||
start: start.into(),
|
start: start.into(),
|
||||||
|
@ -23,7 +23,7 @@ impl CursorTracker {
|
||||||
|
|
||||||
// TODO is this cancelable? so it can be used in tokio::select!
|
// TODO is this cancelable? so it can be used in tokio::select!
|
||||||
// TODO is the result type overkill? should be an option?
|
// TODO is the result type overkill? should be an option?
|
||||||
pub async fn recv(&self) -> Result<Cursor, CodempError> {
|
pub async fn recv(&self) -> Result<CursorPosition, CodempError> {
|
||||||
let mut stream = self.stream.lock().await;
|
let mut stream = self.stream.lock().await;
|
||||||
match stream.recv().await {
|
match stream.recv().await {
|
||||||
Ok(x) => Ok(x),
|
Ok(x) => Ok(x),
|
||||||
|
@ -35,7 +35,7 @@ impl CursorTracker {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// fn try_poll(&self) -> Option<Option<Cursor>> {
|
// fn try_poll(&self) -> Option<Option<CursorPosition>> {
|
||||||
// match self.stream.try_lock() {
|
// match self.stream.try_lock() {
|
||||||
// Err(_) => None,
|
// Err(_) => None,
|
||||||
// Ok(mut x) => match x.try_recv() {
|
// Ok(mut x) => match x.try_recv() {
|
||||||
|
@ -51,14 +51,14 @@ impl CursorTracker {
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct CursorTrackerWorker {
|
pub(crate) struct CursorPositionTrackerWorker {
|
||||||
uid: String,
|
uid: String,
|
||||||
producer: mpsc::Sender<Cursor>,
|
producer: mpsc::Sender<CursorPosition>,
|
||||||
op: mpsc::Receiver<Cursor>,
|
op: mpsc::Receiver<CursorPosition>,
|
||||||
channel: Arc<broadcast::Sender<Cursor>>,
|
channel: Arc<broadcast::Sender<CursorPosition>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CursorTrackerWorker {
|
impl CursorPositionTrackerWorker {
|
||||||
pub(crate) fn new(uid: String) -> Self {
|
pub(crate) fn new(uid: String) -> Self {
|
||||||
let (op_tx, op_rx) = mpsc::channel(64);
|
let (op_tx, op_rx) = mpsc::channel(64);
|
||||||
let (cur_tx, _cur_rx) = broadcast::channel(64);
|
let (cur_tx, _cur_rx) = broadcast::channel(64);
|
||||||
|
@ -79,11 +79,11 @@ impl CursorTrackerWorker {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO is it possible to avoid passing directly tonic Streaming and proto BufferClient ?
|
// TODO is it possible to avoid passing directly tonic Streaming and proto BufferClient ?
|
||||||
pub(crate) async fn work(mut self, mut rx: Streaming<Cursor>, mut tx: BufferClient<Channel>) {
|
pub(crate) async fn work(mut self, mut rx: Streaming<CursorPosition>, mut tx: BufferClient<Channel>) {
|
||||||
loop {
|
loop {
|
||||||
tokio::select!{
|
tokio::select!{
|
||||||
Ok(Some(cur)) = rx.message() => self.channel.send(cur).unwrap_or_warn("could not broadcast event"),
|
Ok(Some(cur)) = rx.message() => self.channel.send(cur).unwrap_or_warn("could not broadcast event"),
|
||||||
Some(op) = self.op.recv() => { tx.moved(op).await.unwrap_or_warn("could not update cursor"); },
|
Some(op) = self.op.recv() => { todo!() } // tx.moved(op).await.unwrap_or_warn("could not update cursor"); },
|
||||||
else => break,
|
else => break,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
19
src/state.rs
19
src/state.rs
|
@ -4,7 +4,6 @@ use tokio::sync::RwLock;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
buffer::{controller::BufferController, handle::BufferHandle},
|
buffer::{controller::BufferController, handle::BufferHandle},
|
||||||
cursor::tracker::CursorTracker,
|
|
||||||
errors::CodempError,
|
errors::CodempError,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -25,35 +24,35 @@ pub mod instance {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Workspace {
|
pub struct Workspace {
|
||||||
client: BufferController,
|
|
||||||
buffers: RwLock<BTreeMap<Box<str>, Arc<BufferHandle>>>,
|
buffers: RwLock<BTreeMap<Box<str>, Arc<BufferHandle>>>,
|
||||||
cursor: Arc<CursorTracker>,
|
// cursor: Arc<CursorTracker>,
|
||||||
|
client: BufferController,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Workspace {
|
impl Workspace {
|
||||||
pub async fn new(dest: &str) -> Result<Self, CodempError> {
|
pub async fn new(dest: &str) -> Result<Self, CodempError> {
|
||||||
let mut client = BufferController::new(dest).await?;
|
let client = BufferController::new(dest).await?;
|
||||||
let cursor = Arc::new(client.listen().await?);
|
// let cursor = Arc::new(client.listen().await?);
|
||||||
Ok(
|
Ok(
|
||||||
Workspace {
|
Workspace {
|
||||||
buffers: RwLock::new(BTreeMap::new()),
|
buffers: RwLock::new(BTreeMap::new()),
|
||||||
cursor,
|
// cursor,
|
||||||
client,
|
client,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cursor
|
// Cursor
|
||||||
pub async fn cursor(&self) -> Arc<CursorTracker> {
|
// pub async fn cursor(&self) -> Arc<CursorTracker> {
|
||||||
self.cursor.clone()
|
// self.cursor.clone()
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Buffer
|
// Buffer
|
||||||
pub async fn buffer(&self, path: &str) -> Option<Arc<BufferHandle>> {
|
pub async fn buffer(&self, path: &str) -> Option<Arc<BufferHandle>> {
|
||||||
self.buffers.read().await.get(path).cloned()
|
self.buffers.read().await.get(path).cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create(&self, path: &str, content: Option<&str>) -> Result<bool, CodempError> {
|
pub async fn create(&self, path: &str, content: Option<&str>) -> Result<(), CodempError> {
|
||||||
Ok(self.client.clone().create(path, content).await?)
|
Ok(self.client.clone().create(path, content).await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue