import EventEmitter from './events';
import type {ComponentFilter, Wall} from './frontend/types';
import type {
InspectedElementPayload,
OwnersList,
ProfilingDataBackend,
RendererID,
ConsolePatchSettings,
} from 'react-devtools-shared/src/backend/types';
import type {StyleAndLayout as StyleAndLayoutPayload} from 'react-devtools-shared/src/backend/NativeStyleEditor/types';
const BATCH_DURATION = 100;
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 HighlightElementInDOM = {
...ElementAndRendererID,
displayName: string | null,
hideAfterTimeout: boolean,
openNativeElementsPanel: 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 = {
appendComponentStack: boolean,
breakOnConsoleErrors: boolean,
componentFilters: Array<ComponentFilter>,
showInlineWarningsAndErrors: boolean,
hideConsoleLogsInStrictMode: boolean,
};
export type BackendEvents = {
backendVersion: [string],
bridgeProtocol: [BridgeProtocol],
extensionBackendInitialized: [],
fastRefreshScheduled: [],
getSavedPreferences: [],
inspectedElement: [InspectedElementPayload],
isBackendStorageAPISupported: [boolean],
isSynchronousXHRSupported: [boolean],
operations: [Array<number>],
ownersList: [OwnersList],
overrideComponentFilters: [Array<ComponentFilter>],
profilingData: [ProfilingDataBackend],
profilingStatus: [boolean],
reloadAppForProfiling: [],
saveToClipboard: [string],
selectFiber: [number],
shutdown: [],
stopInspectingNative: [boolean],
syncSelectionFromNativeElementsPanel: [],
syncSelectionToNativeElementsPanel: [],
unsupportedRendererVersion: [RendererID],
isNativeStyleEditorSupported: [
{isSupported: boolean, validAttributes: ?$ReadOnlyArray<string>},
],
NativeStyleEditor_styleAndLayout: [StyleAndLayoutPayload],
};
type FrontendEvents = {
clearErrorsAndWarnings: [{rendererID: RendererID}],
clearErrorsForFiberID: [ElementAndRendererID],
clearNativeElementHighlight: [],
clearWarningsForFiberID: [ElementAndRendererID],
copyElementPath: [CopyElementPathParams],
deletePath: [DeletePath],
getBackendVersion: [],
getBridgeProtocol: [],
getOwnersList: [ElementAndRendererID],
getProfilingData: [{rendererID: RendererID}],
getProfilingStatus: [],
highlightNativeElement: [HighlightElementInDOM],
inspectElement: [InspectElementParams],
logElementToConsole: [ElementAndRendererID],
overrideError: [OverrideError],
overrideSuspense: [OverrideSuspense],
overrideValueAtPath: [OverrideValueAtPath],
profilingData: [ProfilingDataBackend],
reloadAndProfile: [boolean],
renamePath: [RenamePath],
savedPreferences: [SavedPreferencesParams],
selectFiber: [number],
setTraceUpdatesEnabled: [boolean],
shutdown: [],
startInspectingNative: [],
startProfiling: [boolean],
stopInspectingNative: [boolean],
stopProfiling: [],
storeAsGlobal: [StoreAsGlobalParams],
updateComponentFilters: [Array<ComponentFilter>],
updateConsolePatchSettings: [ConsolePatchSettings],
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: [],
};
class Bridge<
OutgoingEvents: Object,
IncomingEvents: Object,
> extends EventEmitter<{
...IncomingEvents,
...OutgoingEvents,
}> {
_isShutdown: boolean = false;
_messageQueue: Array<any> = [];
_timeoutID: TimeoutID | null = null;
_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._timeoutID) {
this._timeoutID = setTimeout(this._flush, 0);
}
}
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);
if (this._timeoutID !== null) {
clearTimeout(this._timeoutID);
this._timeoutID = null;
}
}
_flush: () => void = () => {
if (this._timeoutID !== null) {
clearTimeout(this._timeoutID);
this._timeoutID = null;
}
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;
this._timeoutID = setTimeout(this._flush, BATCH_DURATION);
}
};
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;