Skip to content

Commit aacde17

Browse files
hoxyqfacebook-github-bot
authored andcommitted
Fusebox/RDT: setup global for Fusebox React DevTools dispatcher (#43418)
Summary: Pull Request resolved: #43418 Changelog: [Internal] This diff adds a script, which will be later imported from `InitializeCore` to setup a required global for communication between React Native runtime (RDT Backend in it) and Chrome DevTools Frontend (RDT Frontend in it). See README for the architecture overview and how bidirectional communication is established. Corresponding PR in Chrome DevTools frontend - facebook/react-native-devtools-frontend#15 Reviewed By: motiz88 Differential Revision: D54770207 fbshipit-source-id: 0f0f04a338b5c7eab817c843a99b07cca95e57fd
1 parent 78d523d commit aacde17

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
assets
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Fusebox Runtime
2+
3+
## What is Fusebox?
4+
"Fusebox" is the internal codename for the new React Native debugger stack based on Chrome DevTools.
5+
6+
## Architecture for React DevTools communication
7+
8+
### Frontend to Backend communication
9+
![Frontend to backend communication diagram](./assets/frontend-to-backend.excalidraw-embedded.png)
10+
11+
### Backend to Frontend communication
12+
![Backend to frontend communication diagram](./assets/backend-to-frontend.excalidraw-embedded.png)
540 KB
Loading
409 KB
Loading
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
* @oncall react_native
10+
*/
11+
12+
type JSONValue =
13+
| string
14+
| number
15+
| boolean
16+
| null
17+
| {[key: string]: JSONValue}
18+
| Array<JSONValue>;
19+
type DomainName = 'react-devtools';
20+
21+
class EventScope<T> {
22+
#listeners: Set<(T) => void> = new Set();
23+
24+
addEventListener(listener: T => void): void {
25+
this.#listeners.add(listener);
26+
}
27+
28+
removeEventListener(listener: T => void): void {
29+
this.#listeners.delete(listener);
30+
}
31+
32+
emit(value: T): void {
33+
// Assuming that listeners won't throw.
34+
for (const listener of this.#listeners) {
35+
listener(value);
36+
}
37+
}
38+
}
39+
40+
class Domain {
41+
name: DomainName;
42+
onMessage: EventScope<JSONValue>;
43+
44+
constructor(name: DomainName) {
45+
if (global[FuseboxReactDevToolsDispatcher.BINDING_NAME] == null) {
46+
throw new Error(
47+
`Could not create domain ${name}: receiving end doesn't exist`,
48+
);
49+
}
50+
51+
this.name = name;
52+
this.onMessage = new EventScope<JSONValue>();
53+
}
54+
55+
sendMessage(message: JSONValue) {
56+
const messageWithDomain = {domain: this.name, message};
57+
const serializedMessageWithDomain = JSON.stringify(messageWithDomain);
58+
59+
global[FuseboxReactDevToolsDispatcher.BINDING_NAME](
60+
serializedMessageWithDomain,
61+
);
62+
}
63+
}
64+
65+
class FuseboxReactDevToolsDispatcher {
66+
static #domainNameToDomainMap: Map<DomainName, Domain> = new Map();
67+
68+
// Referenced and initialized from Chrome DevTools frontend.
69+
static BINDING_NAME: string = '__CHROME_DEVTOOLS_FRONTEND_BINDING__';
70+
static onDomainInitialization: EventScope<Domain> = new EventScope<Domain>();
71+
72+
// Should be private, referenced from Chrome DevTools frontend only.
73+
static initializeDomain(domainName: DomainName): Domain {
74+
const domain = new Domain(domainName);
75+
76+
this.#domainNameToDomainMap.set(domainName, domain);
77+
this.onDomainInitialization.emit(domain);
78+
79+
return domain;
80+
}
81+
82+
// Should be private, referenced from Chrome DevTools frontend only.
83+
static sendMessage(domainName: DomainName, message: string): void {
84+
const domain = this.#domainNameToDomainMap.get(domainName);
85+
if (domain == null) {
86+
throw new Error(
87+
`Could not send message to ${domainName}: domain doesn't exist`,
88+
);
89+
}
90+
91+
try {
92+
const parsedMessage = JSON.parse(message);
93+
domain.onMessage.emit(parsedMessage);
94+
} catch (err) {
95+
console.error(
96+
`Error while trying to send a message to domain ${domainName}:`,
97+
err,
98+
);
99+
}
100+
}
101+
}
102+
103+
Object.defineProperty(global, '__FUSEBOX_REACT_DEVTOOLS_DISPATCHER__', {
104+
value: FuseboxReactDevToolsDispatcher,
105+
configurable: false,
106+
enumerable: false,
107+
writable: false,
108+
});

0 commit comments

Comments
 (0)