From 5cddb27b98fac7be1e992ee8c46bade2049fff3f Mon Sep 17 00:00:00 2001 From: alemi Date: Sun, 30 Jul 2023 22:58:24 +0200 Subject: [PATCH] feat: initial features splitting, added singleton --- Cargo.toml | 8 +++++++- src/buffer/controller.rs | 19 +++++++++++++++---- src/cursor/client.rs | 1 + src/lib.rs | 20 ++++++++++++++------ src/{workspace/controller.rs => state.rs} | 19 +++++++++++++++++-- src/workspace/mod.rs | 1 - 6 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 src/cursor/client.rs rename src/{workspace/controller.rs => state.rs} (79%) delete mode 100644 src/workspace/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 00370e3..0e6edc6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ name = "codemp" # core tracing = "0.1" tonic = { version = "0.9", features = ["tls", "tls-roots"] } -prost = "0.11.8" +prost = { version = "0.11.8", optional = true } md5 = "0.7.0" uuid = { version = "1.3.1", features = ["v4"] } operational-transform = { version = "0.6", features = ["serde"] } @@ -23,6 +23,12 @@ serde = { version = "1", optional = false } serde_json = { version = "1", optional = false } tracing-subscriber = { version = "0.3", optional = true } similar = { version = "2.2", features = ["inline"] } +lazy_static = { version = "1.4", optional = true } [build-dependencies] tonic-build = "0.9" + +[features] +default = ["proto", "static"] +proto = ["dep:prost"] +static = ["dep:lazy_static"] diff --git a/src/buffer/controller.rs b/src/buffer/controller.rs index f7dcd93..39cecb1 100644 --- a/src/buffer/controller.rs +++ b/src/buffer/controller.rs @@ -14,17 +14,28 @@ pub struct TextChange { } #[async_trait] -pub trait OperationControllerSubscriber { +pub trait OperationControllerSubscriber : OperationFactory { async fn poll(&self) -> Option; async fn apply(&self, op: OperationSeq); } -#[derive(Clone)] + pub struct OperationControllerHandle { content: watch::Receiver, operations: mpsc::Sender, original: Arc>, - stream: Arc>>, + stream: Mutex>, +} + +impl Clone for OperationControllerHandle { + fn clone(&self) -> Self { + OperationControllerHandle { + content: self.content.clone(), + operations: self.operations.clone(), + original: self.original.clone(), + stream: Mutex::new(self.original.subscribe()), + } + } } #[async_trait] @@ -87,7 +98,7 @@ impl ControllerWorker { +pub use errors::CodempError; + +#[tonic::async_trait] // TODO move this somewhere? +pub(crate) trait ControllerWorker { fn subscribe(&self) -> T; async fn work(self); } -pub mod proto { - tonic::include_proto!("buffer"); +#[tonic::async_trait] +pub trait Controller { + async fn recv(&self) -> Result; + async fn send(&self, x: T) -> Result<(), CodempError>; } diff --git a/src/workspace/controller.rs b/src/state.rs similarity index 79% rename from src/workspace/controller.rs rename to src/state.rs index 3b6cab4..2431ddb 100644 --- a/src/workspace/controller.rs +++ b/src/state.rs @@ -8,6 +8,21 @@ use crate::{ cursor::controller::CursorSubscriber, errors::CodempError, }; +#[cfg(feature = "static")] +pub mod instance { + use tokio::runtime::Runtime; + use super::Workspace; + + const CODEMP_DEFAULT_HOST : &str = "http://fantabos.co:50051"; + + lazy_static::lazy_static! { + static ref RUNTIME : Runtime = Runtime::new().expect("could not create tokio runtime"); + static ref WORKSPACE : Workspace = RUNTIME.block_on( + Workspace::new(&std::env::var("CODEMP_HOST").unwrap_or(CODEMP_DEFAULT_HOST.into())) + ).expect("could not create codemp workspace"); + } +} + pub struct Workspace { client: CodempClient, buffers: RwLock, BufferController>>, @@ -19,7 +34,7 @@ pub type BufferController = Arc #[async_trait] pub trait WorkspaceHandle { - fn cursor(&self) -> CursorController; + async fn cursor(&self) -> CursorController; async fn buffer(&self, path: &str) -> Option; async fn attach(&self, path: &str) -> Result<(), CodempError>; async fn create(&self, path: &str, content: Option<&str>) -> Result; @@ -42,7 +57,7 @@ impl Workspace { #[async_trait] impl WorkspaceHandle for Workspace { // Cursor - fn cursor(&self) -> CursorController { + async fn cursor(&self) -> CursorController { self.cursor.clone() } diff --git a/src/workspace/mod.rs b/src/workspace/mod.rs deleted file mode 100644 index cb9e0ac..0000000 --- a/src/workspace/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod controller;