Skip to content

Commit 29f9a4c

Browse files
hoxyqfacebook-github-bot
authored andcommitted
jsinspector: refactor domain notifications implementation (#54244)
Summary: # Changelog: [Internal] Refactors the logic a bit. We will use `Runtime` domain as a signal for installation of `console.createTask()` implementation. Differential Revision: D85274860
1 parent 08fee6e commit 29f9a4c

File tree

2 files changed

+71
-23
lines changed

2 files changed

+71
-23
lines changed

packages/react-native/ReactCommon/jsinspector-modern/RuntimeTarget.cpp

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -179,40 +179,71 @@ void RuntimeTarget::notifyDomainStateChanged(
179179
Domain domain,
180180
bool enabled,
181181
const RuntimeAgent& notifyingAgent) {
182-
bool runtimeAndLogStatusBefore = false, runtimeAndLogStatusAfter = false;
183-
if (domain == Domain::Log || domain == Domain::Runtime) {
184-
runtimeAndLogStatusBefore =
185-
agentsByEnabledDomain_[Domain::Runtime].contains(&notifyingAgent) &&
186-
agentsByEnabledDomain_[Domain::Log].contains(&notifyingAgent);
187-
}
188-
189-
if (enabled) {
190-
agentsByEnabledDomain_[domain].insert(&notifyingAgent);
191-
} else {
192-
agentsByEnabledDomain_[domain].erase(&notifyingAgent);
193-
}
194-
threadSafeDomainStatus_[domain] = !agentsByEnabledDomain_[domain].empty();
195-
196-
if (domain == Domain::Log || domain == Domain::Runtime) {
197-
runtimeAndLogStatusAfter =
198-
agentsByEnabledDomain_[Domain::Runtime].contains(&notifyingAgent) &&
199-
agentsByEnabledDomain_[Domain::Log].contains(&notifyingAgent);
182+
auto [domainStateChangedLocally, domainStateChangedGlobally] =
183+
processDomainChange(domain, enabled, notifyingAgent);
184+
185+
switch (domain) {
186+
case Domain::Log:
187+
case Domain::Runtime: {
188+
auto otherDomain = domain == Domain::Log ? Domain::Runtime : Domain::Log;
189+
// There should be an agent that enables both Log and Runtime domains.
190+
if (!agentsByEnabledDomain_[otherDomain].contains(&notifyingAgent)) {
191+
break;
192+
}
200193

201-
if (runtimeAndLogStatusBefore != runtimeAndLogStatusAfter) {
202-
if (runtimeAndLogStatusAfter) {
194+
if (domainStateChangedGlobally && enabled) {
195+
assert(agentsWithRuntimeAndLogDomainsEnabled_ == 0);
196+
emitDebuggerSessionCreated();
197+
++agentsWithRuntimeAndLogDomainsEnabled_;
198+
} else if (domainStateChangedGlobally) {
199+
assert(agentsWithRuntimeAndLogDomainsEnabled_ == 1);
200+
emitDebuggerSessionDestroyed();
201+
--agentsWithRuntimeAndLogDomainsEnabled_;
202+
} else if (domainStateChangedLocally && enabled) {
203+
// This is a case when given domain was already enabled by other Agent,
204+
// so global state didn't change.
203205
if (++agentsWithRuntimeAndLogDomainsEnabled_ == 1) {
204206
emitDebuggerSessionCreated();
205207
}
206-
} else {
207-
assert(agentsWithRuntimeAndLogDomainsEnabled_ > 0);
208+
} else if (domainStateChangedLocally) {
208209
if (--agentsWithRuntimeAndLogDomainsEnabled_ == 0) {
209210
emitDebuggerSessionDestroyed();
210211
}
211212
}
213+
214+
break;
215+
}
216+
case Domain::Network:
217+
break;
218+
case Domain::kMaxValue: {
219+
throw std::logic_error("Unexpected kMaxValue domain value provided");
212220
}
213221
}
214222
}
215223

224+
std::pair<bool, bool> RuntimeTarget::processDomainChange(
225+
Domain domain,
226+
bool enabled,
227+
const RuntimeAgent& notifyingAgent) {
228+
bool domainHadAgentsBefore = !agentsByEnabledDomain_[domain].empty();
229+
bool domainHasBeenEnabledBefore =
230+
agentsByEnabledDomain_[domain].contains(&notifyingAgent);
231+
232+
if (enabled) {
233+
agentsByEnabledDomain_[domain].insert(&notifyingAgent);
234+
} else {
235+
agentsByEnabledDomain_[domain].erase(&notifyingAgent);
236+
}
237+
threadSafeDomainStatus_[domain] = !agentsByEnabledDomain_[domain].empty();
238+
239+
bool domainHasAgentsAfter = !agentsByEnabledDomain_[domain].empty();
240+
241+
return {
242+
domainHasBeenEnabledBefore ^ enabled,
243+
domainHadAgentsBefore ^ domainHasAgentsAfter,
244+
};
245+
}
246+
216247
bool RuntimeTarget::isDomainEnabled(Domain domain) const {
217248
return threadSafeDomainStatus_[domain];
218249
}

packages/react-native/ReactCommon/jsinspector-modern/RuntimeTarget.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <jsinspector-modern/tracing/TraceRecordingState.h>
2222

2323
#include <memory>
24+
#include <utility>
2425

2526
#ifndef JSINSPECTOR_EXPORT
2627
#ifdef _MSC_VER
@@ -122,7 +123,7 @@ class RuntimeTargetDelegate {
122123
*/
123124
class RuntimeTargetController {
124125
public:
125-
enum class Domain { Network, Runtime, Log, kMaxValue };
126+
enum class Domain { Log, Network, Runtime, kMaxValue };
126127

127128
explicit RuntimeTargetController(RuntimeTarget& target);
128129

@@ -347,6 +348,22 @@ class JSINSPECTOR_EXPORT RuntimeTarget
347348
bool enabled,
348349
const RuntimeAgent& notifyingAgent);
349350

351+
/**
352+
* Processes the changes to the state of a given domain.
353+
*
354+
* Returns a pair of booleans:
355+
* 1. Returns true, if an only if the given domain state changed locally,
356+
* for a given session.
357+
* 2. Returns true, if and only if the given domain state changed globally:
358+
* when the given Agent is the only Agent that enabled given domain across
359+
* sessions, or when the only Agent that had this domain enabled has
360+
* disconnected.
361+
*/
362+
std::pair<bool, bool> processDomainChange(
363+
Domain domain,
364+
bool enabled,
365+
const RuntimeAgent& notifyingAgent);
366+
350367
/**
351368
* Checks whether the given domain is enabled in at least one session
352369
* that is currently connected. This may be called from any thread, with

0 commit comments

Comments
 (0)