Skip to content

Commit

Permalink
feat(js): Changed callbacks method to return Controller instead of Ch…
Browse files Browse the repository at this point in the history
…ange
  • Loading branch information
frelodev committed Aug 24, 2024
1 parent 076128e commit b2335e1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
30 changes: 13 additions & 17 deletions src/ffi/js/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<crate::api::TextChange, Fatal> =

#[napi(js_name = "callback", ts_args_type = "fun: (event: BufferController) => void")]
pub fn jscallback(&self, fun: napi::JsFunction) -> napi::Result<()>{
let tsfn : ThreadsafeFunction<crate::buffer::controller::BufferController, Fatal> =
fun.create_threadsafe_function(0,
|ctx : ThreadSafeCallContext<crate::api::TextChange>| {
|ctx : ThreadSafeCallContext<crate::buffer::controller::BufferController>| {
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())
Expand Down
29 changes: 14 additions & 15 deletions src/ffi/js/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use napi::threadsafe_function::ErrorStrategy::Fatal;
use napi_derive::napi;
use napi::threadsafe_function::{ThreadsafeFunction, ThreadSafeCallContext, ThreadsafeFunctionCallMode, ErrorStrategy};
use crate::api::Controller;
Expand Down Expand Up @@ -42,29 +43,27 @@ impl From<crate::api::Cursor> 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<JsCursor, ErrorStrategy::Fatal> =

#[napi(js_name = "callback", ts_args_type = "fun: (event: CursorController) => void")]
pub fn jscallback(&self, fun: napi::JsFunction) -> napi::Result<()>{
let tsfn : ThreadsafeFunction<crate::cursor::controller::CursorController, Fatal> =
fun.create_threadsafe_function(0,
|ctx : ThreadSafeCallContext<JsCursor>| {
|ctx : ThreadSafeCallContext<crate::cursor::controller::CursorController>| {
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?)
Expand Down

0 comments on commit b2335e1

Please sign in to comment.