Skip to content

Commit 7467a3f

Browse files
committed
test: basic example, standardize identifier names
1 parent 6b7a7d0 commit 7467a3f

File tree

3 files changed

+100
-295
lines changed

3 files changed

+100
-295
lines changed

test/threadsafe_function_ex/test/basic.cc

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ using namespace Napi;
99
namespace call {
1010

1111
// Context of the TSFN.
12-
using Context = std::nullptr_t;
12+
using ContextType = std::nullptr_t;
1313

1414
// Data passed (as pointer) to [Non]BlockingCall
15-
struct Data {
15+
struct DataType {
1616
Reference<Napi::Value> data;
1717
Promise::Deferred deferred;
1818
};
1919

2020
// CallJs callback function
2121
static void CallJs(Napi::Env env, Napi::Function jsCallback,
22-
Context * /*context*/, Data *data) {
22+
ContextType * /*context*/, DataType *data) {
2323
if (!(env == nullptr || jsCallback == nullptr)) {
2424
if (data != nullptr) {
2525
jsCallback.Call(env.Undefined(), {data->data.Value()});
@@ -32,10 +32,10 @@ static void CallJs(Napi::Env env, Napi::Function jsCallback,
3232
}
3333

3434
// Full type of the ThreadSafeFunctionEx
35-
using TSFN = ThreadSafeFunctionEx<Context, Data, CallJs>;
35+
using TSFN = ThreadSafeFunctionEx<ContextType, DataType, CallJs>;
3636

3737
class TSFNWrap;
38-
using base = tsfnutil::TSFNWrapBase<TSFNWrap, Context, TSFN>;
38+
using base = tsfnutil::TSFNWrapBase<TSFNWrap, ContextType, TSFN>;
3939

4040
// A JS-accessible wrap that holds the TSFN.
4141
class TSFNWrap : public base {
@@ -49,7 +49,7 @@ class TSFNWrap : public base {
4949
1, // size_t initialThreadCount,
5050
nullptr, // ContextType* context
5151
base::Finalizer, // Finalizer finalizer
52-
&_deferred // FinalizerDataType data
52+
&_deferred // FinalizerDataType* data
5353
);
5454
}
5555

@@ -60,7 +60,7 @@ class TSFNWrap : public base {
6060

6161
Napi::Value Call(const CallbackInfo &info) {
6262
Napi::Env env = info.Env();
63-
Data *data = new Data{Napi::Reference<Napi::Value>(Persistent(info[0])),
63+
DataType *data = new DataType{Napi::Reference<Napi::Value>(Persistent(info[0])),
6464
Promise::Deferred::New(env)};
6565
_tsfn.NonBlockingCall(data);
6666
return data->deferred.Promise();
@@ -73,14 +73,14 @@ class TSFNWrap : public base {
7373
namespace context {
7474

7575
// Context of the TSFN.
76-
using Context = Reference<Napi::Value>;
76+
using ContextType = Reference<Napi::Value>;
7777

7878
// Data passed (as pointer) to [Non]BlockingCall
79-
using Data = Promise::Deferred;
79+
using DataType = Promise::Deferred;
8080

8181
// CallJs callback function
8282
static void CallJs(Napi::Env env, Napi::Function /*jsCallback*/,
83-
Context *context, Data *data) {
83+
ContextType *context, DataType *data) {
8484
if (env != nullptr) {
8585
if (data != nullptr) {
8686
data->Resolve(context->Value());
@@ -92,18 +92,18 @@ static void CallJs(Napi::Env env, Napi::Function /*jsCallback*/,
9292
}
9393

9494
// Full type of the ThreadSafeFunctionEx
95-
using TSFN = ThreadSafeFunctionEx<Context, Data, CallJs>;
95+
using TSFN = ThreadSafeFunctionEx<ContextType, DataType, CallJs>;
9696

9797
class TSFNWrap;
98-
using base = tsfnutil::TSFNWrapBase<TSFNWrap, Context, TSFN>;
98+
using base = tsfnutil::TSFNWrapBase<TSFNWrap, ContextType, TSFN>;
9999

100100
// A JS-accessible wrap that holds the TSFN.
101101
class TSFNWrap : public base {
102102
public:
103103
TSFNWrap(const CallbackInfo &info) : base(info) {
104104
Napi::Env env = info.Env();
105105

106-
Context *context = new Context(Persistent(info[0]));
106+
ContextType *context = new ContextType(Persistent(info[0]));
107107

108108
_tsfn = TSFN::New(
109109
env, // napi_env env,
@@ -112,9 +112,9 @@ class TSFNWrap : public base {
112112
"Test", // ResourceString resourceName,
113113
0, // size_t maxQueueSize,
114114
1, // size_t initialThreadCount,
115-
context, // Context* context,
115+
context, // ContextType* context,
116116
base::Finalizer, // Finalizer finalizer
117-
&_deferred // FinalizerDataType data
117+
&_deferred // FinalizerDataType* data
118118
);
119119
}
120120

@@ -125,7 +125,7 @@ class TSFNWrap : public base {
125125
}
126126

127127
Napi::Value Call(const CallbackInfo &info) {
128-
auto *callData = new Data(info.Env());
128+
auto *callData = new DataType(info.Env());
129129
_tsfn.NonBlockingCall(callData);
130130
return callData->Promise();
131131
};
@@ -140,17 +140,17 @@ namespace empty {
140140
#if NAPI_VERSION > 4
141141

142142
// Context of the TSFN.
143-
using Context = std::nullptr_t;
143+
using ContextType = std::nullptr_t;
144144

145145
// Data passed (as pointer) to [Non]BlockingCall
146-
struct Data {
146+
struct DataType {
147147
Promise::Deferred deferred;
148148
bool reject;
149149
};
150150

151151
// CallJs callback function
152-
static void CallJs(Napi::Env env, Function jsCallback, Context * /*context*/,
153-
Data *data) {
152+
static void CallJs(Napi::Env env, Function jsCallback, ContextType * /*context*/,
153+
DataType *data) {
154154
if (env != nullptr) {
155155
if (data != nullptr) {
156156
if (data->reject) {
@@ -166,10 +166,10 @@ static void CallJs(Napi::Env env, Function jsCallback, Context * /*context*/,
166166
}
167167

168168
// Full type of the ThreadSafeFunctionEx
169-
using TSFN = ThreadSafeFunctionEx<Context, Data, CallJs>;
169+
using TSFN = ThreadSafeFunctionEx<ContextType, DataType, CallJs>;
170170

171171
class TSFNWrap;
172-
using base = tsfnutil::TSFNWrapBase<TSFNWrap, Context, TSFN>;
172+
using base = tsfnutil::TSFNWrapBase<TSFNWrap, ContextType, TSFN>;
173173

174174
// A JS-accessible wrap that holds the TSFN.
175175
class TSFNWrap : public base {
@@ -183,7 +183,7 @@ class TSFNWrap : public base {
183183
1, // size_t initialThreadCount,
184184
nullptr, // ContextType* context
185185
base::Finalizer, // Finalizer finalizer
186-
&_deferred // FinalizerDataType data
186+
&_deferred // FinalizerDataType* data
187187
);
188188
}
189189

@@ -200,7 +200,7 @@ class TSFNWrap : public base {
200200
}
201201

202202
auto *data =
203-
new Data{Promise::Deferred::New(info.Env()), info[0].ToBoolean()};
203+
new DataType{Promise::Deferred::New(info.Env()), info[0].ToBoolean()};
204204
_tsfn.NonBlockingCall(data);
205205
return data->deferred.Promise();
206206
};
@@ -212,7 +212,7 @@ class TSFNWrap : public base {
212212
namespace existing {
213213

214214
// Data passed (as pointer) to [Non]BlockingCall
215-
struct Data {
215+
struct DataType {
216216
Promise::Deferred deferred;
217217
bool reject;
218218
};
@@ -222,7 +222,7 @@ struct Data {
222222
// are napi_*.
223223
static void CallJs(napi_env env, napi_value jsCallback, void * /*context*/,
224224
void *data) {
225-
Data *casted = static_cast<Data *>(data);
225+
DataType *casted = static_cast<DataType *>(data);
226226
if (env != nullptr) {
227227
if (data != nullptr) {
228228
napi_value undefined;
@@ -244,16 +244,16 @@ static void CallJs(napi_env env, napi_value jsCallback, void * /*context*/,
244244
// parameter is the `TSFNWrap` object. We forward-declare, so we can use
245245
// it as an argument inside `ThreadSafeFunctionEx<>`. This also allows us to
246246
// statically get the correct type when using `tsfn.GetContext()`. The converse
247-
// is true: if the Context type does _not_ match that provided to the underlying
247+
// is true: if the ContextType does _not_ match that provided to the underlying
248248
// napi_create_threadsafe_function, then the static type will be incorrect.
249249
class TSFNWrap;
250250

251251
// Context of the TSFN.
252-
using Context = TSFNWrap;
252+
using ContextType = TSFNWrap;
253253

254254
// Full type of our ThreadSafeFunctionEx
255-
using TSFN = ThreadSafeFunctionEx<Context, Data>;
256-
using base = tsfnutil::TSFNWrapBase<TSFNWrap, Context, TSFN>;
255+
using TSFN = ThreadSafeFunctionEx<ContextType, DataType>;
256+
using base = tsfnutil::TSFNWrapBase<TSFNWrap, ContextType, TSFN>;
257257

258258
// A JS-accessible wrap that holds a TSFN.
259259
class TSFNWrap : public base {
@@ -297,7 +297,7 @@ class TSFNWrap : public base {
297297

298298
Napi::Value Call(const CallbackInfo &info) {
299299
auto *data =
300-
new Data{Promise::Deferred::New(info.Env()), info[0].ToBoolean()};
300+
new DataType{Promise::Deferred::New(info.Env()), info[0].ToBoolean()};
301301
_tsfn.NonBlockingCall(data);
302302
return data->deferred.Promise();
303303
};
@@ -321,15 +321,15 @@ class TSFNWrap : public base {
321321
} // namespace existing
322322
namespace simple {
323323

324-
using Context = std::nullptr_t;
324+
using ContextType = std::nullptr_t;
325325

326-
// Full type of our ThreadSafeFunctionEx. We don't specify the `Context` here
326+
// Full type of our ThreadSafeFunctionEx. We don't specify the `ContextType` here
327327
// (even though the _default_ for the type argument is `std::nullptr_t`) to
328328
// demonstrate construction with no type arguments.
329329
using TSFN = ThreadSafeFunctionEx<>;
330330

331331
class TSFNWrap;
332-
using base = tsfnutil::TSFNWrapBase<TSFNWrap, Context, TSFN>;
332+
using base = tsfnutil::TSFNWrapBase<TSFNWrap, ContextType, TSFN>;
333333

334334
// A JS-accessible wrap that holds a TSFN.
335335
class TSFNWrap : public base {

0 commit comments

Comments
 (0)