Skip to content

Commit f9e3db5

Browse files
hoxyqmeta-codesync[bot]
authored andcommitted
jsinspector: refactor domain notifications implementation (facebook#54244)
Summary: Pull Request resolved: facebook#54244 # Changelog: [Internal] Refactors the logic a bit. We will use `Runtime` domain as a signal for installation of `console.createTask()` implementation. Reviewed By: huntie Differential Revision: D85274860 fbshipit-source-id: 80c91a8a83ba2b95b70aa38a72529bbd20275c0d
1 parent 8253a34 commit f9e3db5

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
@@ -116,7 +117,7 @@ class RuntimeTargetDelegate {
116117
*/
117118
class RuntimeTargetController {
118119
public:
119-
enum class Domain { Network, Runtime, Log, kMaxValue };
120+
enum class Domain { Log, Network, Runtime, kMaxValue };
120121

121122
explicit RuntimeTargetController(RuntimeTarget &target);
122123

@@ -330,6 +331,22 @@ class JSINSPECTOR_EXPORT RuntimeTarget : public EnableExecutorFromThis<RuntimeTa
330331
*/
331332
void notifyDomainStateChanged(Domain domain, bool enabled, const RuntimeAgent &notifyingAgent);
332333

334+
/**
335+
* Processes the changes to the state of a given domain.
336+
*
337+
* Returns a pair of booleans:
338+
* 1. Returns true, if an only if the given domain state changed locally,
339+
* for a given session.
340+
* 2. Returns true, if and only if the given domain state changed globally:
341+
* when the given Agent is the only Agent that enabled given domain across
342+
* sessions, or when the only Agent that had this domain enabled has
343+
* disconnected.
344+
*/
345+
std::pair<bool, bool> processDomainChange(
346+
Domain domain,
347+
bool enabled,
348+
const RuntimeAgent& notifyingAgent);
349+
333350
/**
334351
* Checks whether the given domain is enabled in at least one session
335352
* that is currently connected. This may be called from any thread, with

0 commit comments

Comments
 (0)