From 5d6957c9ad2e2d2645f59c4d2cd67dd514b2a8e3 Mon Sep 17 00:00:00 2001 From: alemi Date: Sun, 20 Aug 2023 03:11:06 +0200 Subject: [PATCH] docs: even better examples --- src/lib.rs | 52 +++++++++++++++++----------------------------------- 1 file changed, 17 insertions(+), 35 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index bc2a572..79b634f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,41 +33,28 @@ //! ### Async //! ```no_run //! async fn async_example() -> codemp::Result<()> { -//! // create global instance -//! let codemp = codemp::Instance::default(); -//! -//! // connect to a remote server -//! codemp.connect("http://alemi.dev:50051").await?; +//! let session = codemp::Instance::default(); // create global session +//! session.connect("http://alemi.dev:50051").await?; // connect to remote server //! //! // join a remote workspace, obtaining a cursor controller -//! let cursor = codemp.join("some_workspace").await?; -//! -//! // move cursor -//! cursor.send( +//! let cursor = session.join("some_workspace").await?; +//! cursor.send( // move cursor //! codemp::proto::CursorPosition { //! buffer: "test.txt".into(), //! start: Some(codemp::proto::RowCol { row: 0, col: 0 }), //! end: Some(codemp::proto::RowCol { row: 0, col: 1 }), //! } //! )?; -//! -//! // listen for event -//! let op = cursor.recv().await?; +//! let op = cursor.recv().await?; // listen for event //! println!("received cursor event: {:?}", op); //! -//! // create a new buffer -//! codemp.create("test.txt", None).await?; -//! -//! // attach to created buffer -//! let buffer = codemp.attach("test.txt").await?; -//! -//! // sending operation -//! buffer.send(buffer.insert("hello", 0))?; -//! +//! // attach to a new buffer and execute operations on it +//! session.create("test.txt", None).await?; // create new buffer +//! let buffer = session.attach("test.txt").await?; // attach to it +//! buffer.send(buffer.insert("hello", 0))?; // insert some text //! if let Some(operation) = buffer.delta(4, "o world", 5) { -//! buffer.send(operation)?; +//! buffer.send(operation)?; // replace with precision, if valid //! } -//! //! assert_eq!(buffer.content(), "hello world"); //! //! Ok(()) @@ -80,18 +67,13 @@ //! // activate feature "global" to access static CODEMP_INSTANCE //! use codemp::instance::global::INSTANCE; //! -//! fn sync_example() -> Result<()> { -//! // connect to remote server -//! INSTANCE.connect("http://alemi.dev:50051")?; -//! -//! // join workspace -//! let cursor = INSTANCE.join("some_workspace")?; -//! -//! let (stop, stop_rx) = tokio::sync::mpsc::unbounded_channel(); -//! Arc::new(cursor).callback( -//! INSTANCE.rt(), -//! stop_rx, -//! | cursor_event | { +//! fn sync_example() -> codemp::Result<()> { +//! INSTANCE.connect("http://alemi.dev:50051")?; // connect to server +//! let cursor = INSTANCE.join("some_workspace")?; // join workspace +//! let (stop, stop_rx) = tokio::sync::mpsc::unbounded_channel(); // create stop channel +//! Arc::new(cursor).callback( // register callback +//! INSTANCE.rt(), stop_rx, // pass instance runtime and stop channel receiver +//! | cursor_event | { //! println!("received cursor event: {:?}", cursor_event); //! } //! );