-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proposal: Use React context instead of prop drilling for accessing sh…
…ared state - Refactor app component logic into a context provider - Wrap the root component into the newly created `MemoryAppProvider`. This way child components can access the central state via context API and props drilling is no longer required. - Refactor child components to access the central state info via `context` instead of props.
- Loading branch information
Showing
5 changed files
with
294 additions
and
291 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,199 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2022 Ericsson, Arm and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { DebugProtocol } from '@vscode/debugprotocol'; | ||
import React from 'react'; | ||
import { HOST_EXTENSION } from 'vscode-messenger-common'; | ||
import { logMessageType, readMemoryType, readyType, resetMemoryViewSettingsType, setMemoryViewSettingsType, setOptionsType, setTitleType } from '../../common/messaging'; | ||
import { AddressColumn } from '../columns/address-column'; | ||
import { AsciiColumn } from '../columns/ascii-column'; | ||
import { ColumnStatus, columnContributionService } from '../columns/column-contribution-service'; | ||
import { DataColumn } from '../columns/data-column'; | ||
import { decorationService } from '../decorations/decoration-service'; | ||
import { Decoration, Memory, MemoryDisplayConfiguration, MemoryState } from '../utils/view-types'; | ||
import { variableDecorator } from '../variables/variable-decorations'; | ||
import { messenger } from '../view-messenger'; | ||
|
||
export interface MemoryAppState extends MemoryState, MemoryDisplayConfiguration { | ||
title: string; | ||
decorations: Decoration[]; | ||
columns: ColumnStatus[]; | ||
offset: number; | ||
} | ||
|
||
const MEMORY_DISPLAY_CONFIGURATION_DEFAULTS: MemoryDisplayConfiguration = { | ||
bytesPerWord: 1, | ||
wordsPerGroup: 1, | ||
groupsPerRow: 4, | ||
scrollingBehavior: 'Paginate', | ||
addressRadix: 16, | ||
showRadixPrefix: true, | ||
}; | ||
|
||
const MEMORY_APP_STATE_DEFAULTS: MemoryAppState = { | ||
title: 'Memory', | ||
memory: undefined, | ||
memoryReference: '', | ||
offset: 0, | ||
count: 256, | ||
decorations: [], | ||
columns: columnContributionService.getColumns(), | ||
isMemoryFetching: false, | ||
...MEMORY_DISPLAY_CONFIGURATION_DEFAULTS | ||
}; | ||
|
||
const MEMORY_APP_CONTEXT_DEFAULTS: MemoryAppContext = { | ||
...MEMORY_APP_STATE_DEFAULTS, | ||
updateMemoryState: () => { }, | ||
updateMemoryDisplayConfiguration: () => { }, | ||
resetMemoryDisplayConfiguration: () => { }, | ||
updateTitle: () => { }, | ||
refreshMemory: () => { }, | ||
fetchMemory: async () => { }, | ||
toggleColumn: () => { } | ||
}; | ||
|
||
export const MemoryAppContext = React.createContext<MemoryAppContext>(MEMORY_APP_CONTEXT_DEFAULTS); | ||
|
||
export interface MemoryAppContext extends MemoryAppState { | ||
updateMemoryState: (newState: Partial<MemoryAppState>) => void; | ||
updateMemoryDisplayConfiguration: (newState: Partial<MemoryDisplayConfiguration>) => void; | ||
resetMemoryDisplayConfiguration: () => void; | ||
updateTitle: (title: string) => void; | ||
refreshMemory: () => void; | ||
fetchMemory: (partialOptions?: Partial<DebugProtocol.ReadMemoryArguments>) => Promise<void>; | ||
toggleColumn: (id: string, active: boolean) => void; | ||
} | ||
|
||
interface MemoryAppProviderProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
export class MemoryAppProvider extends React.Component<MemoryAppProviderProps, MemoryAppState> { | ||
|
||
public constructor(props: MemoryAppProviderProps) { | ||
super(props); | ||
columnContributionService.register(new AddressColumn(), false); | ||
columnContributionService.register(new DataColumn(), false); | ||
columnContributionService.register(variableDecorator); | ||
columnContributionService.register(new AsciiColumn()); | ||
decorationService.register(variableDecorator); | ||
this.state = { | ||
title: 'Memory', | ||
memory: undefined, | ||
memoryReference: '', | ||
offset: 0, | ||
count: 256, | ||
decorations: [], | ||
columns: columnContributionService.getColumns(), | ||
isMemoryFetching: false, | ||
...MEMORY_DISPLAY_CONFIGURATION_DEFAULTS | ||
}; | ||
} | ||
|
||
public componentDidMount(): void { | ||
messenger.onRequest(setOptionsType, options => this.setOptions(options)); | ||
messenger.onNotification(setMemoryViewSettingsType, config => { | ||
for (const column of columnContributionService.getColumns()) { | ||
const id = column.contribution.id; | ||
const configurable = column.configurable; | ||
this.toggleColumn(id, !configurable || !!config.visibleColumns?.includes(id)); | ||
} | ||
this.setState(prevState => ({ ...prevState, ...config, title: config.title ?? prevState.title, })); | ||
}); | ||
messenger.sendNotification(readyType, HOST_EXTENSION, undefined); | ||
} | ||
|
||
public render(): React.ReactNode { | ||
const contextValue: MemoryAppContext = { | ||
...this.state, | ||
updateMemoryState: this.updateMemoryState, | ||
fetchMemory: this.fetchMemory, | ||
refreshMemory: this.refreshMemory, | ||
resetMemoryDisplayConfiguration: this.resetMemoryDisplayConfiguration, | ||
toggleColumn: this.toggleColumn, | ||
updateMemoryDisplayConfiguration: this.updateMemoryDisplayConfiguration, | ||
updateTitle: this.updateTitle | ||
}; | ||
|
||
return ( | ||
<MemoryAppContext.Provider value={contextValue}> | ||
{this.props.children} | ||
</MemoryAppContext.Provider> | ||
); | ||
} | ||
|
||
protected updateMemoryState = (newState: Partial<MemoryState>) => this.setState(prevState => ({ ...prevState, ...newState })); | ||
protected updateMemoryDisplayConfiguration = (newState: Partial<MemoryDisplayConfiguration>) => this.setState(prevState => ({ ...prevState, ...newState })); | ||
protected resetMemoryDisplayConfiguration = () => messenger.sendNotification(resetMemoryViewSettingsType, HOST_EXTENSION, undefined); | ||
protected updateTitle = (title: string) => { | ||
this.setState({ title }); | ||
messenger.sendNotification(setTitleType, HOST_EXTENSION, title); | ||
}; | ||
|
||
protected async setOptions(options?: Partial<DebugProtocol.ReadMemoryArguments>): Promise<void> { | ||
messenger.sendRequest(logMessageType, HOST_EXTENSION, `Setting options: ${JSON.stringify(options)}`); | ||
this.setState(prevState => ({ ...prevState, ...options })); | ||
return this.fetchMemory(options); | ||
} | ||
|
||
protected refreshMemory = () => { this.fetchMemory(); }; | ||
|
||
protected fetchMemory = async (partialOptions?: Partial<DebugProtocol.ReadMemoryArguments>): Promise<void> => this.doFetchMemory(partialOptions); | ||
protected async doFetchMemory(partialOptions?: Partial<DebugProtocol.ReadMemoryArguments>): Promise<void> { | ||
this.setState(prev => ({ ...prev, isMemoryFetching: true })); | ||
const completeOptions = { | ||
memoryReference: partialOptions?.memoryReference || this.state.memoryReference, | ||
offset: partialOptions?.offset ?? this.state.offset, | ||
count: partialOptions?.count ?? this.state.count | ||
}; | ||
|
||
try { | ||
const response = await messenger.sendRequest(readMemoryType, HOST_EXTENSION, completeOptions); | ||
await Promise.all(Array.from( | ||
new Set(columnContributionService.getUpdateExecutors().concat(decorationService.getUpdateExecutors())), | ||
executor => executor.fetchData(completeOptions) | ||
)); | ||
|
||
this.setState({ | ||
decorations: decorationService.decorations, | ||
memory: this.convertMemory(response), | ||
memoryReference: completeOptions.memoryReference, | ||
offset: completeOptions.offset, | ||
count: completeOptions.count, | ||
isMemoryFetching: false | ||
}); | ||
|
||
messenger.sendRequest(setOptionsType, HOST_EXTENSION, completeOptions); | ||
} finally { | ||
this.setState(prev => ({ ...prev, isMemoryFetching: false })); | ||
} | ||
|
||
} | ||
|
||
protected convertMemory(result: DebugProtocol.ReadMemoryResponse['body']): Memory { | ||
if (!result?.data) { throw new Error('No memory provided!'); } | ||
const address = BigInt(result.address); | ||
const bytes = Uint8Array.from(Buffer.from(result.data, 'base64')); | ||
return { bytes, address }; | ||
} | ||
|
||
protected toggleColumn = (id: string, active: boolean): void => { this.doToggleColumn(id, active); }; | ||
protected async doToggleColumn(id: string, isVisible: boolean): Promise<void> { | ||
const columns = isVisible ? await columnContributionService.show(id, this.state) : columnContributionService.hide(id); | ||
this.setState(prevState => ({ ...prevState, columns })); | ||
} | ||
} |
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
Oops, something went wrong.