docs: even better examples

This commit is contained in:
əlemi 2023-08-20 03:11:06 +02:00
parent 3382ea72ea
commit 5d6957c9ad

View file

@ -33,41 +33,28 @@
//! ### Async //! ### Async
//! ```no_run //! ```no_run
//! async fn async_example() -> codemp::Result<()> { //! async fn async_example() -> codemp::Result<()> {
//! // create global instance //! let session = codemp::Instance::default(); // create global session
//! let codemp = codemp::Instance::default(); //! session.connect("http://alemi.dev:50051").await?; // connect to remote server
//!
//! // connect to a remote server
//! codemp.connect("http://alemi.dev:50051").await?;
//! //!
//! // join a remote workspace, obtaining a cursor controller //! // join a remote workspace, obtaining a cursor controller
//! let cursor = codemp.join("some_workspace").await?; //! let cursor = session.join("some_workspace").await?;
//! //! cursor.send( // move cursor
//! // move cursor
//! cursor.send(
//! codemp::proto::CursorPosition { //! codemp::proto::CursorPosition {
//! buffer: "test.txt".into(), //! buffer: "test.txt".into(),
//! start: Some(codemp::proto::RowCol { row: 0, col: 0 }), //! start: Some(codemp::proto::RowCol { row: 0, col: 0 }),
//! end: Some(codemp::proto::RowCol { row: 0, col: 1 }), //! end: Some(codemp::proto::RowCol { row: 0, col: 1 }),
//! } //! }
//! )?; //! )?;
//! //! let op = cursor.recv().await?; // listen for event
//! // listen for event
//! let op = cursor.recv().await?;
//! println!("received cursor event: {:?}", op); //! println!("received cursor event: {:?}", op);
//! //!
//! // create a new buffer //! // attach to a new buffer and execute operations on it
//! codemp.create("test.txt", None).await?; //! session.create("test.txt", None).await?; // create new buffer
//! //! let buffer = session.attach("test.txt").await?; // attach to it
//! // attach to created buffer //! buffer.send(buffer.insert("hello", 0))?; // insert some text
//! let buffer = codemp.attach("test.txt").await?;
//!
//! // sending operation
//! buffer.send(buffer.insert("hello", 0))?;
//!
//! if let Some(operation) = buffer.delta(4, "o world", 5) { //! 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"); //! assert_eq!(buffer.content(), "hello world");
//! //!
//! Ok(()) //! Ok(())
@ -80,18 +67,13 @@
//! // activate feature "global" to access static CODEMP_INSTANCE //! // activate feature "global" to access static CODEMP_INSTANCE
//! use codemp::instance::global::INSTANCE; //! use codemp::instance::global::INSTANCE;
//! //!
//! fn sync_example() -> Result<()> { //! fn sync_example() -> codemp::Result<()> {
//! // connect to remote server //! INSTANCE.connect("http://alemi.dev:50051")?; // connect to server
//! INSTANCE.connect("http://alemi.dev:50051")?; //! let cursor = INSTANCE.join("some_workspace")?; // join workspace
//! //! let (stop, stop_rx) = tokio::sync::mpsc::unbounded_channel(); // create stop channel
//! // join workspace //! Arc::new(cursor).callback( // register callback
//! let cursor = INSTANCE.join("some_workspace")?; //! INSTANCE.rt(), stop_rx, // pass instance runtime and stop channel receiver
//! //! | cursor_event | {
//! let (stop, stop_rx) = tokio::sync::mpsc::unbounded_channel();
//! Arc::new(cursor).callback(
//! INSTANCE.rt(),
//! stop_rx,
//! | cursor_event | {
//! println!("received cursor event: {:?}", cursor_event); //! println!("received cursor event: {:?}", cursor_event);
//! } //! }
//! ); //! );