Skip to content

Commit b43b0a8

Browse files
committed
[futurepress#95] Windows: Implements getActiveServerId()
1 parent 729463a commit b43b0a8

File tree

5 files changed

+49
-22
lines changed

5 files changed

+49
-22
lines changed

windows/ReactNativeStaticServer/ReactNativeModule.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ using namespace std::chrono_literals;
1212
using namespace winrt::ReactNativeStaticServer;
1313
using namespace winrt::Windows::Networking::Connectivity;
1414

15-
double activeServerId;
1615
ReactNativeModule* mod;
1716
React::ReactPromise<std::string>* pendingResult;
1817
Server *server;
@@ -37,17 +36,26 @@ void unlock_sem() {
3736
}
3837

3938
void OnSignal(std::string signal, std::string details) {
40-
if (signal == CRASHED || signal == TERMINATED) {
41-
delete server;
42-
server = NULL;
43-
}
4439
if (pendingResult) {
4540
if (signal == CRASHED) RNException("Server crashed").reject(*pendingResult);
4641
else pendingResult->Resolve(details);
47-
delete pendingResult;
48-
pendingResult = NULL;
49-
unlock_sem();
50-
} else mod->sendEvent(signal, details);
42+
}
43+
else {
44+
// CAREFUL: sendEvent() depends on server object to get it ID,
45+
// thus MUST BE called before the server instance is dropped below.
46+
mod->sendEvent(signal, details);
47+
}
48+
49+
if (signal == CRASHED || signal == TERMINATED) {
50+
delete server;
51+
server = NULL;
52+
}
53+
54+
if (pendingResult) {
55+
delete pendingResult;
56+
pendingResult = NULL;
57+
unlock_sem();
58+
}
5159
}
5260

5361
ReactNativeStaticServerSpec_Constants ReactNativeModule::GetConstants() noexcept {
@@ -59,6 +67,10 @@ ReactNativeStaticServerSpec_Constants ReactNativeModule::GetConstants() noexcept
5967
return res;
6068
}
6169

