import EventEmitter from './events';
import type {ComponentFilter, Wall} from './frontend/types';
import type {
InspectedElementPayload,
OwnersList,
ProfilingDataBackend,
RendererID,
DevToolsHookSettings,
} from 'react-devtools-shared/src/backend/types';
import type {StyleAndLayout as StyleAndLayoutPayload} from 'react-devtools-shared/src/backend/NativeStyleEditor/types';
export type BridgeProtocol = {
version: number,
minNpmVersion: string,
maxNpmVersion: string | null,
};
export const BRIDGE_PROTOCOL: Array<BridgeProtocol> = [
{
version: 0,
minNpmVersion: '"<4.11.0"',
maxNpmVersion: '"<4.11.0"',
},
{
version: 1,
minNpmVersion: '4.13.0',
maxNpmVersion: '4.21.0',
},
{
version: 2,
minNpmVersion: '4.22.0',
maxNpmVersion: null,
},
];
export const currentBridgeProtocol: BridgeProtocol =
BRIDGE_PROTOCOL[BRIDGE_PROTOCOL.length - 1];
type ElementAndRendererID = {id: number, rendererID: RendererID};
type Message = {
event: string,
payload: any,
};
type HighlightHostInstance = {
...ElementAndRendererID,
displayName: string | null,
hideAfterTimeout: boolean,
openBuiltinElementsPanel: boolean,
scrollIntoView: boolean,
};
type OverrideValue = {
...ElementAndRendererID,
path: Array<string | number>,
wasForwarded?: boolean,
value: any,
};
type OverrideHookState = {
...OverrideValue,
hookID: number,
};
type PathType = 'props' | 'hooks' | 'state' | 'context';
type DeletePath = {
...ElementAndRendererID,
type: PathType,
hookID?: ?number,
path: Array<string | number>,
};
type RenamePath = {
...ElementAndRendererID,
type: PathType,
hookID?: ?number,
oldPath: Array<string | number>,
newPath: Array<string | number>,
};
type OverrideValueAtPath = {
...ElementAndRendererID,
type: PathType,
hookID?: ?number,
path: Array<string | number>,
value: any,
};
type OverrideError = {
...ElementAndRendererID,
forceError: boolean,
};
type OverrideSuspense = {
...ElementAndRendererID,
forceFallback: boolean,
};
type CopyElementPathParams = {
...ElementAndRendererID,
path: Array<string | number>,
};
type ViewAttributeSourceParams = {
...ElementAndRendererID,
path: Array<string | number>,
};
type InspectElementParams = {
...ElementAndRendererID,
forceFullData: boolean,
path: Array<number | string> | null,
requestID: number,
};
type StoreAsGlobalParams = {
...ElementAndRendererID,
count: number,
path: Array<string | number>,
};
type NativeStyleEditor_RenameAttributeParams = {
...ElementAndRendererID,
oldName: string,
newName: string,
value: string,
};
type NativeStyleEditor_SetValueParams = {
...ElementAndRendererID,
name: string,
value: string,
};
type SavedPreferencesParams = {
componentFilters: Array<ComponentFilter>,
};
export type BackendEvents = {
backendInitialized: [],
backendVersion: [string],
bridgeProtocol: [BridgeProtocol],
extensionBackendInitialized: [],
fastRefreshScheduled: [],
getSavedPreferences: [],
inspectedElement: [InspectedElementPayload],
isReloadAndProfileSupportedByBackend: [boolean],
operations: [Array<number>],
ownersList: [OwnersList],
overrideComponentFilters: [Array<ComponentFilter>],
environmentNames: [Array<string>],
profilingData: [ProfilingDataBackend],
profilingStatus: [boolean],
reloadAppForProfiling: [],
saveToClipboard: [string],
selectElement: [number],
shutdown: [],
stopInspectingHost: [boolean],
syncSelectionFromBuiltinElementsPanel: [],
syncSelectionToBuiltinElementsPanel: [],
unsupportedRendererVersion: [],
isNativeStyleEditorSupported: [
{isSupported: boolean, validAttributes: ?$ReadOnlyArray<string>},
],
NativeStyleEditor_styleAndLayout: [StyleAndLayoutPayload],
hookSettings: [$ReadOnly<DevToolsHookSettings>],
};
type FrontendEvents = {
clearErrorsAndWarnings: [{rendererID: RendererID}],
clearErrorsForElementID: [ElementAndRendererID],
clearHostInstanceHighlight: [],
clearWarningsForElementID: [ElementAndRendererID],
copyElementPath: [CopyElementPathParams],
deletePath: [DeletePath],
getBackendVersion: [],
getBridgeProtocol: [],
getIfHasUnsupportedRendererVersion: [],
getOwnersList: [ElementAndRendererID],
getProfilingData: [{rendererID: RendererID}],
getProfilingStatus: [],
highlightHostInstance: [HighlightHostInstance],
inspectElement: [InspectElementParams],
logElementToConsole: [ElementAndRendererID],
overrideError: [OverrideError],
overrideSuspense: [OverrideSuspense],
overrideValueAtPath: [OverrideValueAtPath],
profilingData: [ProfilingDataBackend],
reloadAndProfile: [boolean],
renamePath: [RenamePath],
savedPreferences: [SavedPreferencesParams],
setTraceUpdatesEnabled: [boolean],
shutdown: [],
startInspectingHost: [],
startProfiling: [boolean],
stopInspectingHost: [boolean],
stopProfiling: [],
storeAsGlobal: [StoreAsGlobalParams],
updateComponentFilters: [Array<ComponentFilter>],
getEnvironmentNames: [],
updateHookSettings: [$ReadOnly<DevToolsHookSettings>],
viewAttributeSource: [ViewAttributeSourceParams],
viewElementSource: [ElementAndRendererID],
NativeStyleEditor_measure: [ElementAndRendererID],
NativeStyleEditor_renameAttribute: [NativeStyleEditor_RenameAttributeParams],
NativeStyleEditor_setValue: [NativeStyleEditor_SetValueParams],
overrideContext: [OverrideValue],
overrideHookState: [OverrideHookState],
overrideProps: [OverrideValue],
overrideState: [OverrideValue],
resumeElementPolling: [],
pauseElementPolling: [],
getHookSettings: [],
};
class Bridge<
OutgoingEvents: Object,
IncomingEvents: Object,
> extends EventEmitter<{
...IncomingEvents,
...OutgoingEvents,
}> {
_isShutdown: boolean = false;
_messageQueue: Array<any> = [];
_scheduledFlush: boolean = false;
_wall: Wall;
_wallUnlisten: Function | null = null;
constructor(wall: Wall) {
super();
this._wall = wall;
this._wallUnlisten =
wall.listen((message: Message) => {
if (message && message.event) {
(this: any).emit(message.event, message.payload);
}
}) || null;
this.addListener('overrideValueAtPath', this.overrideValueAtPath);
}
get wall(): Wall {
return this._wall;
}
send<EventName: $Keys<OutgoingEvents>>(
event: EventName,
...payload: $ElementType<OutgoingEvents, EventName>
) {
if (this._isShutdown) {
console.warn(
`Cannot send message "${event}" through a Bridge that has been shutdown.`,
);
return;
}
this._messageQueue.push(event, payload);
if (!this._scheduledFlush) {
this._scheduledFlush = true;
if (typeof devtoolsJestTestScheduler === 'function') {
devtoolsJestTestScheduler(this._flush);
} else {
queueMicrotask(this._flush);
}
}
}
shutdown() {
if (this._isShutdown) {
console.warn('Bridge was already shutdown.');
return;
}
this.emit('shutdown');
this.send('shutdown');
this._isShutdown = true;
this.addListener = function () {};
this.emit = function () {};
this.removeAllListeners();
const wallUnlisten = this._wallUnlisten;
if (wallUnlisten) {
wallUnlisten();
}
do {
this._flush();
} while (this._messageQueue.length);
}
_flush: () => void = () => {
try {
if (this._messageQueue.length) {
for (let i = 0; i < this._messageQueue.length; i += 2) {
this._wall.send(this._messageQueue[i], ...this._messageQueue[i + 1]);
}
this._messageQueue.length = 0;
}
} finally {
this._scheduledFlush = false;
}
};
overrideValueAtPath: OverrideValueAtPath => void = ({
id,
path,
rendererID,
type,
value,
}: OverrideValueAtPath) => {
switch (type) {
case 'context':
this.send('overrideContext', {
id,
path,
rendererID,
wasForwarded: true,
value,
});
break;
case 'hooks':
this.send('overrideHookState', {
id,
path,
rendererID,
wasForwarded: true,
value,
});
break;
case 'props':
this.send('overrideProps', {
id,
path,
rendererID,
wasForwarded: true,
value,
});
break;
case 'state':
this.send('overrideState', {
id,
path,
rendererID,
wasForwarded: true,
value,
});
break;
}
};
}
export type BackendBridge = Bridge<BackendEvents, FrontendEvents>;
export type FrontendBridge = Bridge<FrontendEvents, BackendEvents>;
export default Bridge;