@@ -120,7 +120,8 @@ Returns one of:
120
120
Indicates that an existing thread will stop making use of the thread-safe
121
121
function. A thread should call this API when it stops making use of this
122
122
thread-safe function. Using any thread-safe APIs after having called this API
123
- has undefined results in the current thread, as the thread-safe function may have been destroyed.
123
+ has undefined results in the current thread, as the thread-safe function may
124
+ have been destroyed.
124
125
125
126
``` cpp
126
127
napi_status Napi::ThreadSafeFunctionEx<ContextType, DataType, Callback>::Release()
@@ -176,7 +177,8 @@ Returns one of:
176
177
- ` napi_ok ` : ` data ` was successfully added to the queue.
177
178
- ` napi_queue_full ` : The queue was full when trying to call in a non-blocking
178
179
method.
179
- - ` napi_closing ` : The thread-safe function is aborted and no further calls can be made.
180
+ - ` napi_closing ` : The thread-safe function is aborted and no further calls can
181
+ be made.
180
182
- ` napi_invalid_arg ` : The thread-safe function is closed.
181
183
- ` napi_generic_failure ` : A generic error occurred when attemping to add to the
182
184
queue.
@@ -186,110 +188,99 @@ Returns one of:
186
188
187
189
``` cpp
188
190
#include < chrono>
189
- #include < thread>
190
191
#include < napi.h>
192
+ #include < thread>
191
193
192
194
using namespace Napi ;
193
195
194
196
using Context = Reference<Value>;
195
197
using DataType = int ;
196
- void CallJs ( Napi::Env env, Function callback, Context* context, DataType* data );
198
+ void CallJs (Napi::Env env, Function callback, Context * context, DataType * data);
197
199
using TSFN = ThreadSafeFunctionEx<Context, DataType, CallJs>;
198
200
using FinalizerDataType = void;
199
201
200
202
std::thread nativeThread;
201
203
TSFN tsfn;
202
204
203
- Value Start( const CallbackInfo& info )
204
- {
205
+ Value Start(const CallbackInfo &info) {
205
206
Napi::Env env = info.Env();
206
207
207
- if ( info.Length() < 2 )
208
- {
209
- throw TypeError::New( env, "Expected two arguments" );
210
- }
211
- else if ( !info[ 0] .IsFunction() )
212
- {
213
- throw TypeError::New( env, "Expected first arg to be function" );
214
- }
215
- else if ( !info[ 1] .IsNumber() )
216
- {
217
- throw TypeError::New( env, "Expected second arg to be number" );
208
+ if (info.Length() < 2) {
209
+ throw TypeError::New(env, "Expected two arguments");
210
+ } else if (!info[ 0] .IsFunction()) {
211
+ throw TypeError::New(env, "Expected first arg to be function");
212
+ } else if (!info[ 1] .IsNumber()) {
213
+ throw TypeError::New(env, "Expected second arg to be number");
218
214
}
219
215
220
216
int count = info[ 1] .As<Number >().Int32Value();
221
217
222
218
// Create a new context set to the the receiver (ie, ` this ` ) of the function
223
219
// call
224
- Context* context = new Reference<Value >( Persistent( info.This() ) );
220
+ Context * context = new Reference<Value >(Persistent(info.This()) );
225
221
226
222
// Create a ThreadSafeFunction
227
- tsfn = TSFN::New( env,
228
- info[ 0] .As<Function >(), // JavaScript function called asynchronously
229
- "Resource Name", // Name
230
- 0, // Unlimited queue
231
- 1, // Only one thread will use this initially
232
- context,
233
- [ ] ( Napi::Env, FinalizerDataType* ,
234
- Context* ctx ) { // Finalizer used to clean threads up
235
- nativeThread.join();
236
- delete ctx;
237
- } );
223
+ tsfn = TSFN::New(
224
+ env,
225
+ info[ 0] .As<Function >(), // JavaScript function called asynchronously
226
+ "Resource Name", // Name
227
+ 0, // Unlimited queue
228
+ 1, // Only one thread will use this initially
229
+ context,
230
+ [ ] (Napi::Env, FinalizerDataType * ,
231
+ Context * ctx) { // Finalizer used to clean threads up
232
+ nativeThread.join();
233
+ delete ctx;
234
+ });
238
235
239
236
// Create a native thread
240
- nativeThread = std::thread( [ count] {
241
- for ( int i = 0; i < count; i++ )
242
- {
237
+ nativeThread = std::thread([ count] {
238
+ for (int i = 0; i < count; i++) {
243
239
// Create new data
244
- int* value = new int( clock() );
240
+ int * value = new int(clock());
245
241
246
242
// Perform a blocking call
247
- napi_status status = tsfn.BlockingCall( value );
248
- if ( status != napi_ok )
249
- {
243
+ napi_status status = tsfn.BlockingCall(value);
244
+ if (status != napi_ok) {
250
245
// Handle error
251
246
break;
252
247
}
253
248
254
- std::this_thread::sleep_for( std::chrono::seconds( 1 ) );
249
+ std::this_thread::sleep_for(std::chrono::seconds(1) );
255
250
}
256
251
257
252
// Release the thread-safe function
258
253
tsfn.Release();
259
- } );
254
+ });
260
255
261
- return Boolean::New( env, true );
256
+ return Boolean::New(env, true);
262
257
}
263
258
264
259
// Transform native data into JS data, passing it to the provided
265
260
// ` callback ` -- the TSFN's JavaScript function.
266
- void CallJs( Napi::Env env, Function callback, Context* context, DataType * data )
267
- {
261
+ void CallJs(Napi::Env env, Function callback, Context * context,
262
+ DataType * data) {
268
263
// Is the JavaScript environment still available to call into, eg. the TSFN is
269
264
// not aborted
270
- if ( env != nullptr )
271
- {
265
+ if (env != nullptr) {
272
266
// On N-API 5+, the ` callback ` parameter is optional; however, this example
273
267
// does ensure a callback is provided.
274
- if ( callback != nullptr )
275
- {
276
- callback.Call( context->Value(), {Number::New( env, * data )} );
268
+ if (callback != nullptr) {
269
+ callback.Call(context->Value(), {Number::New(env, * data)});
277
270
}
278
271
}
279
- if ( data != nullptr )
280
- {
272
+ if (data != nullptr) {
281
273
// We're finished with the data.
282
274
delete data;
283
275
}
284
276
}
285
277
286
- Napi::Object Init( Napi::Env env, Object exports )
287
- {
288
- exports.Set( "start", Function::New( env, Start ) );
278
+ Napi::Object Init(Napi::Env env, Object exports) {
279
+ exports.Set("start", Function::New(env, Start));
289
280
return exports;
290
281
}
291
282
292
- NODE_API_MODULE( clock, Init )
283
+ NODE_API_MODULE(clock, Init)
293
284
```
294
285
295
286
The above code can be used from JavaScript as follows:
@@ -304,7 +295,7 @@ start.call(new Date(), function (clock) {
304
295
```
305
296
306
297
When executed, the output will show the value of ` clock() ` five times at one
307
- second intervals, prefixed with the TSFN's context -- ` start ` 's receiver (ie,
298
+ second intervals, prefixed with the TSFN's context -- ` start ` 's receiver (ie,
308
299
` new Date() ` ):
309
300
310
301
```
0 commit comments