Skip to content

Commit

Permalink
better typing
Browse files Browse the repository at this point in the history
  • Loading branch information
mondaychen committed Jun 6, 2022
1 parent b7d225a commit d0346ff
Showing 1 changed file with 53 additions and 50 deletions.
103 changes: 53 additions & 50 deletions packages/react-devtools-shared/src/backend/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,43 +58,43 @@ const debug = (methodName, ...args) => {

type ElementAndRendererID = {|
id: number,
rendererID: number,
rendererID: RendererID,
|};

type StoreAsGlobalParams = {|
count: number,
id: number,
path: Array<string | number>,
rendererID: number,
rendererID: RendererID,
|};

type CopyElementParams = {|
id: number,
path: Array<string | number>,
rendererID: number,
rendererID: RendererID,
|};

type InspectElementParams = {|
forceFullData: boolean,
id: number,
path: Array<string | number> | null,
rendererID: number,
rendererID: RendererID,
requestID: number,
|};

type OverrideHookParams = {|
id: number,
hookID: number,
path: Array<string | number>,
rendererID: number,
rendererID: RendererID,
wasForwarded?: boolean,
value: any,
|};

type SetInParams = {|
id: number,
path: Array<string | number>,
rendererID: number,
rendererID: RendererID,
wasForwarded?: boolean,
value: any,
|};
Expand All @@ -106,7 +106,7 @@ type DeletePathParams = {|
hookID?: ?number,
id: number,
path: Array<string | number>,
rendererID: number,
rendererID: RendererID,
|};

type RenamePathParams = {|
Expand All @@ -115,32 +115,32 @@ type RenamePathParams = {|
id: number,
oldPath: Array<string | number>,
newPath: Array<string | number>,
rendererID: number,
rendererID: RendererID,
|};

type OverrideValueAtPathParams = {|
type: PathType,
hookID?: ?number,
id: number,
path: Array<string | number>,
rendererID: number,
rendererID: RendererID,
value: any,
|};

type OverrideErrorParams = {|
id: number,
rendererID: number,
rendererID: RendererID,
forceError: boolean,
|};

type OverrideSuspenseParams = {|
id: number,
rendererID: number,
rendererID: RendererID,
forceFallback: boolean,
|};

type PersistedSelection = {|
rendererID: number,
rendererID: RendererID,
path: Array<PathFrame>,
|};

Expand All @@ -153,7 +153,7 @@ export default class Agent extends EventEmitter<{|
_bridge: BackendBridge;
_isProfiling: boolean = false;
_recordChangeDescriptions: boolean = false;
_rendererInterfaces: {[key: RendererID]: RendererInterface, ...} = {};
_rendererInterfaces: Map<RendererID, RendererInterface> = new Map();
_persistedSelection: PersistedSelection | null = null;
_persistedSelectionMatch: PathMatch | null = null;
_traceUpdatesEnabled: boolean = false;
Expand Down Expand Up @@ -256,7 +256,7 @@ export default class Agent extends EventEmitter<{|
}

clearErrorsAndWarnings = ({rendererID}: {|rendererID: RendererID|}) => {
const renderer = this._rendererInterfaces[rendererID];
const renderer = this._rendererInterfaces.get(rendererID);
if (renderer == null) {
console.warn(`Invalid renderer id "${rendererID}"`);
} else {
Expand All @@ -265,7 +265,7 @@ export default class Agent extends EventEmitter<{|
};

clearErrorsForFiberID = ({id, rendererID}: ElementAndRendererID) => {
const renderer = this._rendererInterfaces[rendererID];
const renderer = this._rendererInterfaces.get(rendererID);
if (renderer == null) {
console.warn(`Invalid renderer id "${rendererID}"`);
} else {
Expand All @@ -274,7 +274,7 @@ export default class Agent extends EventEmitter<{|
};

clearWarningsForFiberID = ({id, rendererID}: ElementAndRendererID) => {
const renderer = this._rendererInterfaces[rendererID];
const renderer = this._rendererInterfaces.get(rendererID);
if (renderer == null) {
console.warn(`Invalid renderer id "${rendererID}"`);
} else {
Expand All @@ -283,7 +283,7 @@ export default class Agent extends EventEmitter<{|
};

copyElementPath = ({id, path, rendererID}: CopyElementParams) => {
const renderer = this._rendererInterfaces[rendererID];
const renderer = this._rendererInterfaces.get(rendererID);
if (renderer == null) {
console.warn(`Invalid renderer id "${rendererID}" for element "${id}"`);
} else {
Expand All @@ -292,7 +292,7 @@ export default class Agent extends EventEmitter<{|
};

deletePath = ({hookID, id, path, rendererID, type}: DeletePathParams) => {
const renderer = this._rendererInterfaces[rendererID];
const renderer = this._rendererInterfaces.get(rendererID);
if (renderer == null) {
console.warn(`Invalid renderer id "${rendererID}" for element "${id}"`);
} else {
Expand All @@ -304,7 +304,7 @@ export default class Agent extends EventEmitter<{|
id,
rendererID,
}: ElementAndRendererID): InstanceAndStyle | null {
const renderer = this._rendererInterfaces[rendererID];
const renderer = this._rendererInterfaces.get(rendererID);
if (renderer == null) {
console.warn(`Invalid renderer id "${rendererID}"`);
return null;
Expand Down Expand Up @@ -339,20 +339,20 @@ export default class Agent extends EventEmitter<{|
};

getProfilingData = ({rendererID}: {|rendererID: RendererID|}) => {
const renderer = this._rendererInterfaces[rendererID];
const renderer = this._rendererInterfaces.get(rendererID);
if (renderer == null) {
console.warn(`Invalid renderer id "${rendererID}"`);
} else {
this._bridge.send('profilingData', renderer.getProfilingData());
}

this._bridge.send('profilingData', renderer.getProfilingData());
};

getProfilingStatus = () => {
this._bridge.send('profilingStatus', this._isProfiling);
};

getOwnersList = ({id, rendererID}: ElementAndRendererID) => {
const renderer = this._rendererInterfaces[rendererID];
const renderer = this._rendererInterfaces.get(rendererID);
if (renderer == null) {
console.warn(`Invalid renderer id "${rendererID}" for element "${id}"`);
} else {
Expand All @@ -368,7 +368,7 @@ export default class Agent extends EventEmitter<{|
rendererID,
requestID,
}: InspectElementParams) => {
const renderer = this._rendererInterfaces[rendererID];
const renderer = this._rendererInterfaces.get(rendererID);
if (renderer == null) {
console.warn(`Invalid renderer id "${rendererID}" for element "${id}"`);
} else {
Expand Down Expand Up @@ -398,7 +398,7 @@ export default class Agent extends EventEmitter<{|
};

logElementToConsole = ({id, rendererID}: ElementAndRendererID) => {
const renderer = this._rendererInterfaces[rendererID];
const renderer = this._rendererInterfaces.get(rendererID);
if (renderer == null) {
console.warn(`Invalid renderer id "${rendererID}" for element "${id}"`);
} else {
Expand All @@ -407,7 +407,7 @@ export default class Agent extends EventEmitter<{|
};

overrideError = ({id, rendererID, forceError}: OverrideErrorParams) => {
const renderer = this._rendererInterfaces[rendererID];
const renderer = this._rendererInterfaces.get(rendererID);
if (renderer == null) {
console.warn(`Invalid renderer id "${rendererID}" for element "${id}"`);
} else {
Expand All @@ -420,7 +420,7 @@ export default class Agent extends EventEmitter<{|
rendererID,
forceFallback,
}: OverrideSuspenseParams) => {
const renderer = this._rendererInterfaces[rendererID];
const renderer = this._rendererInterfaces.get(rendererID);
if (renderer == null) {
console.warn(`Invalid renderer id "${rendererID}" for element "${id}"`);
} else {
Expand All @@ -436,7 +436,7 @@ export default class Agent extends EventEmitter<{|
type,
value,
}: OverrideValueAtPathParams) => {
const renderer = this._rendererInterfaces[rendererID];
const renderer = this._rendererInterfaces.get(rendererID);
if (renderer == null) {
console.warn(`Invalid renderer id "${rendererID}" for element "${id}"`);
} else {
Expand Down Expand Up @@ -554,7 +554,7 @@ export default class Agent extends EventEmitter<{|
rendererID,
type,
}: RenamePathParams) => {
const renderer = this._rendererInterfaces[rendererID];
const renderer = this._rendererInterfaces.get(rendererID);
if (renderer == null) {
console.warn(`Invalid renderer id "${rendererID}" for element "${id}"`);
} else {
Expand All @@ -573,7 +573,7 @@ export default class Agent extends EventEmitter<{|
rendererID: RendererID,
rendererInterface: RendererInterface,
) {
this._rendererInterfaces[rendererID] = rendererInterface;
this._rendererInterfaces.set(rendererID, rendererInterface);

if (this._isProfiling) {
rendererInterface.startProfiling(this._recordChangeDescriptions);
Expand Down Expand Up @@ -641,7 +641,7 @@ export default class Agent extends EventEmitter<{|
};

storeAsGlobal = ({count, id, path, rendererID}: StoreAsGlobalParams) => {
const renderer = this._rendererInterfaces[rendererID];
const renderer = this._rendererInterfaces.get(rendererID);
if (renderer == null) {
console.warn(`Invalid renderer id "${rendererID}" for element "${id}"`);
} else {
Expand Down Expand Up @@ -685,7 +685,7 @@ export default class Agent extends EventEmitter<{|
};

viewAttributeSource = ({id, path, rendererID}: CopyElementParams) => {
const renderer = this._rendererInterfaces[rendererID];
const renderer = this._rendererInterfaces.get(rendererID);
if (renderer == null) {
console.warn(`Invalid renderer id "${rendererID}" for element "${id}"`);
} else {
Expand All @@ -694,7 +694,7 @@ export default class Agent extends EventEmitter<{|
};

viewElementSource = ({id, rendererID}: ElementAndRendererID) => {
const renderer = this._rendererInterfaces[rendererID];
const renderer = this._rendererInterfaces.get(rendererID);
if (renderer == null) {
console.warn(`Invalid renderer id "${rendererID}" for element "${id}"`);
} else {
Expand Down Expand Up @@ -748,7 +748,7 @@ export default class Agent extends EventEmitter<{|
const rendererID = operations[0];
if (this._persistedSelection.rendererID === rendererID) {
// Check if we can select a deeper match for the persisted selection.
const renderer = this._rendererInterfaces[rendererID];
const renderer = this._rendererInterfaces.get(rendererID);
if (renderer == null) {
console.warn(`Invalid renderer id "${rendererID}"`);
} else {
Expand All @@ -775,23 +775,26 @@ export default class Agent extends EventEmitter<{|
}
};

onUnsupportedRenderer(rendererID: number) {
onUnsupportedRenderer(rendererID: RendererID) {
this._bridge.send('unsupportedRendererVersion', rendererID);
}

_throttledPersistSelection = throttle((rendererID: number, id: number) => {
// This is throttled, so both renderer and selected ID
// might not be available by the time we read them.
// This is why we need the defensive checks here.
const renderer = this._rendererInterfaces[rendererID];
const path = renderer != null ? renderer.getPathForElement(id) : null;
if (path !== null) {
sessionStorageSetItem(
SESSION_STORAGE_LAST_SELECTION_KEY,
JSON.stringify(({rendererID, path}: PersistedSelection)),
);
} else {
sessionStorageRemoveItem(SESSION_STORAGE_LAST_SELECTION_KEY);
}
}, 1000);
_throttledPersistSelection = throttle(
(rendererID: RendererID, id: number) => {
// This is throttled, so both renderer and selected ID
// might not be available by the time we read them.
// This is why we need the defensive checks here.
const renderer = this._rendererInterfaces.get(rendererID);
const path = renderer != null ? renderer.getPathForElement(id) : null;
if (path !== null) {
sessionStorageSetItem(
SESSION_STORAGE_LAST_SELECTION_KEY,
JSON.stringify(({rendererID, path}: PersistedSelection)),
);
} else {
sessionStorageRemoveItem(SESSION_STORAGE_LAST_SELECTION_KEY);
}
},
1000,
);
}

0 comments on commit d0346ff

Please sign in to comment.