Finished implementing core API

Co-authored-by: alemi <me@alemi.dev>
This commit is contained in:
frelodev 2023-08-20 00:48:35 +02:00
parent 7f9422103a
commit 9929aa753a

View file

@ -21,8 +21,8 @@ impl From::<JsCodempError> for napi::Error {
} }
#[napi] #[napi]
pub fn connect(addr: String) -> napi::Result<()> { pub async fn connect(addr: String) -> napi::Result<()> {
CODEMP_INSTANCE.connect(&addr) CODEMP_INSTANCE.connect(&addr).await
.map_err(|e| JsCodempError(e).into()) .map_err(|e| JsCodempError(e).into())
} }
@ -31,8 +31,8 @@ pub fn connect(addr: String) -> napi::Result<()> {
/// CURSOR /// CURSOR
#[napi] #[napi]
pub fn join(session: String) -> napi::Result<JsCursorController> { pub async fn join(session: String) -> napi::Result<JsCursorController> {
let controller = CODEMP_INSTANCE.join(&session) let controller = CODEMP_INSTANCE.join(&session).await
.map_err(|e| napi::Error::from(JsCodempError(e)))?; .map_err(|e| napi::Error::from(JsCodempError(e)))?;
Ok(controller.into()) Ok(controller.into())
} }
@ -59,9 +59,9 @@ impl JsCursorController {
} }
#[napi] #[napi]
pub async fn send(&self, buffer: String, start: (i32, i32), end: (i32, i32)) -> napi::Result<()> { pub fn send(&self, buffer: String, start: (i32, i32), end: (i32, i32)) -> napi::Result<()> {
let pos = CodempCursorPosition { buffer, start: Some(RowCol::from(start)), end: Some(RowCol::from(end)) }; let pos = CodempCursorPosition { buffer, start: Some(RowCol::from(start)), end: Some(RowCol::from(end)) };
self.0.send(pos).await self.0.send(pos)
.map_err(|e| napi::Error::from(JsCodempError(e))) .map_err(|e| napi::Error::from(JsCodempError(e)))
} }
} }
@ -124,6 +124,15 @@ impl From::<CodempTextChange> for JsTextChange {
} }
} }
impl From::<OperationSeq> for JsCodempOperationSeq{
fn from(value: OperationSeq) -> Self {
JsCodempOperationSeq(value)
}
}
impl From::<Arc<CodempBufferController>> for JsBufferController { impl From::<Arc<CodempBufferController>> for JsBufferController {
fn from(value: Arc<CodempBufferController>) -> Self { fn from(value: Arc<CodempBufferController>) -> Self {
JsBufferController(value) JsBufferController(value)
@ -134,10 +143,22 @@ impl From::<Arc<CodempBufferController>> for JsBufferController {
#[napi] #[napi]
pub struct JsBufferController(Arc<CodempBufferController>); pub struct JsBufferController(Arc<CodempBufferController>);
#[napi(js_name = "CodempOperationSeq")]
pub struct JsCodempOperationSeq(CodempOperationSeq);
#[napi] #[napi]
impl JsBufferController { impl JsBufferController {
#[napi]
pub fn delta(&self, start: i64, txt: String, end: i64) -> Option<JsCodempOperationSeq> {
self.0.delta(start as usize, &txt, end as usize).map(|x| x.into())
}
#[napi] #[napi]
pub async fn recv(&self) -> napi::Result<JsTextChange> { pub async fn recv(&self) -> napi::Result<JsTextChange> {
Ok( Ok(
@ -147,9 +168,10 @@ impl JsBufferController {
) )
} }
//#[napi] #[napi]
pub async fn send(&self, op: OperationSeq) -> napi::Result<()> { pub fn send(&self, op: &JsCodempOperationSeq) -> napi::Result<()> {
self.0.send(op).await // TODO might be nice to take ownership of the opseq
self.0.send(op.0.clone())
.map_err(|e| napi::Error::from(JsCodempError(e))) .map_err(|e| napi::Error::from(JsCodempError(e)))
} }
@ -157,15 +179,15 @@ impl JsBufferController {
} }
#[napi] #[napi]
pub fn create(path: String, content: Option<String>) -> napi::Result<()> { pub async fn create(path: String, content: Option<String>) -> napi::Result<()> {
CODEMP_INSTANCE.create(&path, content.as_deref()) CODEMP_INSTANCE.create(&path, content.as_deref()).await
.map_err(|e| napi::Error::from(JsCodempError(e))) .map_err(|e| napi::Error::from(JsCodempError(e)))
} }
#[napi] #[napi]
pub fn attach(path: String) -> napi::Result<JsBufferController> { pub async fn attach(path: String) -> napi::Result<JsBufferController> {
Ok( Ok(
CODEMP_INSTANCE.attach(&path) CODEMP_INSTANCE.attach(&path).await
.map_err(|e| napi::Error::from(JsCodempError(e)))? .map_err(|e| napi::Error::from(JsCodempError(e)))?
.into() .into()
) )