@@ -25,17 +25,30 @@ using namespace v8;
2525
2626/* These two are definitely static */
2727Isolate *isolate;
28+ bool valid = true ;
29+
30+ /* We hold all apps until free */
31+ std::vector<std::unique_ptr<uWS::App>> apps;
32+ std::vector<std::unique_ptr<uWS::SSLApp>> sslApps;
2833
2934#include " Utilities.h"
3035#include " WebSocketWrapper.h"
3136#include " HttpResponseWrapper.h"
3237#include " HttpRequestWrapper.h"
3338#include " AppWrapper.h"
3439
35- /* Used for debugging */
36- void print (const FunctionCallbackInfo<Value> &args) {
37- NativeString nativeString (isolate, args[0 ]);
38- std::cout << nativeString.getString () << std::endl;
40+ /* Todo: Apps should be freed once the GC says so BUT ALWAYS before freeing the loop */
41+
42+ /* This has to be called in beforeExit, but exit also seems okay */
43+ void uWS_free (const FunctionCallbackInfo<Value> &args) {
44+ if (valid) {
45+ /* Freeing apps here, it could be done earlier but not sooner */
46+ apps.clear ();
47+ sslApps.clear ();
48+ /* Freeing the loop here means we give time for our timers to close, etc */
49+ uWS::Loop::get ()->free ();
50+ valid = false ;
51+ }
3952}
4053
4154/* todo: Put this function and all inits of it in its own header */
@@ -50,13 +63,13 @@ void Main(Local<Object> exports) {
5063 /* We want this so that we can redefine process.nextTick to using the V8 native microtask queue */
5164 isolate->SetMicrotasksPolicy (MicrotasksPolicy::kAuto );
5265
53- /* Hook up our timers */
54- us_loop_integrate ((us_loop *) uWS::Loop::defaultLoop () );
66+ /* Integrate with existing libuv loop, we just pass a boolean basically */
67+ uWS::Loop::get (( void *) 1 );
5568
5669 /* uWS namespace */
5770 exports->Set (String::NewFromUtf8 (isolate, " App" ), FunctionTemplate::New (isolate, uWS_App<uWS::App>)->GetFunction ());
5871 exports->Set (String::NewFromUtf8 (isolate, " SSLApp" ), FunctionTemplate::New (isolate, uWS_App<uWS::SSLApp>)->GetFunction ());
59- exports->Set (String::NewFromUtf8 (isolate, " print " ), FunctionTemplate::New (isolate, print )->GetFunction ());
72+ exports->Set (String::NewFromUtf8 (isolate, " free " ), FunctionTemplate::New (isolate, uWS_free )->GetFunction ());
6073
6174 /* Expose some µSockets functions directly under uWS namespace */
6275 exports->Set (String::NewFromUtf8 (isolate, " us_listen_socket_close" ), FunctionTemplate::New (isolate, uWS_us_listen_socket_close)->GetFunction ());
0 commit comments