feat(js): Changed callbacks method to return Controller instead of Change

This commit is contained in:
frelodev 2024-08-24 17:43:04 +02:00
parent 076128e1db
commit 6ea5a72b0c
3 changed files with 29 additions and 34 deletions

View file

@ -1,6 +1,6 @@
{ {
"name": "@codemp/codemp", "name": "@codemp/codemp",
"version": "0.0.4", "version": "0.0.5",
"dependencies": { "dependencies": {
"@napi-rs/cli": "^2.18.0", "@napi-rs/cli": "^2.18.0",

View file

@ -7,30 +7,26 @@ use crate::buffer::controller::BufferController;
#[napi] #[napi]
impl BufferController { impl BufferController {
#[napi(js_name = "callback", ts_args_type = "fun: (event: TextChange) => void")]
#[napi(js_name = "callback", ts_args_type = "fun: (event: BufferController) => void")]
pub fn jscallback(&self, fun: napi::JsFunction) -> napi::Result<()>{ pub fn jscallback(&self, fun: napi::JsFunction) -> napi::Result<()>{
let tsfn : ThreadsafeFunction<crate::api::TextChange, Fatal> = let tsfn : ThreadsafeFunction<crate::buffer::controller::BufferController, Fatal> =
fun.create_threadsafe_function(0, fun.create_threadsafe_function(0,
|ctx : ThreadSafeCallContext<crate::api::TextChange>| { |ctx : ThreadSafeCallContext<crate::buffer::controller::BufferController>| {
Ok(vec![ctx.value]) Ok(vec![ctx.value])
} }
)?; )?;
let _controller = self.clone(); self.callback(move |controller : BufferController| {
tokio::spawn(async move {
//tokio::time::sleep(std::time::Duration::from_secs(1)).await; tsfn.call(controller.clone(), ThreadsafeFunctionCallMode::Blocking); //check this with tracing also we could use Ok(event) to get the error
loop { // If it blocks the main thread too many time we have to change this
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),
}
}
}); });
Ok(()) Ok(())
} }
#[napi(js_name = "get_name")] #[napi(js_name = "get_name")]
pub fn js_name(&self) -> napi::Result<&str> { pub fn js_name(&self) -> napi::Result<&str> {
Ok(&self.name()) Ok(&self.name())

View file

@ -1,5 +1,6 @@
use napi::threadsafe_function::ErrorStrategy::Fatal;
use napi_derive::napi; 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::api::Controller;
use crate::cursor::controller::CursorController; use crate::cursor::controller::CursorController;
@ -42,29 +43,27 @@ impl From<crate::api::Cursor> for JsCursor {
#[napi] #[napi]
impl CursorController { impl CursorController {
#[napi(js_name = "callback", ts_args_type = "fun: (event: Cursor) => void")]
#[napi(js_name = "callback", ts_args_type = "fun: (event: CursorController) => void")]
pub fn jscallback(&self, fun: napi::JsFunction) -> napi::Result<()>{ pub fn jscallback(&self, fun: napi::JsFunction) -> napi::Result<()>{
let tsfn : ThreadsafeFunction<JsCursor, ErrorStrategy::Fatal> = let tsfn : ThreadsafeFunction<crate::cursor::controller::CursorController, Fatal> =
fun.create_threadsafe_function(0, fun.create_threadsafe_function(0,
|ctx : ThreadSafeCallContext<JsCursor>| { |ctx : ThreadSafeCallContext<crate::cursor::controller::CursorController>| {
Ok(vec![ctx.value]) Ok(vec![ctx.value])
} }
)?; )?;
let _controller = self.clone(); self.callback(move |controller : CursorController| {
tokio::spawn(async move {
loop { tsfn.call(controller.clone(), ThreadsafeFunctionCallMode::Blocking); //check this with tracing also we could use Ok(event) to get the error
match _controller.recv().await { // If it blocks the main thread too many time we have to change this
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),
}
}
}); });
Ok(()) Ok(())
} }
#[napi(js_name = "send")] #[napi(js_name = "send")]
pub async fn js_send(&self, pos: JsCursor) -> napi::Result<()> { pub async fn js_send(&self, pos: JsCursor) -> napi::Result<()> {
Ok(self.send(crate::api::Cursor::from(pos)).await?) Ok(self.send(crate::api::Cursor::from(pos)).await?)