From d0346ff11517296b2e0f95d8a64e1802d3fbfc13 Mon Sep 17 00:00:00 2001 From: Mengdi Chen Date: Mon, 6 Jun 2022 17:23:42 -0400 Subject: [PATCH] better typing --- .../src/backend/agent.js | 103 +++++++++--------- 1 file changed, 53 insertions(+), 50 deletions(-) diff --git a/packages/react-devtools-shared/src/backend/agent.js b/packages/react-devtools-shared/src/backend/agent.js index 811ad2aaf6660..e05a43d265b6a 100644 --- a/packages/react-devtools-shared/src/backend/agent.js +++ b/packages/react-devtools-shared/src/backend/agent.js @@ -58,27 +58,27 @@ const debug = (methodName, ...args) => { type ElementAndRendererID = {| id: number, - rendererID: number, + rendererID: RendererID, |}; type StoreAsGlobalParams = {| count: number, id: number, path: Array, - rendererID: number, + rendererID: RendererID, |}; type CopyElementParams = {| id: number, path: Array, - rendererID: number, + rendererID: RendererID, |}; type InspectElementParams = {| forceFullData: boolean, id: number, path: Array | null, - rendererID: number, + rendererID: RendererID, requestID: number, |}; @@ -86,7 +86,7 @@ type OverrideHookParams = {| id: number, hookID: number, path: Array, - rendererID: number, + rendererID: RendererID, wasForwarded?: boolean, value: any, |}; @@ -94,7 +94,7 @@ type OverrideHookParams = {| type SetInParams = {| id: number, path: Array, - rendererID: number, + rendererID: RendererID, wasForwarded?: boolean, value: any, |}; @@ -106,7 +106,7 @@ type DeletePathParams = {| hookID?: ?number, id: number, path: Array, - rendererID: number, + rendererID: RendererID, |}; type RenamePathParams = {| @@ -115,7 +115,7 @@ type RenamePathParams = {| id: number, oldPath: Array, newPath: Array, - rendererID: number, + rendererID: RendererID, |}; type OverrideValueAtPathParams = {| @@ -123,24 +123,24 @@ type OverrideValueAtPathParams = {| hookID?: ?number, id: number, path: Array, - 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, |}; @@ -153,7 +153,7 @@ export default class Agent extends EventEmitter<{| _bridge: BackendBridge; _isProfiling: boolean = false; _recordChangeDescriptions: boolean = false; - _rendererInterfaces: {[key: RendererID]: RendererInterface, ...} = {}; + _rendererInterfaces: Map = new Map(); _persistedSelection: PersistedSelection | null = null; _persistedSelectionMatch: PathMatch | null = null; _traceUpdatesEnabled: boolean = false; @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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; @@ -339,12 +339,12 @@ 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 = () => { @@ -352,7 +352,7 @@ export default class Agent extends EventEmitter<{| }; 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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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); @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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, + ); }