70+
void ReactNativeModule::getActiveServerId(React::ReactPromise<std::optional<double>>&& result) noexcept {
71+
result.Resolve(server ? std::optional(server->id()) : std::nullopt);
72+
}
73+
6274
void ReactNativeModule::getLocalIpAddress(React::ReactPromise<std::string>&& result) noexcept {
6375
try {
6476
auto hosts = NetworkInformation::GetHostNames();
@@ -114,7 +126,7 @@ void ReactNativeModule::getOpenPort(
114126

115127
void ReactNativeModule::sendEvent(std::string signal, std::string details) {
116128
JSValueObject obj = JSValueObject{
117-
{"serverId", activeServerId},
129+
{"serverId", server->id()},
118130
{"event", signal},
119131
{"details", details}
120132
};
@@ -142,9 +154,8 @@ void ReactNativeModule::start(
142154
}
143155

144156
mod = this;
145-
activeServerId = id;
146157
pendingResult = new React::ReactPromise<std::string>(result);
147-
server = new Server(configPath, errlogPath, OnSignal);
158+
server = new Server(id, configPath, errlogPath, OnSignal);
148159
server->launch();
149160
}
150161

windows/ReactNativeStaticServer/ReactNativeModule.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ struct ReactNativeModule
3333

3434
void sendEvent(std::string signal, std::string details);
3535

36+
REACT_METHOD(getActiveServerId)
37+
void getActiveServerId(React::ReactPromise<std::optional<double>>&& result) noexcept;
38+
3639
REACT_METHOD(getLocalIpAddress)
3740
void getLocalIpAddress(React::ReactPromise<std::string>&& result) noexcept;
3841

windows/ReactNativeStaticServer/Server.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ void Server::OnLaunchedCallback() {
5151
Server::activeServer->_signalConsumer(LAUNCHED, "");
5252
}
5353

54-
Server::Server(std::string configPath,
54+
Server::Server(
55+
double id,
56+
std::string configPath,
5557
std::string errlogPath,
5658
SignalConsumer signalConsumer
5759
):
60+
_id(id),
5861
_configPath(configPath),
5962
_errlogPath(errlogPath),
6063
_signalConsumer(signalConsumer)

windows/ReactNativeStaticServer/Server.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ namespace winrt::ReactNativeStaticServer {
1313
class Server {
1414
public:
1515
Server(
16+
double id,
1617
std::string configPath,
1718
std::string errlogPath,
1819
SignalConsumer signalConsumer);
1920

21+
inline double id() { return _id; }
22+
2023
void launch();
2124
void shutdown();
2225
private:
26+
double _id;
2327
std::string _configPath;
2428
std::string _errlogPath;
2529
SignalConsumer _signalConsumer;

windows/ReactNativeStaticServer/codegen/NativeReactNativeStaticServerSpec.g.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ struct ReactNativeStaticServerSpec : winrt::Microsoft::ReactNative::TurboModuleS
3737
};
3838
static constexpr auto methods = std::tuple{
3939
Method<void(std::string) noexcept>{0, L"addListener"},
40-
Method<void(double) noexcept>{1, L"removeListeners"},
41-
Method<void(double, std::string, std::string, Promise<std::string>) noexcept>{2, L"start"},
42-
Method<void(Promise<std::string>) noexcept>{3, L"getLocalIpAddress"},
43-
Method<void(std::string, Promise<double>) noexcept>{4, L"getOpenPort"},
44-
Method<void(Promise<std::string>) noexcept>{5, L"stop"},
40+
Method<void(Promise<std::optional<double>>) noexcept>{1, L"getActiveServerId"},
41+
Method<void(double) noexcept>{2, L"removeListeners"},
42+
Method<void(double, std::string, std::string, Promise<std::string>) noexcept>{3, L"start"},
43+
Method<void(Promise<std::string>) noexcept>{4, L"getLocalIpAddress"},
44+
Method<void(std::string, Promise<double>) noexcept>{5, L"getOpenPort"},
45+
Method<void(Promise<std::string>) noexcept>{6, L"stop"},
4546
};
4647

4748
template <class TModule>
@@ -62,26 +63,31 @@ struct ReactNativeStaticServerSpec : winrt::Microsoft::ReactNative::TurboModuleS
6263
" REACT_METHOD(addListener) static void addListener(std::string eventName) noexcept { /* implementation */ }\n");
6364
REACT_SHOW_METHOD_SPEC_ERRORS(
6465
1,
66+
"getActiveServerId",
67+
" REACT_METHOD(getActiveServerId) void getActiveServerId(::React::ReactPromise<std::optional<double>> &&result) noexcept { /* implementation */ }\n"
68+
" REACT_METHOD(getActiveServerId) static void getActiveServerId(::React::ReactPromise<std::optional<double>> &&result) noexcept { /* implementation */ }\n");
69+
REACT_SHOW_METHOD_SPEC_ERRORS(
70+
2,
6571
"removeListeners",
6672
" REACT_METHOD(removeListeners) void removeListeners(double count) noexcept { /* implementation */ }\n"
6773
" REACT_METHOD(removeListeners) static void removeListeners(double count) noexcept { /* implementation */ }\n");
6874
REACT_SHOW_METHOD_SPEC_ERRORS(
69-
2,
75+
3,
7076
"start",
7177
" REACT_METHOD(start) void start(double id, std::string configPath, std::string errlogPath, ::React::ReactPromise<std::string> &&result) noexcept { /* implementation */ }\n"
7278
" REACT_METHOD(start) static void start(double id, std::string configPath, std::string errlogPath, ::React::ReactPromise<std::string> &&result) noexcept { /* implementation */ }\n");
7379
REACT_SHOW_METHOD_SPEC_ERRORS(
74-
3,
80+
4,
7581
"getLocalIpAddress",
7682
" REACT_METHOD(getLocalIpAddress) void getLocalIpAddress(::React::ReactPromise<std::string> &&result) noexcept { /* implementation */ }\n"
7783
" REACT_METHOD(getLocalIpAddress) static void getLocalIpAddress(::React::ReactPromise<std::string> &&result) noexcept { /* implementation */ }\n");
7884
REACT_SHOW_METHOD_SPEC_ERRORS(
79-
4,
85+
5,
8086
"getOpenPort",
8187
" REACT_METHOD(getOpenPort) void getOpenPort(std::string address, ::React::ReactPromise<double> &&result) noexcept { /* implementation */ }\n"
8288
" REACT_METHOD(getOpenPort) static void getOpenPort(std::string address, ::React::ReactPromise<double> &&result) noexcept { /* implementation */ }\n");
8389
REACT_SHOW_METHOD_SPEC_ERRORS(
84-
5,
90+
6,
8591
"stop",
8692
" REACT_METHOD(stop) void stop(::React::ReactPromise<std::string> &&result) noexcept { /* implementation */ }\n"
8793
" REACT_METHOD(stop) static void stop(::React::ReactPromise<std::string> &&result) noexcept { /* implementation */ }\n");

0 commit comments

Comments
 (0)