forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fusebox/RDT: setup global for fusebox runtime (facebook#43418)
Summary: Pull Request resolved: facebook#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 - facebookexperimental/rn-chrome-devtools-frontend#15 Differential Revision: D54770207
- Loading branch information
1 parent
3706bf0
commit 078bf01
Showing
4 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Fusebox Runtime | ||
|
||
## What is Fusebox? | ||
https://fburl.com/wut/j7u7kgal | ||
|
||
## Architecture | ||
|
||
### Frontend to Backend communication | ||
![Frontend to backend communication diagram](./assets/frontend-to-backend.excalidraw-embedded.png) | ||
|
||
### Backend to Frontend communication | ||
![Backend to frontend communication diagram](./assets/backend-to-frontend.excalidraw-embedded.png) |
Binary file added
BIN
+535 KB
...ative/Libraries/Core/fusebox/assets/backend-to-frontend.excalidraw-embedded.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+395 KB
...ative/Libraries/Core/fusebox/assets/frontend-to-backend.excalidraw-embedded.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
106 changes: 106 additions & 0 deletions
106
packages/react-native/Libraries/Core/fusebox/setUpFuseboxRuntime.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict-local | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
type JSONValue = | ||
| string | ||
| number | ||
| boolean | ||
| null | ||
| {[key: string]: JSONValue} | ||
| Array<JSONValue>; | ||
type DomainName = 'react-devtools'; | ||
|
||
class EventScope<T> { | ||
#cache: Set<(T) => void> = new Set(); | ||
|
||
addEventListener(listener: T => void): void { | ||
this.#cache.add(listener); | ||
} | ||
|
||
removeEventListener(listener: T => void): void { | ||
this.#cache.delete(listener); | ||
} | ||
|
||
// Should be hidden. | ||
emit(value: T) { | ||
for (const listener of this.#cache) { | ||
listener(value); | ||
} | ||
} | ||
} | ||
|
||
class Domain { | ||
name: DomainName; | ||
onMessage: EventScope<JSONValue>; | ||
|
||
constructor(name: DomainName) { | ||
if (global[FuseboxRuntime.BINDING_NAME] == null) { | ||
throw new Error( | ||
`Could not create domain ${name}: receiving end doesn't exist`, | ||
); | ||
} | ||
|
||
this.name = name; | ||
this.onMessage = new EventScope<JSONValue>(); | ||
} | ||
|
||
sendMessage(message: JSONValue) { | ||
const messageWithDomain = {domain: this.name, message}; | ||
const serializedMessageWithDomain = JSON.stringify(messageWithDomain); | ||
|
||
global[FuseboxRuntime.BINDING_NAME](serializedMessageWithDomain); | ||
} | ||
} | ||
|
||
class FuseboxRuntime { | ||
static #domainNameToDomainMap: Map<DomainName, Domain> = new Map(); | ||
|
||
// Referenced and initialized from Chrome DevTools frontend. | ||
static BINDING_NAME: string = '__CHROME_DEVTOOLS_FRONTEND_BINDING__'; | ||
static onDomainInitialization: EventScope<Domain> = new EventScope<Domain>(); | ||
|
||
// Should be private, referenced from Chrome DevTools frontend. | ||
static initializeDomain(domainName: DomainName): Domain { | ||
const domain = new Domain(domainName); | ||
|
||
this.#domainNameToDomainMap.set(domainName, domain); | ||
this.onDomainInitialization.emit(domain); | ||
|
||
return domain; | ||
} | ||
|
||
// Should be private, referenced from Chrome DevTools frontend. | ||
static sendMessage(domainName: DomainName, message: string): void { | ||
const domain = this.#domainNameToDomainMap.get(domainName); | ||
if (domain == null) { | ||
throw new Error( | ||
`Could not send message to ${domainName}: domain doesn't exist`, | ||
); | ||
} | ||
|
||
try { | ||
const parsedMessage = JSON.parse(message); | ||
domain.onMessage.emit(parsedMessage); | ||
} catch (err) { | ||
console.error( | ||
`Error while trying to send a message to domain ${domainName}:`, | ||
err, | ||
); | ||
} | ||
} | ||
} | ||
|
||
Object.defineProperty(global, '__FUSEBOX_RUNTIME__', { | ||
value: FuseboxRuntime, | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
}); |