diff --git a/dist/js/package.json b/dist/js/package.json index 0504616..6bec29c 100644 --- a/dist/js/package.json +++ b/dist/js/package.json @@ -1,6 +1,6 @@ { "name": "@codemp/codemp", - "version": "0.0.4", + "version": "0.0.5", "dependencies": { "@napi-rs/cli": "^2.18.0", diff --git a/src/ffi/js/buffer.rs b/src/ffi/js/buffer.rs index f729951..3ac8714 100644 --- a/src/ffi/js/buffer.rs +++ b/src/ffi/js/buffer.rs @@ -7,30 +7,26 @@ use crate::buffer::controller::BufferController; #[napi] impl BufferController { - #[napi(js_name = "callback", ts_args_type = "fun: (event: TextChange) => void")] - pub fn jscallback(&self, fun: napi::JsFunction) -> napi::Result<()>{ - let tsfn : ThreadsafeFunction = + + #[napi(js_name = "callback", ts_args_type = "fun: (event: BufferController) => void")] + pub fn jscallback(&self, fun: napi::JsFunction) -> napi::Result<()>{ + let tsfn : ThreadsafeFunction = fun.create_threadsafe_function(0, - |ctx : ThreadSafeCallContext| { + |ctx : ThreadSafeCallContext| { Ok(vec![ctx.value]) } )?; - let _controller = self.clone(); - tokio::spawn(async move { - //tokio::time::sleep(std::time::Duration::from_secs(1)).await; - loop { - tokio::time::sleep(std::time::Duration::from_millis(200)).await; - match _controller.recv().await { - Ok(event) => { - tsfn.call(event, ThreadsafeFunctionCallMode::NonBlocking); //check this with tracing also we could use Ok(event) to get the error - }, - Err(crate::Error::Deadlocked) => continue, - Err(e) => break tracing::warn!("error receiving: {}", e), - } - } + self.callback(move |controller : BufferController| { + + tsfn.call(controller.clone(), ThreadsafeFunctionCallMode::Blocking); //check this with tracing also we could use Ok(event) to get the error + // If it blocks the main thread too many time we have to change this + }); + Ok(()) } + + #[napi(js_name = "get_name")] pub fn js_name(&self) -> napi::Result<&str> { Ok(&self.name()) diff --git a/src/ffi/js/cursor.rs b/src/ffi/js/cursor.rs index 4b4e024..b0f7bb6 100644 --- a/src/ffi/js/cursor.rs +++ b/src/ffi/js/cursor.rs @@ -1,5 +1,6 @@ +use napi::threadsafe_function::ErrorStrategy::Fatal; use napi_derive::napi; -use napi::threadsafe_function::{ThreadsafeFunction, ThreadSafeCallContext, ThreadsafeFunctionCallMode, ErrorStrategy}; +use napi::threadsafe_function::{ThreadsafeFunction, ThreadSafeCallContext, ThreadsafeFunctionCallMode}; use crate::api::Controller; use crate::cursor::controller::CursorController; @@ -42,29 +43,27 @@ impl From for JsCursor { #[napi] impl CursorController { - #[napi(js_name = "callback", ts_args_type = "fun: (event: Cursor) => void")] - pub fn jscallback(&self, fun: napi::JsFunction) -> napi::Result<()>{ - let tsfn : ThreadsafeFunction = + + #[napi(js_name = "callback", ts_args_type = "fun: (event: CursorController) => void")] + pub fn jscallback(&self, fun: napi::JsFunction) -> napi::Result<()>{ + let tsfn : ThreadsafeFunction = fun.create_threadsafe_function(0, - |ctx : ThreadSafeCallContext| { + |ctx : ThreadSafeCallContext| { Ok(vec![ctx.value]) } )?; - let _controller = self.clone(); - tokio::spawn(async move { - loop { - match _controller.recv().await { - Ok(event) => { - tsfn.call(event.into(), ThreadsafeFunctionCallMode::NonBlocking); //check this shit with tracing also we could use Ok(event) to get the error - }, - Err(crate::Error::Deadlocked) => continue, - Err(e) => break tracing::warn!("error receiving: {}", e), - } - } + self.callback(move |controller : CursorController| { + + tsfn.call(controller.clone(), ThreadsafeFunctionCallMode::Blocking); //check this with tracing also we could use Ok(event) to get the error + // If it blocks the main thread too many time we have to change this + }); + Ok(()) } + + #[napi(js_name = "send")] pub async fn js_send(&self, pos: JsCursor) -> napi::Result<()> { Ok(self.send(crate::api::Cursor::from(pos)).await?)