Skip to content

Commit 121bf6f

Browse files
hoxyqfacebook-github-bot
authored andcommitted
setup global for fusebox runtime
Summary: 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 Differential Revision: D54770207
1 parent c91b960 commit 121bf6f

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed
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+
https://fburl.com/wut/j7u7kgal
5+
6+
## Architecture
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)
535 KB
Loading
395 KB
Loading
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
#cache: Set<(T) => void> = new Set();
23+
24+
addEventListener(listener: T => void): void {
25+
this.#cache.add(listener);
26+
}
27+
28+
removeEventListener(listener: T => void): void {
29+
this.#cache.delete(listener);
30+
}
31+
32+
// Should be hidden.
33+
emit(value: T) {
34+
for (const listener of this.#cache) {
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[FuseboxRuntime.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[FuseboxRuntime.BINDING_NAME](serializedMessageWithDomain);
60+
}
61+
}
62+
63+
class FuseboxRuntime {
64+
static #domainNameToDomainMap: Map<DomainName, Domain> = new Map();
65+
66+
// Referenced and initialized from Chrome DevTools frontend.
67+
static BINDING_NAME: string = '__CHROME_DEVTOOLS_FRONTEND_BINDING__';
68+
static onDomainInitialization: EventScope<Domain> = new EventScope<Domain>();
69+
70+
// Should be private, referenced from Chrome DevTools frontend.
71+
static initializeDomain(domainName: DomainName): Domain {
72+
const domain = new Domain(domainName);
73+
74+
this.#domainNameToDomainMap.set(domainName, domain);
75+
this.onDomainInitialization.emit(domain);
76+
77+
return domain;
78+
}
79+
80+
// Should be private, referenced from Chrome DevTools frontend.
81+
static sendMessage(domainName: DomainName, message: string): void {
82+
const domain = this.#domainNameToDomainMap.get(domainName);
83+
if (domain == null) {
84+
throw new Error(
85+
`Could not send message to ${domainName}: domain doesn't exist`,
86+
);
87+
}
88+
89+
try {
90+
const parsedMessage = JSON.parse(message);
91+
domain.onMessage.emit(parsedMessage);
92+
} catch (err) {
93+
console.error(
94+
`Error while trying to send a message to domain ${domainName}:`,
95+
err,
96+
);
97+
}
98+
}
99+
}
100+
101+
Object.defineProperty(global, '__FUSEBOX_RUNTIME__', {
102+
value: FuseboxRuntime,
103+
configurable: false,
104+
enumerable: false,
105+
writable: false,
106+
});

0 commit comments

Comments
 (0)