diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index a907c780..8c68405a 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -207,6 +207,7 @@ Generated by [AVA](https://avajs.dev). export function withoutAbortController(a: number, b: number): Promise␊ export function withAbortController(a: number, b: number, signal: AbortSignal): Promise␊ export function callThreadsafeFunction(callback: (...args: any[]) => any): void␊ + export function callLongThreadsafeFunction(callback: (...args: any[]) => any): void␊ export function threadsafeFunctionThrowError(cb: (...args: any[]) => any): void␊ export function threadsafeFunctionFatalMode(cb: (...args: any[]) => any): void␊ export function threadsafeFunctionFatalModeError(cb: (...args: any[]) => any): void␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 14120495..e2499777 100644 Binary files a/examples/napi/__test__/typegen.spec.ts.snap and b/examples/napi/__test__/typegen.spec.ts.snap differ diff --git a/examples/napi/electron-renderer/index.js b/examples/napi/electron-renderer/index.js index e3cc9965..6e9f5c3e 100644 --- a/examples/napi/electron-renderer/index.js +++ b/examples/napi/electron-renderer/index.js @@ -1,7 +1,7 @@ const { ipcRenderer } = require('electron') -const { callThreadsafeFunction } = require('../index') +const { callLongThreadsafeFunction } = require('../index') -callThreadsafeFunction(() => {}) +callLongThreadsafeFunction(() => {}) ipcRenderer.on('ping', () => ipcRenderer.send('pong')) diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index 0abc1042..2ad678be 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -197,6 +197,7 @@ export function createSymbol(): symbol export function withoutAbortController(a: number, b: number): Promise export function withAbortController(a: number, b: number, signal: AbortSignal): Promise export function callThreadsafeFunction(callback: (...args: any[]) => any): void +export function callLongThreadsafeFunction(callback: (...args: any[]) => any): void export function threadsafeFunctionThrowError(cb: (...args: any[]) => any): void export function threadsafeFunctionFatalMode(cb: (...args: any[]) => any): void export function threadsafeFunctionFatalModeError(cb: (...args: any[]) => any): void diff --git a/examples/napi/src/threadsafe_function.rs b/examples/napi/src/threadsafe_function.rs index 468c6e51..7e52c489 100644 --- a/examples/napi/src/threadsafe_function.rs +++ b/examples/napi/src/threadsafe_function.rs @@ -1,4 +1,4 @@ -use std::thread; +use std::{thread, time::Duration}; use napi::{ bindgen_prelude::*, @@ -19,6 +19,19 @@ pub fn call_threadsafe_function(callback: JsFunction) -> Result<()> { Ok(()) } +#[napi] +pub fn call_long_threadsafe_function(callback: JsFunction) -> Result<()> { + let tsfn: ThreadsafeFunction = + callback.create_threadsafe_function(0, |ctx| Ok(vec![ctx.value + 1]))?; + thread::spawn(move || { + for n in 0..10 { + thread::sleep(Duration::from_millis(100)); + tsfn.call(Ok(n), ThreadsafeFunctionCallMode::NonBlocking); + } + }); + Ok(()) +} + #[napi] pub fn threadsafe_function_throw_error(cb: JsFunction) -> Result<()> { let tsfn: ThreadsafeFunction =