@@ -46,21 +46,15 @@ HelloObjectAsync::HelloObjectAsync(std::string&& name)
4646 : name_(std::move(name)) {}
4747
4848// Triggered from Javascript world when calling "new HelloObjectAsync(name)"
49- NAN_METHOD (HelloObjectAsync::New)
50- {
51- if (info.IsConstructCall ())
52- {
53- try
54- {
55- if (info.Length () >= 1 )
56- {
57- if (info[0 ]->IsString ())
58- {
49+ NAN_METHOD (HelloObjectAsync::New) {
50+ if (info.IsConstructCall ()) {
51+ try {
52+ if (info.Length () >= 1 ) {
53+ if (info[0 ]->IsString ()) {
5954 // Don't want to risk passing a null string around, which might create unpredictable behavior.
6055 Nan::Utf8String utf8_value (info[0 ]);
6156 int len = utf8_value.length ();
62- if (len <= 0 )
63- {
57+ if (len <= 0 ) {
6458 return Nan::ThrowTypeError (" arg must be a non-empty string" );
6559 }
6660
@@ -91,28 +85,20 @@ NAN_METHOD(HelloObjectAsync::New)
9185 **/
9286 auto * const self = new HelloObjectAsync (std::move (name));
9387 self->Wrap (info.This ()); // Connects C++ object to Javascript object (this)
94- }
95- else
96- {
88+ } else {
9789 return Nan::ThrowTypeError (
9890 " arg must be a string" );
9991 }
100- }
101- else
102- {
92+ } else {
10393 return Nan::ThrowTypeError (
10494 " must provide string arg" );
10595 }
106- }
107- catch (const std::exception& ex)
108- {
96+ } catch (const std::exception& ex) {
10997 return Nan::ThrowTypeError (ex.what ());
11098 }
11199
112100 info.GetReturnValue ().Set (info.This ());
113- }
114- else
115- {
101+ } else {
116102 return Nan::ThrowTypeError (
117103 " Cannot call constructor as function, you need to use 'new' keyword" );
118104 }
@@ -121,23 +107,19 @@ NAN_METHOD(HelloObjectAsync::New)
121107// This function performs expensive allocation of std::map, querying, and string
122108// comparison, therefore threads are nice & busy.
123109// Also, notice that name is passed by reference (std::string const& name)
124- std::string do_expensive_work (bool louder, std::string const & name)
125- {
110+ std::string do_expensive_work (bool louder, std::string const & name) {
126111
127112 std::map<std::size_t , std::string> container;
128113 std::size_t work_to_do = 100000 ;
129114
130- for (std::size_t i = 0 ; i < work_to_do; ++i)
131- {
115+ for (std::size_t i = 0 ; i < work_to_do; ++i) {
132116 container.emplace (i, std::to_string (i));
133117 }
134118
135- for (std::size_t i = 0 ; i < work_to_do; ++i)
136- {
119+ for (std::size_t i = 0 ; i < work_to_do; ++i) {
137120 std::string const & item = container[i];
138121
139- if (item != std::to_string (i))
140- {
122+ if (item != std::to_string (i)) {
141123
142124 // AsyncHelloWorker's Execute function will take care of this error
143125 // and return it to js-world via callback
@@ -150,8 +132,7 @@ std::string do_expensive_work(bool louder, std::string const& name)
150132
151133 std::string result = " ...threads are busy async bees...hello " + name;
152134
153- if (louder)
154- {
135+ if (louder) {
155136 result += " !!!!" ;
156137 }
157138
@@ -179,14 +160,10 @@ struct AsyncHelloWorker : Nan::AsyncWorker // NOLINT to disable cppcoreguideline
179160 // The Execute() function is getting called when the worker starts to run.
180161 // - You only have access to member variables stored in this worker.
181162 // - You do not have access to Javascript v8 objects here.
182- void Execute () override
183- {
184- try
185- {
163+ void Execute () override {
164+ try {
186165 result_ = do_expensive_work (louder_, *name_);
187- }
188- catch (const std::exception& e)
189- {
166+ } catch (const std::exception& e) {
190167 SetErrorMessage (e.what ());
191168 }
192169 }
@@ -198,8 +175,7 @@ struct AsyncHelloWorker : Nan::AsyncWorker // NOLINT to disable cppcoreguideline
198175 // - You have access to Javascript v8 objects again
199176 // - You have to translate from C++ member variables to Javascript v8 objects
200177 // - Finally, you call the user's callback with your results
201- void HandleOKCallback () override
202- {
178+ void HandleOKCallback () override {
203179 Nan::HandleScope scope;
204180
205181 const auto argc = 2u ;
@@ -220,8 +196,7 @@ struct AsyncHelloWorker : Nan::AsyncWorker // NOLINT to disable cppcoreguideline
220196 const std::string* name_;
221197};
222198
223- NAN_METHOD (HelloObjectAsync::helloAsync)
224- {
199+ NAN_METHOD (HelloObjectAsync::helloAsync) {
225200 // "info" comes from the NAN_METHOD macro, which returns differently according
226201 // to the Node version
227202 // "What is node::ObjectWrap???" The short version is that node::ObjectWrap
@@ -242,28 +217,24 @@ NAN_METHOD(HelloObjectAsync::helloAsync)
242217 // instead of throwing.
243218 // Also, "info" comes from the NAN_METHOD macro, which returns differently
244219 // according to the version of node
245- if (!info[1 ]->IsFunction ())
246- {
220+ if (!info[1 ]->IsFunction ()) {
247221 return Nan::ThrowTypeError (" second arg 'callback' must be a function" );
248222 }
249223 v8::Local<v8::Function> callback = info[1 ].As <v8::Function>();
250224
251225 // Check first argument, should be an 'options' object
252- if (!info[0 ]->IsObject ())
253- {
226+ if (!info[0 ]->IsObject ()) {
254227 return utils::CallbackError (" first arg 'options' must be an object" ,
255228 callback);
256229 }
257230 v8::Local<v8::Object> options = info[0 ].As <v8::Object>();
258231
259232 // Check options object for the "louder" property, which should be a boolean
260233 // value
261- if (options->Has (Nan::New (" louder" ).ToLocalChecked ()))
262- {
234+ if (options->Has (Nan::New (" louder" ).ToLocalChecked ())) {
263235 v8::Local<v8::Value> louder_val =
264236 options->Get (Nan::New (" louder" ).ToLocalChecked ());
265- if (!louder_val->IsBoolean ())
266- {
237+ if (!louder_val->IsBoolean ()) {
267238 return utils::CallbackError (" option 'louder' must be a boolean" ,
268239 callback);
269240 }
@@ -282,14 +253,12 @@ NAN_METHOD(HelloObjectAsync::helloAsync)
282253}
283254
284255// Singleton
285- Nan::Persistent<v8::Function>& HelloObjectAsync::create_once ()
286- {
256+ Nan::Persistent<v8::Function>& HelloObjectAsync::create_once () {
287257 static Nan::Persistent<v8::Function> init;
288258 return init;
289259}
290260
291- void HelloObjectAsync::Init (v8::Local<v8::Object> target)
292- {
261+ void HelloObjectAsync::Init (v8::Local<v8::Object> target) {
293262 // A handlescope is needed so that v8 objects created in the local memory
294263 // space (this function in this case)
295264 // are cleaned up when the function is done running (and the handlescope is
0 commit comments