mirror of
https://github.com/hexedtech/codemp.git
synced 2024-12-22 21:04:53 +01:00
feat(js): Changed callbacks method to return Controller instead of Change
This commit is contained in:
parent
076128e1db
commit
6ea5a72b0c
3 changed files with 29 additions and 34 deletions
2
dist/js/package.json
vendored
2
dist/js/package.json
vendored
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@codemp/codemp",
|
||||
"version": "0.0.4",
|
||||
"version": "0.0.5",
|
||||
|
||||
"dependencies": {
|
||||
"@napi-rs/cli": "^2.18.0",
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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<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?)
|
||||
|
|
Loading…
Reference in a new issue