@@ -196,6 +196,22 @@ struct ThreadSafeFinalize {
196196 FinalizerDataType* data;
197197 Finalizer callback;
198198};
199+
200+ template <typename ContextType, typename DataType, typename CallJs, CallJs call>
201+ typename std::enable_if<call != nullptr >::type
202+ static inline CallJsWrapper (napi_env env, napi_value jsCallback, void *context, void *data) {
203+ call (env, Function (env, jsCallback), static_cast <ContextType *>(context),
204+ static_cast <DataType *>(data));
205+ }
206+
207+ template <typename ContextType, typename DataType, typename CallJs, CallJs call>
208+ typename std::enable_if<call == nullptr >::type
209+ static inline CallJsWrapper (napi_env env, napi_value jsCallback, void * /* context*/ ,
210+ void * /* data*/ ) {
211+ if (jsCallback != nullptr ) {
212+ Function (env, jsCallback).Call (0 , nullptr );
213+ }
214+ }
199215#endif
200216
201217template <typename Getter, typename Setter>
@@ -4260,6 +4276,20 @@ inline void AsyncWorker::OnWorkComplete(Napi::Env /*env*/, napi_status status) {
42604276// ThreadSafeFunctionEx<ContextType,DataType,CallJs> class
42614277// //////////////////////////////////////////////////////////////////////////////
42624278
4279+ // static
4280+ template <typename ContextType, typename DataType,
4281+ void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
4282+ template <typename ResourceString>
4283+ inline ThreadSafeFunctionEx<ContextType, DataType, CallJs>
4284+ ThreadSafeFunctionEx<ContextType, DataType, CallJs>::New(
4285+ napi_env env, const Function &callback, const Object &resource,
4286+ ResourceString resourceName, size_t maxQueueSize, size_t initialThreadCount,
4287+ ContextType *context) {
4288+ return New (
4289+ env, callback, resource, resourceName, maxQueueSize, initialThreadCount,
4290+ context, [](Env, void *, ContextType *) {}, static_cast <void *>(nullptr ));
4291+ }
4292+
42634293// static
42644294template <typename ContextType, typename DataType,
42654295 void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
@@ -4397,19 +4427,13 @@ ThreadSafeFunctionEx<ContextType, DataType, CallJs>::New(
43974427 return tsfn;
43984428}
43994429
4430+ // static
44004431template <typename ContextType, typename DataType,
44014432 void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
44024433void ThreadSafeFunctionEx<ContextType, DataType, CallJs>::CallJsInternal(
44034434 napi_env env, napi_value jsCallback, void *context, void *data) {
4404-
4405- if (CallJs == nullptr ) {
4406- if (jsCallback != nullptr ) {
4407- Function (env, jsCallback).Call (0 , nullptr );
4408- }
4409- } else {
4410- CallJs (env, Function (env, jsCallback), static_cast <ContextType *>(context),
4411- static_cast <DataType *>(data));
4412- }
4435+ details::CallJsWrapper<ContextType, DataType, decltype (CallJs), CallJs>(
4436+ env, jsCallback, context, data);
44134437}
44144438
44154439// //////////////////////////////////////////////////////////////////////////////
0 commit comments