Skip to content

Commit

Permalink
Solves #310 re-init/hot-reload problem
Browse files Browse the repository at this point in the history
  • Loading branch information
murat-dogan committed Dec 1, 2024
1 parent 64e7eac commit 6131814
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 26 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch",
"test:wpt": "npm run run:wpt:server & (sleep 8 && (npm run run:wpt:test | tee test/wpt-tests/last-test-results.md) )",
"run:wpt:server": "cd test/wpt-tests/wpt && ./wpt serve",
"run:wpt:test": "ts-node test/wpt-tests/index.ts",
"wpt:server": "cd test/wpt-tests/wpt && ./wpt serve",
"wpt:test": "ts-node test/wpt-tests/index.ts",
"_prebuild": "prebuild -r napi --backend cmake-js",
"prepack": "npm run build:tsc"
},
Expand Down
10 changes: 7 additions & 3 deletions src/cpp/data-channel-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "plog/Log.h"

Napi::FunctionReference DataChannelWrapper::constructor;
Napi::FunctionReference DataChannelWrapper::constructor = Napi::FunctionReference();
std::unordered_set<DataChannelWrapper *> DataChannelWrapper::instances;

void DataChannelWrapper::CloseAll()
Expand Down Expand Up @@ -46,8 +46,12 @@ Napi::Object DataChannelWrapper::Init(Napi::Env env, Napi::Object exports)
InstanceMethod("onMessage", &DataChannelWrapper::onMessage),
});

constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
// If this is not the first call, we don't want to reassign the constructor (hot-reload problem)
if(constructor.IsEmpty())
{
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
}

exports.Set("DataChannel", func);
return exports;
Expand Down
10 changes: 7 additions & 3 deletions src/cpp/media-audio-wrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "media-audio-wrapper.h"
#include "media-direction.h"

Napi::FunctionReference AudioWrapper::constructor;
Napi::FunctionReference AudioWrapper::constructor = Napi::FunctionReference();
std::unordered_set<AudioWrapper *> AudioWrapper::instances;

Napi::Object AudioWrapper::Init(Napi::Env env, Napi::Object exports)
Expand Down Expand Up @@ -35,8 +35,12 @@ Napi::Object AudioWrapper::Init(Napi::Env env, Napi::Object exports)
InstanceMethod("parseSdpLine", &AudioWrapper::parseSdpLine),
});

constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
// If this is not the first call, we don't want to reassign the constructor (hot-reload problem)
if(constructor.IsEmpty())
{
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
}

exports.Set("Audio", func);
return exports;
Expand Down
10 changes: 7 additions & 3 deletions src/cpp/media-rtcpreceivingsession-wrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "media-rtcpreceivingsession-wrapper.h"

Napi::FunctionReference RtcpReceivingSessionWrapper::constructor;
Napi::FunctionReference RtcpReceivingSessionWrapper::constructor = Napi::FunctionReference();
std::unordered_set<RtcpReceivingSessionWrapper *> RtcpReceivingSessionWrapper::instances;

Napi::Object RtcpReceivingSessionWrapper::Init(Napi::Env env, Napi::Object exports)
Expand All @@ -14,8 +14,12 @@ Napi::Object RtcpReceivingSessionWrapper::Init(Napi::Env env, Napi::Object expor
// Instance Methods
});

constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
// If this is not the first call, we don't want to reassign the constructor (hot-reload problem)
if(constructor.IsEmpty())
{
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
}

exports.Set("RtcpReceivingSession", func);
return exports;
Expand Down
10 changes: 7 additions & 3 deletions src/cpp/media-track-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "plog/Log.h"

Napi::FunctionReference TrackWrapper::constructor;
Napi::FunctionReference TrackWrapper::constructor = Napi::FunctionReference();
std::unordered_set<TrackWrapper *> TrackWrapper::instances;

void TrackWrapper::CloseAll()
Expand Down Expand Up @@ -48,8 +48,12 @@ Napi::Object TrackWrapper::Init(Napi::Env env, Napi::Object exports)
InstanceMethod("onMessage", &TrackWrapper::onMessage),
});

constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
// If this is not the first call, we don't want to reassign the constructor (hot-reload problem)
if(constructor.IsEmpty())
{
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
}

exports.Set("Track", func);
return exports;
Expand Down
10 changes: 7 additions & 3 deletions src/cpp/media-video-wrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "media-video-wrapper.h"
#include "media-direction.h"

Napi::FunctionReference VideoWrapper::constructor;
Napi::FunctionReference VideoWrapper::constructor = Napi::FunctionReference();
std::unordered_set<VideoWrapper *> VideoWrapper::instances;

Napi::Object VideoWrapper::Init(Napi::Env env, Napi::Object exports)
Expand Down Expand Up @@ -37,8 +37,12 @@ Napi::Object VideoWrapper::Init(Napi::Env env, Napi::Object exports)
InstanceMethod("parseSdpLine", &VideoWrapper::parseSdpLine),
});

constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
// If this is not the first call, we don't want to reassign the constructor (hot-reload problem)
if(constructor.IsEmpty())
{
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
}

exports.Set("Video", func);
return exports;
Expand Down
10 changes: 7 additions & 3 deletions src/cpp/peer-connection-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <cctype>
#include <sstream>

Napi::FunctionReference PeerConnectionWrapper::constructor;
Napi::FunctionReference PeerConnectionWrapper::constructor = Napi::FunctionReference();
std::unordered_set<PeerConnectionWrapper *> PeerConnectionWrapper::instances;

void PeerConnectionWrapper::CloseAll()
Expand Down Expand Up @@ -65,8 +65,12 @@ Napi::Object PeerConnectionWrapper::Init(Napi::Env env, Napi::Object exports)
InstanceMethod("maxMessageSize", &PeerConnectionWrapper::maxMessageSize),
});

constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
// If this is not the first call, we don't want to reassign the constructor (hot-reload problem)
if(constructor.IsEmpty())
{
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
}

exports.Set("PeerConnection", func);
return exports;
Expand Down
10 changes: 7 additions & 3 deletions src/cpp/web-socket-server-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "plog/Log.h"

Napi::FunctionReference WebSocketServerWrapper::constructor;
Napi::FunctionReference WebSocketServerWrapper::constructor = Napi::FunctionReference();
std::unordered_set<WebSocketServerWrapper *> WebSocketServerWrapper::instances;

void WebSocketServerWrapper::StopAll()
Expand All @@ -26,8 +26,12 @@ Napi::Object WebSocketServerWrapper::Init(Napi::Env env, Napi::Object exports)
InstanceMethod("onClient", &WebSocketServerWrapper::onClient)
});

constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
// If this is not the first call, we don't want to reassign the constructor (hot-reload problem)
if(constructor.IsEmpty())
{
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
}

exports.Set("WebSocketServer", func);
return exports;
Expand Down
10 changes: 7 additions & 3 deletions src/cpp/web-socket-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "plog/Log.h"

Napi::FunctionReference WebSocketWrapper::constructor;
Napi::FunctionReference WebSocketWrapper::constructor = Napi::FunctionReference();
std::unordered_set<WebSocketWrapper *> WebSocketWrapper::instances;

void WebSocketWrapper::CloseAll()
Expand Down Expand Up @@ -47,8 +47,12 @@ Napi::Object WebSocketWrapper::Init(Napi::Env env, Napi::Object exports)
InstanceMethod("path", &WebSocketWrapper::path),
});

constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
// If this is not the first call, we don't want to reassign the constructor (hot-reload problem)
if(constructor.IsEmpty())
{
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
}

exports.Set("WebSocket", func);
return exports;
Expand Down
5 changes: 5 additions & 0 deletions src/lib/node-datachannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ try {
nodeDataChannel = require('../../../build/Release/node_datachannel.node');
}
catch (e) {
// If this is not a module not found error, rethrow it
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}

// from src
nodeDataChannel = require('../../build/Release/node_datachannel.node');
}
Expand Down

0 comments on commit 6131814

Please sign in to comment.